DS Unit-2 Stack
DS Unit-2 Stack
GTU # 3130702
Unit-2
Linear Data
Structure
(Stack)
Stack
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 a LIFO (Last In First Out) list.
Deletio
n
Insertio
C n
B … …
A
TO
P
Stack Cont…
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.
Applications of Stack
Recursion
Keeping track of function calls
Evaluation of expressions
Reversing characters
Servicing hardware interrupts
Solving combinatorial problems using backtracking
Expression Conversion (Infix to Postfix, Infix to Prefix)
Game Playing (Chess)
Microsoft Word (Undo / Redo)
Compiler – Parsing syntax & expression
Finding paths
Procedure : PUSH (S, TOP, X)
This procedure inserts an element X to the top of a stack.
Stack is represented by a vector S containing N elements.
A pointer TOP represents the top element in the stack.
a+b*c+d*e
1 2
3
4
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
c ((+^ ab 2
Then write (‘INVALID’)
EXIT ^ c
((+^ abc 3
Repeat while G(S[TOP]) > F(NEXT) d ^
((+^ abc 3
TEMP POP (S, TOP)
POLISH POLISH O TEMP ) ^d
((( abcd^^ 1
RANK RANK + R(TEMP) +
IF RANK <1
Then write (‘INVALID’)
EXIT
Empty
Read – , is it
Stack Read 5, is it operator? POP two
operand? PUSH
Read 6, is it 2 symbols and
perform operation
operand? PUSH
Read 2, is it 6 and PUSH result 4
operand? PUSH 5 Operan – Operan 5
d1 d2
Read + , is it
Read next operator? POP two
symbol, if it is symbols and perform
end of string, operation and PUSH
POP answer result
Answ 9
er
from Stack Operan
d1
+ Operan
d2
Algorithm: EVALUATE_POSTFIX
Given an input string POSTFIX representing postfix expression.
This algorithm evaluates postfix expression and put the result into
variable VALUE.
Stack is represented by a vector S, TOP denotes the top of the stack,
Algorithm PUSH and POP are used for stack manipulation.
Function NEXTCHAR returns the next symbol in given input string.
OPERAND1, OPERAND2 and TEMP are used for temporary variables
PERFORM_OPERATION is a function which performs required operation
on OPERAND1 & OPERAND2.
Algorithm: EVALUATE_POSTFIX
1. [Initialize Stack]
TOP 0
VALUE 0
2. [Evaluate the postfix expression]
Repeat until last character
TEMP NEXTCHAR (POSTFIX)
If TEMP is DIGIT
Then PUSH (S, TOP, TEMP)
Else OPERAND2 POP (S, TOP)
OPERAND1 POP (S, TOP)
VALUE PERFORM_OPERATION(OPERAND1, OPERAND2, TEMP)
PUSH (S, POP, VALUE)
3. [Return answer from stack]
Return (POP (S, TOP))
Evaluation of postfix expression
Evaluate Expression: 5 4 6 + * 4 9 3 / + *
Evaluate Expression: 7 5 2 + * 4 1 1 + / -
Evaluate Expression: 12, 7, 3, -, /, 2, 1, 5, +, *, +
Algorithm: EVALUATE_PREFIX
Given an input string PREFIX representing prefix expression.
This algorithm evaluates prefix expression and put the result into variable
VALUE.
Stack is represented by a vector S, TOP denotes the top of the stack,
Algorithm PUSH and POP are used for stack manipulation.
Function NEXTCHAR returns the next symbol in given input string.
OPERAND1, OPERAND2 and TEMP are used for temporary variables
PERFORM_OPERATION is a function which performs required operation
on OPERAND1 & OPERAND2.
Algorithm: EVALUATE_PREFIX
1. [Initialize Stack]
TOP 0
VALUE 0
2. [Evaluate the prefix expression]
Repeat from last character up to first
TEMP NEXTCHAR (PREFIX)
If TEMP is DIGIT
Then PUSH (S, TOP, TEMP)
Else OPERAND1 POP (S, TOP)
OPERAND2 POP (S, TOP)
VALUE PERFORM_OPERATION(OPERAND1, OPERAND2, TEMP)
PUSH (S, POP, VALUE)
3. [Return answer from stack]
Return (POP (S, TOP))
Recursion
A procedure that contains a procedure call to itself or a procedure
call to second procedure which eventually causes the first
procedure to be called is known as recursive procedure.
Two important conditions for any recursive procedure
1 Each time a procedure calls itself it must be nearer in some sense to a solution.
2 There must be a decision criterion for stopping the process or computation.
Thank
You