Introduction To Stack 
By: 
Dr. Ghulam Rasool
Stack 
• A variable sized data structure in which insertion or deletion 
is made just from one end 
or 
• A LIFO data structure is called Stack 
Operations on Stack 
1) Push ( to insert an element into stack) 
2) Pop( to take an element out of stack) 
The last element inserted is deleted or taken out first and 
first element at last 
The stack can be implemented using Array as well as Link 
List
Stack using Arrays 
• The no of elements that can be inserted in a the stack is 
the dimension of array. The current position of stack(i.e 
its size is known by a pointer called its top. 
Algorithm to insert data in stack Push(S,Top, X, N) 
1 If Top >= N then 
wrtie (‘Stack full’) and exit 
2 Top=Top+1 
3 S[Top]= X 
4 Exit
Stack Operations 
Algorithm to delete an element from Stack Pop(S, Top) 
1 If Top= 0 then 
write(‘ stack is empty’) and exit 
2 Pop = S[Top] 
3 Top= Top-1 
4 Exit 
Applications of Stack 
i) Recursion 
ii) Evaluation of Arithmetic expressions 
a) Infix notation 
b) prefix notations 
c) postfix notations
Recursion 
• The calling of a subprogram to itself is called recursions 
Examples: Factorial of a number 
Fact(N) 
1) If N= 0 then 
Fact =1 
2) Fact= N* Fact(N-1) 
Polish Suffix notations 
• It is named after Polish mathematician Jan Lukasiewiez. 
The operator symbol is placed before its two operands in 
this notations 
• Examples: AB+, CD-, EF*
PSN notations 
• The expressions in which operands are preceded by the 
operators are called PSN notations or postfix notations. 
• Example: Following are PSN notations 
AB+ 
DE* 
ABC*+ 
• Convert A+B*C into PSN 
Symbol scanned operator Stack PSN 
A A 
+ + A 
B + AB 
* +* AB 
C +* ABC 
$ ABC *+
RPN(Q, P) 
• Suppose Q is expression in infix form. This algorithm 
covert this expression into equivalent postfix expression P 
1) Scan Q from left to right and repeat Steps 2 to 5 for each 
element of Q until the end of Expression 
2) If an operand is encountered, add it to P 
3) If a left parenthesis is encountered, push it onto stack 
4) If an operator is encountered then: 
a) Repeatedly pop from stack and add to P each operator 
which has the same precedence as or higher precedence 
than (?) 
b) Add (?) to stack 
5) If a right parenthesis is encountered then: 
a) Repeatedly pop from stack and add to P each operator 
until a left parenthesis is encountered 
b) remove left parenthesis 
6) Exit
Quiz 1 
• Q.1 What is difference b/w Stack and Array. 
Can a stack be an array and an array can be a 
stack? 
• Q.2 Convert following expressions into PSN 
((A- (B+C)) *D)^(E+F)
Assignment I 
Q.2 Write a program that should take infix expression and convert it 
into PSN. The program should also evaluate PSN expression.
Algorithm for evaluation of PSN 
1 Scan the Postfix string from left to right. 
2 Initialise an empty stack. 
3 Repeat step 4 until the end of string 
4 If the scanned character is an operand, Push 
it to the stack. 
a) If the scanned character is an Operator, pop top 
two elements and apply operator. 
b) Push result back on top of stack 
5 Pop last value from stack 
5 Exit.
Example 
• infix expression: 1+ 2 *3 
• Postfix : 1 2 3 + * 
Symbol Stack 
1 1 
2 1, 2 
3 1, 2, 3 
+ 1, 5 
* 5
Towers of Hanoi 
• Suppose three Pegs labled A, B and C and Suppose on Peg A 
there are placed a finite number n of disks with decreasing size. 
• The objective of game is to move the disks from Peg A to PegC 
using Peg B as an intermediate. 
• The rules of game are as: 
i) Only one disk may be moved at a time, specifically only the 
top disk on any Peg may be moved to any other Peg. 
ii) A larger disk can not be placed on a smaller disk 
• Example: For n=3 
• Move top disk from A to C Move top disk from A to C 
• Move top disk from A to B Move top disk from B to A 
• Move top disk from C to B Move top disk from B to C 
• Move top disk from A to C
Recursive Solution 
1 Move top n-1 disks from A to B 
2 Move the top disk from A to C 
3 Move the top n-1 disks from B to C 
General notation: 
Tower(N, Beg, Sec, End) 
When N=1 
Tower(1, Beg, Sec, End) 
When N>1 
Tower( N-1 Beg, End, Sec) 
Tower(1, Beg, Sec, End) Beg …> End 
Tower( N-1, Sec, Beg, End)

Introduction To Stack

  • 1.
    Introduction To Stack By: Dr. Ghulam Rasool
  • 2.
    Stack • Avariable sized data structure in which insertion or deletion is made just from one end or • A LIFO data structure is called Stack Operations on Stack 1) Push ( to insert an element into stack) 2) Pop( to take an element out of stack) The last element inserted is deleted or taken out first and first element at last The stack can be implemented using Array as well as Link List
  • 3.
    Stack using Arrays • The no of elements that can be inserted in a the stack is the dimension of array. The current position of stack(i.e its size is known by a pointer called its top. Algorithm to insert data in stack Push(S,Top, X, N) 1 If Top >= N then wrtie (‘Stack full’) and exit 2 Top=Top+1 3 S[Top]= X 4 Exit
  • 4.
    Stack Operations Algorithmto delete an element from Stack Pop(S, Top) 1 If Top= 0 then write(‘ stack is empty’) and exit 2 Pop = S[Top] 3 Top= Top-1 4 Exit Applications of Stack i) Recursion ii) Evaluation of Arithmetic expressions a) Infix notation b) prefix notations c) postfix notations
  • 5.
    Recursion • Thecalling of a subprogram to itself is called recursions Examples: Factorial of a number Fact(N) 1) If N= 0 then Fact =1 2) Fact= N* Fact(N-1) Polish Suffix notations • It is named after Polish mathematician Jan Lukasiewiez. The operator symbol is placed before its two operands in this notations • Examples: AB+, CD-, EF*
  • 6.
    PSN notations •The expressions in which operands are preceded by the operators are called PSN notations or postfix notations. • Example: Following are PSN notations AB+ DE* ABC*+ • Convert A+B*C into PSN Symbol scanned operator Stack PSN A A + + A B + AB * +* AB C +* ABC $ ABC *+
  • 7.
    RPN(Q, P) •Suppose Q is expression in infix form. This algorithm covert this expression into equivalent postfix expression P 1) Scan Q from left to right and repeat Steps 2 to 5 for each element of Q until the end of Expression 2) If an operand is encountered, add it to P 3) If a left parenthesis is encountered, push it onto stack 4) If an operator is encountered then: a) Repeatedly pop from stack and add to P each operator which has the same precedence as or higher precedence than (?) b) Add (?) to stack 5) If a right parenthesis is encountered then: a) Repeatedly pop from stack and add to P each operator until a left parenthesis is encountered b) remove left parenthesis 6) Exit
  • 8.
    Quiz 1 •Q.1 What is difference b/w Stack and Array. Can a stack be an array and an array can be a stack? • Q.2 Convert following expressions into PSN ((A- (B+C)) *D)^(E+F)
  • 9.
    Assignment I Q.2Write a program that should take infix expression and convert it into PSN. The program should also evaluate PSN expression.
  • 10.
    Algorithm for evaluationof PSN 1 Scan the Postfix string from left to right. 2 Initialise an empty stack. 3 Repeat step 4 until the end of string 4 If the scanned character is an operand, Push it to the stack. a) If the scanned character is an Operator, pop top two elements and apply operator. b) Push result back on top of stack 5 Pop last value from stack 5 Exit.
  • 11.
    Example • infixexpression: 1+ 2 *3 • Postfix : 1 2 3 + * Symbol Stack 1 1 2 1, 2 3 1, 2, 3 + 1, 5 * 5
  • 12.
    Towers of Hanoi • Suppose three Pegs labled A, B and C and Suppose on Peg A there are placed a finite number n of disks with decreasing size. • The objective of game is to move the disks from Peg A to PegC using Peg B as an intermediate. • The rules of game are as: i) Only one disk may be moved at a time, specifically only the top disk on any Peg may be moved to any other Peg. ii) A larger disk can not be placed on a smaller disk • Example: For n=3 • Move top disk from A to C Move top disk from A to C • Move top disk from A to B Move top disk from B to A • Move top disk from C to B Move top disk from B to C • Move top disk from A to C
  • 13.
    Recursive Solution 1Move top n-1 disks from A to B 2 Move the top disk from A to C 3 Move the top n-1 disks from B to C General notation: Tower(N, Beg, Sec, End) When N=1 Tower(1, Beg, Sec, End) When N>1 Tower( N-1 Beg, End, Sec) Tower(1, Beg, Sec, End) Beg …> End Tower( N-1, Sec, Beg, End)