Unit 2 Stack
Unit 2 Stack
Unit 2 Stack
Stack
struct STACK{
int count;// keeps the number of elements in the stack
int top;// indicates the location of the top of the stack
int items[STACKSIZE];//array to store the stack
elements
}
Stacks
Push Operation
• Push an item onto the top of the stack (insert an item)
push()
Algorithm
1. [check for underflow of the stack]
if TOP=-1
then Write(“stack underflow on peep)
Exit
2. [return former top element of the stack]
Return(S[TOP])
EXAMPLE
• The 10 elements entered by the user are pushed into the stack
and then the elements are popped back and displayed.
Stack Implementation using Arrays
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 10
struct STACK{
int count;
int top;
int items[STACKSIZE]; /*stack can contain up to 10 integers*/
};
struct Node {
int data;
struct Node *next;
}*top = NULL;
push(value) - Inserting an element into the Stack
• Language processing :
• space for parameters and local variables is created internally using a stack.
• compiler's syntax check for matching braces is implemented by using stack.
Example: 8/4*2
will give different results if we perform division first and
then multiplication or vice versa.
Precedence and Associativity
• For example −
2. (A + B) / (C + D) – (D * E)
[AB +] / [CD+] – [DE *]
[AB + CD + /] – [DE *]
AB + CD + / DE * -
Postfix notation
•In postfix notation, operators are applied to
the operands that are immediately left to
them.
(A + B) * C
[+ AB] * C
* + ABC
Prefix notation
1. (A - B) * (C + D)
[- AB] * [+ CD]
* - AB + CD
2. (A + B) / (C + D) – (D * E)
[+ AB] / [+ CD] – [* DE]
[/ + AB + CD] – [* DE]
- / + AB + CD * DE
Infix to Postfix using Stack - Rules
1. Scan from left to right, we shall get a symbol which may be
operator or operand or parenthesis. Follow below steps.
i. If operand, add it to the postfix
ii. If opening parenthesis, push into stack
iii. If operator, then check the top of the stack
a) If stack is empty, push into stack
b) If top is opening parenthesis, push into stack
c) If precedence of operator at top is higher or same as the
current, then repeatedly it is popped and added to postfix
exp, otherwise pushed onto stack
iv. If symbol is closing parenthesis, then
a) repeatedly pop from stack and add each operator to
postfix exp until corresponding parenthesis encountered.
b) Remove opening parenthesis from the stack.
2. If Stack is not empty then pop each symbol and add it to the
postfix.
Example
Convert infix to postfix.
5*(6+2)-(12/4)
5*[62+]-[124/]
562+*-124/
562+*124/-
5*(6+2)-(12/4)
Symbol Scanned Stack Postfix Exp
1 5 5
2 * * 5
3 ( *, ( 5
4 6 *, ( 5, 6
5 + *, (, + 5, 6
6 2 *, (, + 5, 6, 2
7 ) * 5, 6, 2, +
8 - - 5, 6, 2, +, *
9 ( -,( 5, 6, 2, +, *
10 12 -,( 5, 6, 2, +, *, 12
11 / - , (, / 5, 6, 2, +, *, 12
12 4 - , (, / 5, 6, 2, +, *, 12, 4
13 ) - 5, 6, 2, +, *, 12, 4, /
14 5, 6, 2, +, *, 12, 4, /, -
Scanned Char Stack Postfix Exp
a+b*c-d/e*h
a a
+ + a
b + ab
* +,* ab
c +,* abc
- - abc*+
d - abc*+d
/ -,/ abc*+d
e -,/ abc*+de
* -,* abc*+de/
h -,* abc*+de/h
- abc*+de/h*
abc*+de/h*-
Practice
1. (a+((b*c)*d))
2. a/b^c-d
3. (a-b/c)*(d*e-f)
Infix to Prefix
A/B^C-D
A/^BC-D
/A^BC-D
-/A^BCD
Infix to Prefix using Stack - Rules
1. Scan from right to left or reverse the string.
I. If operand add to o/p string
II. If it is closing parenthesis, push on stack
III. If an operator, then
a) If stack is empty, push into stack
b) If top is closing parenthesis, push into stack
c) If precedence of operator at top is higher, pop from stack and
add to the o/p string Else push operator
IV. If symbol is opening parenthesis, then
a) pop operators from stack and add to o/p string until
corresponding closing parenthesis encountered.
b) Remove parenthesis from the stack.
2. If Stack is not empty then pop each symbol and add it to the o/p
string.
3. Reverse the output string.
A/B^C-D
Char Scanned Stack Prefix Exp
D D
- - D
C - DC
^ -^ DC
B -^ DCB
/ -/ DCB^
A -/ DCB^A
- DCB^A/
DCB^A/-
Reverse it
-/A^BCD
Practice
1. (A-B/C)*(D*E-F)
2. (A+((B*B)*D))
Evaluation of postfix using Stack
1. Scan from left to right
2. If operand is encountered push it into stack
3. If an operator ‘$’ is found,
1. Pop two elements from stack where B is the top
element and A is next top element
2. Evaluate A $ B
3. Push the result on stack
4. Repeat step 1 to 3.
5. Evaluated value is equal to the value at top of
stack
Example#1
• Postfix String : 123*+4-
• Initially the Stack is empty. Now, the first three
characters scanned are 1,2 and 3, which are
operands. Thus they will be pushed into the stack in
that order.
Example#1
• Next character scanned is "*", which is an operator.
Thus, we pop the top two elements from the stack
and perform the "*" operation with the two
operands. The second operand will be the first
element that is popped.
Example#1
• The value of the expression(2*3) that has been evaluated(6) is
pushed into the stack.
End result :
• Postfix String : 123*+4-
• Result : 3
5, 6, 2, +, *, 12, 4, /, -
Scanned Symbol Stack Operation
5 5
6 5, 6
2 5, 6, 2
+ 5, 8 6 + 2 (A=6, B=2)
* 40 5 * 8 (A=5, B=8)
12 40, 12
4 40, 12, 4
/ 40, 3 12 / 4 (A=12, B=4)
- 37 40 – 3(A=40, B=3)
Practice
1. 5 9 8 + 4 6 * + 7 - *
2. 7, 2, -, 1, 3, +, /
Tower of Hanoi
• Recursive application
• It says, “If you can solve n-1 cases, then you can easily
solve nth case”
Tower of Hanoi
• In problem,
• there are three posts.
• Disks of different sizes (call the number of disks "n") are placed
on the lefthand post, arranged by size with the smallest on top.
• You have to transfer all the disks to the right hand post in the
fewest possible moves, without ever placing a larger disk on a
smaller one.
• One move is considered to be moving one disk from one post to
another post.
Tower of Hanoi
Tower of Hanoi - Recursive
N=3 N = 2 in Step 1
Src a a move(n-1, src, spare, dst) ;
Dst c b move(1, src, dst, spare);
Spr b c
move(n-1, spare, dst, src);
Tower of Hanoi - Recursive
Step2: move(1, a, c, b); here n=1, so move is possible [a->c]
Step3: move(2, b, c, a); here n=2, so recursion will take
place
move(1, b, a, c) ; here n=1, so move is possible [b->a]
move(1, b, c, a) ; here n=1, so move is possible [b->c]
move(1, a, c, b) ; here n=1, so move is possible [a->c]
1. http://britton.disted.camosun.bc.ca/hanoi.swf
2. https://www.youtube.com/watch?v=5_6nsViVM00