Stack
Stack
Stack
1
Stacks are known as LIFO (Last In, First Out) lists:
The last element inserted will be the first to be
retrieved, using Push and Pop
Attributes of Stack
MAXSTACK: which gives the maximum
number of elements that can be held by the stack.
TOP: the index of the top element of stack
Operations of Stack
push: add an element to the top of stack
2
Array Representation
STACK
of Stack
A B C
1 2 3 4 5 6 7 8 9
3 9
TOP MAXSTACK
STACK: Linear Array
TOP: Pointer variable, which contains the location of top element of the stack.
MAXSTACK: A variable, which gives the maximum number of elements that can
be held by the stack.
If TOP=0 or TOP=NULL Stack is empty and this condition is
known as underflow.
If TOP= MAXSTACK Stack is Full and this condition is known
as Overflow.
3
Push and Pop operations on Stack
PUSH(STACK,TOP,MAXSTACK,ITEM) POP(STACK,TOP,ITEM)
top
B
top top A
A A
top
4
Linked Representation of
Stacks
Linked Stack is a stack that is implemented
using a singly linked list
INFO field of the node holds the element of
the stack
LINK field of the node holds pointer to the
neighboring element in the stack
5 10 3 7
INFO LINK
5
PUSH operation in Linked Stack
8
Evaluation of a postfix notation
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat Step3 and 4 for each
element of P until the sentinel ”)” is encountered.
3. If an operand is encountered, push it on STACK.
4. If an operator X is encountered, then:
(a) Remove the two top elements of STACK, where A is the
top element and B is the next to top element.
(b) Evaluate B X A.
(c) Place the result of (b) back on STACK.
5. Set VALUE equal to the top element on the STACK.
6. Exit.
9
Example
10
Symbol Scanned STACK
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
(10) )
11
Infix to Postfix Conversion
POLISH(Q,P)
1. Push “(” onto STACK and add “)” to the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the
STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator X is encountered , then:
1. Repeatedly pop from STACK and add to P each operator which has
the same or higher precedence than X.
2. Add X to STACK.
6. If a right parenthesis is encountered, then:
1. Repeatedly pop from STACK and add to P each operator until a left
parenthesis is encountered.
2. Remove the left parenthesis.
7. Exit.
12
Example: Infix to Postfix Expression A+(B*C-(D/E↑F)*G)*H
Symbol Scanned
(1) A ( A
(2) + (+ A
(3) ( (+( A
(4) B (+( AB
(5) * (+(* AB
(6) C (+(* ABC
(7) - (+(- ABC*
(8) ( (+(-( ABC*
(9) D (+(-( ABC*D
(10) / (+(-(/ ABC*D
(11) E (+(-(/ ABC*DE
14
RECURSION: Application of Stack
To calculate factorial of a number
int fact (int n)
{
if ( n==1)
return 1;
return (n * fact (n-1) );
}
To make Fibonacci Series is another
example of RECURSION.
15
Towers of Hanoi : Application of
Stack
Three pegs labeled A, B,C
In A, there are finite number of disks with
decreasing size
Objective- move the disks from A to C using
B as an auxiliary.
The rules of the game are as follows
Only one disk may be moved at a time.
At no time can a larger disk be placed on a
smaller disk
16
A B C
17
If n=3
A C A B C B A C B A B C A C
For n disks
1. Move the top n-1 disks from peg A to peg B
2. Move the top disk from peg A to C
3. Move the top n-1 disks from peg B to peg C
18
Tower (2, A,B,C)
Tower(2, A,B,C)
19