Stack Data Structure with example
Stack Data Structure with example
(iv)pop(s)-stack underflow
STACK IMPLEMENTATION
Stack data structure is not inherently provided by many programming
languages.
Stack is implemented using arrays or linked lists.
Let S be a stack, n be the capacity, x be the element to be pushed, then
push and pop will be given as
Push(S,x) and Pop(S)
Here we use “top” which keeps track of the top element in the stack.
When top = = -1 , and pop() operation gives stack underflow as result.
When top = = n-1, and push() operation gives stack overflow as result.
The pop() operation just gives an illusion of deletion, but the elements
are retained. Only the top is decremented.
S[0:5] Pop(S) a b c d
e Top=4
Push(S,a a Pop(S) a
) Pop(S) Top=0
Top=0 Pop(S)
Pop(S)
Push(S,a) a b c d e
Push(S,b) f
Push(S,c) Top=5 Pop(S)
push(S,d) Top=-1
Push(S,e)
push(S,f)
Push(S,g a b c d e Pop(S)
) f
Top=5 = n-1 Top=-1 stack underflow
Stack overflow
STACK APPLICATIONS
Evaluation of expression
Conversion of infix to postfix expression
Computation of postfix expression
Parenthesis handling
Backtracking
Conversion of decimal to other number system
Maze tracer
Undo operations
PARENTHESIS CHECKING
Procedure check()
Declare a character stack S.
Now traverse the expression.
a) If the current character is a starting bracket then push it to
stack.
b) If the current character is a closing bracket then pop from
stack and if the popped character is the matching starting bracket
then fine else parenthesis are not balanced.
After complete traversal, if there is some starting bracket left in stack
then “not balanced”
End procedure
Eg: [a+(b*c)+{(d-e)}]
[ Push [
[ ( Push (
[ { Push {
[ { ( Push (
matches, pop (
[ {
[ Matches, pop {
Matches, pop [
operands.
This is the usual way we write expressions.
An expression such as A * ( B + C ) / D is
Polish notation"): X Y +
Operators are written after their operands.
left-to-right
Prefix Expression
Prefix notation (also known as "Polish
notation"): + X Y
INFIX TO POSTFIX CONVERSION
Scan the Infix expression left to right
If the character x is an operand
Output the character into the Postfix Expression
If the character x is a left or right parenthesis
If the character is “(“
Push it into the stack
if the character is “)”
Repeatedly pop and output all the operators/characters
until “(“ is popped from the stack.
If the character x is a is a regular operator
Step 1: Check the character y currently at the top of the
stack.
Step 2: If Stack is empty or y=‘(‘ or y is an operator of
Postfix Expression
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
a+b-c)*d–(e+f)
Postfix Expression
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
+b-c)*d–(e+f)
Postfix Expression
a
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
b-c)*d–(e+f)
Postfix Expression
a
+
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
-c)*d–(e+f)
Postfix Expression
ab
+
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
c)*d–(e+f)
Postfix Expression
ab+
-
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
)*d–(e+f)
Postfix Expression
ab+c
-
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
*d–(e+f)
Postfix Expression
ab+c-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
d–(e+f)
Postfix Expression
ab+c-
*
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
–(e+f)
Postfix Expression
ab+c-d
*
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
(e+f)
Postfix Expression
ab+c–d*
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
e+f)
Postfix Expression
ab+c–d*
(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
+f)
Postfix Expression
ab+c–d*e
(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
f)
Postfix Expression
+ ab+c–d*e
(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
)
Postfix Expression
+ ab+c–d*ef
(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
Postfix Expression
ab+c–d*ef+
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
Postfix Expression
ab+c–d*ef+-
Eg: (A*B)+C
( ( Push (
A ( A Print A
* ( * Push(*)
B ( * AB Print B
+ Push +
+
C + AB*C Print C
$ AB*C+ Pop +
Convert infix expression to postfix
expression
( ( A + B ) — C * ( D / E ) ) + F
( ( A + B ) * ( C – D ) + E ) / (F + G)
EVALUATION OF POSTFIX EXPRESSION
Postfix expression is easily evaluated by the compiler by using stack.
The procedure for the evaluation of postfix expression is given below:
procedure eval(E)
x=getnextchar(E);
case x:
x is an operand: push x into stack S.
x is an operator: pop elements, perform operation and
push result into stack.
x is null: pop stack and print result
end case
End procedure
Eg: AB*C+ A=2, B=3, C=5
C 6 Push C
C
+ Pop C and 6,
1
C+6, push 11
1
Result:11
POSFIX EVALUATION
231*+9–
4 5 7 2 + – *
3 4 + 2 * 7 /