Prepared By: Dr. Dhruti Sharma, IT, SCET
Stack-Definitions & Concepts,
Operations On Stacks,
Applications of Stacks,
Polish Expression, Reverse Polish Expression And Their Compilation,
Recursion, Tower of Hanoi
Prepared By: Dr. Dhruti Sharma, IT, SCET
A linear list which allows insertion and deletion of an element at one
end only is called stack.
The insertion operation is called as PUSH and deletion operation as
POP.
The most accessible elements in stack is known as top.
The elements can only be removed in the opposite orders from that
in which they were added to the stack.
Such a linear list is referred to as aDeletion
LIFO (Last In First Out) list.
Insertion
C
B … …
A
A pointer TOP keeps track of the top element in the stack.
Initially, when the stack is empty, TOP has a value of “zero”.
Each time a new element is inserted in the stack, the pointer is
incremented by “one” before, the element is placed on the stack.
The pointer is decremented by “one” each time a deletion is made
from the stack.
Prepared By: Dr. Dhruti Sharma, IT, SCET
Procedure : PUSH (S, TOP, X)
o This procedure inserts an element X to the top of a stack.
o Stack is represented by a vector S containing N elements.
o A pointer TOP represents the top element in the stack.
Initially, Stack is empty, TOP = 0, N=3
1. [Check for stack overflow]
If TOP ≥ N PUSH(S, TOP, 10) TOP = 3 -5
Then write (‘STACK OVERFLOW’) TOP = 2 8
Return PUSH(S, TOP, 8) TOP = 1 10
2. [Increment TOP] Stack S
TOP ← TOP + 1 PUSH(S, TOP, -5)
3. [Insert Element]
S[TOP] ← X PUSH(S, TOP, 6)
4. [Finished]
PreparedReturn
By: Dr. Dhruti Sharma, IT, SCET Overflow (as TOP=3)
Procedure : POP (S, TOP)
o This function removes & returns the top element from a stack.
o Stack is represented by a vector S containing N elements.
o A pointer TOP represents the top element in the stack.
1. [Check for stack underflow] POP(S, TOP) TOP = 3 -5
If TOP = 0 TOP = 2 8
Then write (‘STACK UNDERFLOW’) TOP = 1 10
POP(S, TOP)
Return (0) TOP = 0 S
2. [Decrement TOP] POP(S, TOP)
TOP ← TOP - 1
3. [Return former top element of stack]
POP(S, TOP)
Return(S[TOP + 1])
Underflow
Function : PEEP (S, TOP, I)
o This function returns the value of the Ith (1 <= I <= TOP) element from the TOP
of the stack. The element is not deleted by this function.
o Stack is represented by a vector S containing N elements.
1. [Check for stack underflow] -5
If TOP-I+1 ≤ 0 PEEP (S, TOP, 2) TOP = 3
8
Then write (‘STACK UNDERFLOW’) 10
Return (0) PEEP (S, TOP, 3)
S
2. [Return Ith element from top
of the stack] PEEP (S, TOP, 4)
Return(S[TOP–I+1])
Underflow
PROCEDURE : CHANGE (S, TOP, X, I)
o This procedure changes the value of the Ith element from the top of the stack to
X.
o Stack is represented by a vector S containing N elements.
1. [Check for stack underflow] CHANGE (S, TOP, 50, 2) TOP = 3 -5
If TOP-I+1 ≤ 0 8
50
Then write (‘STACK UNDERFLOW’) 9
10
Return CHANGE (S, TOP, 9, 3) S
2. [Change Ith element from top
of the stack] CHANGE (S, TOP, 25, 8)
S[TOP–I+1] ← X
3. [Finished]
Return Underflow
Conversion of Infix to postfix notation
Evaluation of postfix expression
Prepared By: Dr. Dhruti Sharma, IT, SCET
Conversion from Infix Expression
a+b*c+d*e
1 2
3
4
A repeated scanning from left to right is needed as operators appears
inside the operands.
Repeated scanning is avoided if the infix expression is first converted to
an equivalent parenthesis free prefix or suffix (postfix) expression.
Prefix Expression: Operator, Operand, Operand
Postfix Expression: Operand, Operand, Operator
Prepared By: Dr. Dhruti Sharma, IT, SCET
This type of notation is known Lukasiewicz Notation or Polish
Notation (Prefix) or Reverse Polish Notation(Postfix) due to Polish
logician Jan Lukasiewicz.
In both prefix and postfix equivalents of an infix expression, the
variables are in same relative position.
The expressions in postfix or prefix form are parenthesis free and
operators are rearranged according to rules of precedence for
operators.
Prepared By: Dr. Dhruti Sharma, IT, SCET
Sr. Infix Postfix (Reverse Polish) Prefix (Polish)
1 a a a
2 a+b ab+ +ab
3 a+b+c ab+c+ ++abc
4 a + (b + c) abc++ +a+bc
5 a + (b * c) abc*+ +a * b c
6 a * (b + c) abc+* *a+bc
7 a*b*c a b *c* ** a b c
a+b+c a+b+c (ab+)+ c (ab+) c + ab+c+
Prepared By: Dr. Dhruti Sharma, IT, SCET
Every operator/variable has some precedence.
Also a rank is assigned to each operator/variable. Typically, a rank
function r() (as shown below) is used
r(sj) = 1 for 1<=j<=26 and r(+)=r(-)=r(*)=r(/)=-1
Symbol Input precedence Rank function (R)
function (F)
+, - 1 -1
*, / 2 -1
Variables 3 1
Prepared By: Dr. Dhruti Sharma, IT, SCET
The validity of any expression can be check by finding its rank. If
rank is “1” then expression is “valid”.
Exp = ( A + B * C / D - E + F / G / ( H + I ))
Rank (Exp) = R(A) + R(+) + R(B) + R(*) + R(C) + R (/) + R(D) + R(-) + R(E) + R(+)
+ R(F) + R(/) + R(G) + R(/) + R(H) + R(+) + R(I)
Rank (Exp) = 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1)
+ 1 + (-1) + 1
Rank (Exp) = 1 and so expression is valid.
Symbol Input precedence Stack precedence Rank function (R)
function (F) function (G)
+, - 1 2 -1
*, / 3 4 -1
^ 6 5 -1
Variables 7 8 1
( 9 0 -
) 0 - -
Prepared By: Dr. Dhruti Sharma, IT, SCET
Create an empty stack called stack for keeping operators. Create
an empty list for output.
Read the character list from left to right and perform following
steps
o If the character is an operand (Variable), append it to the end of the output list
o If the character is a left parenthesis ‘(’, push it on the stack
o If the character is a right parenthesis ‘)’, pop the stack until the corresponding
left parenthesis ‘(’ is removed. Append each operator to the end of the output
list.
o If the character is an operator, *, /, +, or -, push it on the stack. However, first
remove any operators already on the stack that have higher or equal
precedence and append them to the output list.
(a+b^c^d)*(e+f/d)
Prepared By: Dr. Dhruti Sharma, IT, SCET
1. [Initialize Stack] 5. [Remove symbols with greater
TOP 1 precedence from stack]
S[TOP] ← ‘(’ IF TOP < 1
2. [Initialize output string and rank Then write (‘INVALID’)
count] EXIT
POLISH ‘’ Repeat while G(S[TOP]) > F(NEXT)
RANK 0 TEMP POP (S, TOP)
POLISH POLISH O TEMP
3. [Get first input symbol]
RANK RANK + R(TEMP)
NEXT NEXTCHAR(INFIX)
IF RANK <1
4. [Translate the infix expression] Then write (‘INVALID’)
Repeat thru step 7 EXIT
while NEXT != ‘‘
6. [Are there matching parentheses]
Symbol IPF (F) SPF (G) RF (R) IF G(S[TOP]) != F(NEXT)
+, - 1 2 -1 Then call PUSH (S,TOP, NEXT)
Else POP (S,TOP)
*, / 3 4 -1
7. [Get next symbol]
^ 6 5 -1 NEXT NEXTCHAR(INFIX)
Variables 7 8 1 8. [Is the expression valid]
IF TOP != 0 OR RANK != 1
( 9 0 -
Then write (‘INVALID‘)
) 0 - - Else write (‘VALID’)
Input Content Reverse polish Rank
(( a + b ^ c ^ d ) * ( e + f / d ) ) Symbo of stack expression
l ( 0
NEXT (
1. [Initialize Stack]
TOP 1
S[TOP] ← ‘(’
2. [Initialize output string and rank
count]
POLISH ‘’
RANK 0
3. [Get first input symbol]
NEXT NEXTCHAR(INFIX)
Input Content of Reverse Rank
(a+b^c^d)*(e+f/d)) Symbol stack polish
( expression 0
NEXT ( 0
((
4. [Translate the infix expression]
Repeat thru step 7 a ((a 0
while NEXT!= ‘ ‘ + ((+
(( a 1
5. [Remove symbols with greater precedence from b ((+b a 1
stack] ((+
((+^ ab 2
^
IF TOP < 1
Then write (‘INVALID’)
c ((+^c ab 2
EXIT ^ ((+^^
((+^ abc 3
Repeat while G(S[TOP]) > F(NEXT) abc
TEMP POP (S, TOP)
d ((+^^d 3
POLISH POLISH O TEMP ) ((
( abcd^^+ 1
RANK RANK + R(TEMP)
IF RANK <1
Then write (‘INVALID’)
EXIT
6. [Are there matching parentheses]
IF G(S[TOP]) != F(NEXT)
Then call PUSH (S,TOP, NEXT)
Else POP (S,TOP)
7. [Get next symbol]
NEXT NEXTCHAR(INFIX)
Convert the following infix expression to postfix. Show the stack
contents.
o A$B-C*D+E$F/G
o A+B–C*D*E$F$G
o a+b*c-d/e*h
o ((a+b^c^d)*(e+f/d))
Convert the following infix expression to prefix.
o A+B–C*D*E$F$G
Prepared By: Dr. Dhruti Sharma, IT, SCET
Each operator in postfix string refers to the previous two operands in
the string.
Each time we read an operand, we PUSH it onto Stack.
When we reach an operator, its operands will be top two elements on
the stack.
We can then POP these two elements, perform the indicated operation
on them and PUSH the result on the stack so that it will available for
use as an operand for the next operator.
Prepared By: Dr. Dhruti Sharma, IT, SCET
Evaluation of postfix expression
Evaluate Expression: 5 6 2 - +
Empty Stack
Read – , is it operator? POP
Read 5, is it operand? PUSH two symbols and perform
Read 6, is it operand? PUSH 2 operation and PUSH result
Read 2, is it operand? PUSH 6 4
5 Operand 1 – Operand 2 5
Read + , is it operator? POP
two symbols and perform
Read next symbol, if it
operation and PUSH result
is end of string, POP
answer from Stack
Answer 9 Operand 1 + Operand 2