Stack
Stack
-1 Stack is Empty
Here, the Peek operation will return 5, as it is the value of the topmost
element of the stack
1.1.6 Count Operation: count the number of element of the stack. (the
number element equivalent to top+1 )
1.2Applications of Stack
The simplest application of a stack is to reverse a word. You push a given
word to stack - letter by letter - and then pop letters from the stack.
There are other uses also like:
1. Parsing(A parser is a compiler / interpreter component that breaks data
into smaller elements for easy translation into another language. )
2. Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
3. A parentheses balancing program.
4. Tracking of local variables at run time.
5. Compiler Syntax Analyzer.
6. Redo-undo features at many places like editors, photoshop.
7. Forward and backward feature in web browsers
8. Used in recursion, backtracking, graph algorithm .
The time complexities for push() and pop() functions are O(1) because we
always have to insert or remove the data from the top of the stack, which is
a one step process.
Both pre- and postfix have basically the same advantages over infix
notation. The most important of these are:
Step1. Add unique symbol# into the stack and at end of array
infix.
Step2. Scan the symbol of array(infix expression) one by one
from left to right
Step3. If the symbol is left parenthesis ‘(‘ then add it into the
stack without checking any condition.
Step4. If the symbol is operand then add it into the output postfix
expression
Step5.
a)If symbol is operator then pop the operator which has same
precedence or higher precedence than the operator which
occurs. (ISP>=ICP)
b) add all pop operator to postfix expression .
Step6. Add scan symbol operator into stack.
Step7. i) If symbol is right parenthesis ‘)’ then pop the entire operator
from the stack until left parenthesis in stack.
ii) Remove the left parenthesis
Step8. If the pop symbol is # then pop all symbols from stack and
add them to the postfix expression except #symbol.
Step9. Do the same process until # comes in scanning array (infix
expression).
#
A A
* #* A
( #*( A
B #*( AB
+ #*(+ AB
C #*(+ ABC
^ #*(+^ ABC
D #*(+^ ABCD
) #* ABCD^+
- #- ABCD^+*
E #- ABCD^+*E
^ #-^ ABCD^+*E
F #-^ ABCD^+*EF
* #-* ABCD^+*EF^
( #-*( ABCD^+*EF^
G ABCD^+*EF^G
/ #-*(/ ABCD^+*EF^G
H #-*(/ ABCD^+*EF^GH
) #-* ABCD^+*EF^GH/*-
Infix to prefix
A*(B+C^D)-E^F*(G/H) =
)H/G(*F^E-)D^C+B(*A
(H/G)*F^E-(D^C+B)*A
Char Stack prefix
( (
H ( H
/ (/ H
G (/ HG
) HG/
* * HG/
F * HG/F
^ *^ HG/F
E *^ HG/FE
- - HG/FE^*
( -( HG/FE^*
D -( HG/FE^*D
^ -(^ HG/FE^*D
C -(^ HG/FE^*DC
+ -(+ HG/FE^*DC^
B -(+ HG/FE^*DC^B
) - HG/FE^*DC^B+
* -* HG/FE^*DC^B+
A -* HG/FE^*DC^B+A
HG/FE^*DC^B+A*-
-*A+B^CD*^EF/GH
Postfix evaluation
CHAR STACK
4 4
5 4,5
4 4,5,4
2 4,5,4,2
^ 4,5,16
+ 4,21
* 84
2 84,2
2 84,2,2
^ 84,4
9 84,4,9
3 84,4,9,3
/ 84,4,3
* 84,12
- 72
Prefix evaluation
+, -, 2, 7, *, 8, /, 8, 4
4,8,/,8,*,7,2,-,+
Char stack
4 4
8 4,8
/ 2
8 2,8
* 16
7 16,7
2 16,7,2
- 16,-5
+ 11
Postfix to infix
ABCD^+*EF^GH/*-
Char Stack
A A,
B A,B
C A,B,C
D A,B,C,D
^ A,B,(C^D)
+ A,(B+(C^D))
* (A*(B+(C^D)))
E (A*(B+(C^D))),E
F (A*(B+(C^D))),E,F
^ (A*(B+(C^D))), (E^F)
G (A*(B+(C^D))), (E^F),G
H (A*(B+(C^D))), (E^F),G,H
/ (A*(B+(C^D))), (E^F),(G/H)
* (A*(B+(C^D))), ((E^F)*(G/H))
- (A*(B+(C^D)))-((E^F)*(G/H))
Prefix to infix
-*A+B^CD*^EF/GH
HG/FE^*DC^B+A*-
Char Stack
H H,
G H,G
/ (G/H)
F (G/H),F
E (G/H),F,E
^ (G/H),(E^F)
* (E^F)* (G/H)
D (E^F)* (G/H), D
C (E^F)* (G/H),D,C
^ (E^F)* (G/H),(C^D)
B (E^F)* (G/H),(C^D),B
+ (E^F)* (G/H),(B+(C^D))
A (E^F)* (G/H),(B+(C^D)),A
* (E^F)* (G/H),(A*(B+(C^D)))
- (A*(B+(C^D)))-( (E^F)* (G/H))
POSTFIX TO PREFIX
ABCD^+*EF^GH/*-
CHAR STACK
A A
B A,B
C A,B,C
D A,B,C,D
^ A,B, ^CD
+ A,+B^CD
* *A+B^CD
E *A+B^CD,E
F *A+B^CD,E,F
^ *A+B^CD,^EF
G *A+B^CD,^EF,G
H *A+B^CD,^EF,G,H
/ *A+B^CD,^EF,/GH
* *A+B^CD,*^EF/GH
- -*A+B^CD*^EF/GH