Stack Notes unit-IV
Stack Notes unit-IV
UNIT-III Page 1
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.2 Stacks as an ADT (abstract data type)
UNIT-III Page 2
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.4.1 ‘C’ Function for primitive operations on stacks.
Q.Explain push and pop operation on stack using array representation.?
Q.Write a procedure to push element on stack?
Q.Write a C program to implement PUSH and POP function in stack as an array.
Q.What is the affect of push and pop operations on to the stack?
Q.Explain the concept of representing stack through arrays.?
Q.Explain operation on stack using array.
UNIT-III Page 3
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
5)int pop(stack *s)
int pop(stack *s)
{
int x;
x=sdata[stop];
stop=stop-1;
}
The function pop() return the top element from the stack. it also reduce the value of top by 1.
Stack overflow
Stack overflow is caused by insertion in stack that is already full.
C function for operation on stack
void push()
{
If(stop==MAX-1)
printf(“\ n overflow!!cannot be inserted”);
else
{
stop=stop+1;
sdata[stop]=x;
}
}
Stack underflow
Stack underflow is caused by deletion from an empty stack.
int pop(stack *s)
{
int x;
if(stop = = -1)
printf(“\ n underflow!!cannot be deleted”);
else
{
x=sdata[stop];
stop=stop-1;
}
return(x);
}
UNIT-III Page 4
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Reversing stack list
Q.Explain how stack can be used to reverse a list using suitable example.
Stack is LIFO(Last in first Out)structure .
It is an ordered list of the same types of elements.
A stack is a linear list where all insertion and deletion are permitted only at one end of list.
Reversal of list is application of stack.
In reverse list element is push one by one into stack ,once all elements are push(inserted) into
stack then last element comes first at the time of pop(delete) operation. because of in stack first
topmost element is pop, hence reversal of list occurs.
We push element 3,5,7 in stack
UNIT-III Page 5
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.6 Application of stack
Q.Explain any two application of stack.
Q.Write down application of stacks.
Q.State any four application of stack.
Q.Explain any three application of stack in details with example.
Application of stack
1)Expression conversion
a)Infix to postfix b)infix to prefix
c)Postfix to infix d)prefix to infix
2)Expression evaluation
3)Parsing
4)Simulation
5)Function call
Expression Representation
a)Evaluation of an infix expression
b)Evaluation of a postfix(reverse polish) expression
c)Evaluation of a prefix (polish notation) expression
UNIT-III Page 6
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
UNIT-III Page 7
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
UNIT-III Page 8
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Homework
Evaluate the following postfix expressions using stack
1) 5 4 6 + * 4 9 3 / + *
Ans :-350
2) 7 5 2 + * 4 1 1 + / -
Ans :-47
3)6 ,2,3,+,-,3,8,2,+,+,*,2, ,3,+
Ans :-172
UNIT-III Page 9
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
UNIT-III Page 10
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Conversion of expression from infix to postfix
Precedence of operator (Priority of operator)
1) ( )
2) ^ exponential
3) * /
4) + -
5) No two operator of same priority can stay together in the stack column.
6) ( ) if any operator come between open and close bracket then that operator is pop out from
stack
Q.Convert infix expression (A+B/C*(D+E)-F) into to postfix expression
UNIT-III Page 11
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q.Convert infix expression A*(B+C)/D-G into to postfix expression
UNIT-III Page 12
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
5^E+D*(C^B+A)
Home work
Conversion of expression from infix to Prefix
1)(A-B/C)*(D*E-F)
2) ((A+B)*(C+D))/(E+F)
3)(q-p)*(y+x)
UNIT-III Page 13
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q:Consider the following arithmetic expression P, written in postfix notaion.
P:12,7,3,-,1,2,+,5,+,*,+
P= 12,7,3,-,1,2,+,5,+,*,+
P= 12,(7-3),1,2,+,5,+,*,+
P= 12,(7-3),(1+2),5,+,*,+
P= 12,(7-3),(1+2+5),*,+
P= 12,(7-3)*(1+2+5),+
P= 12+(7-3)*(1+2+5) INFIX
P=12+4*8
P=12+32=44
Homework
Q:Consider the following arithmetic expression P, written in postfix notaion
1) P: 5, 6, 2, +,*, 12, 4,/,-
A) 37 B)73 C) 44 D)50
2)P:4,2, ,3,*,3,-,8,4,/,1,1,+,/,+
A) 46 B)73 C) 87 D)88
3)P:10,2,*,15,3,/,+,12,3,2, ,+,+
A) 46 B)77 C) 89 D)88
4)P:12,7,3,-,/,2,1,5,+,*,+
A) 15 B)24 C) 30D)32
Factorial(n)= 1 if(n=0)
n*factorial(n-1) otherwise
UNIT-III Page 14
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the
larger one. There are other variations of the puzzle where the number of disks increase, but the tower
count remains the same.
Rules
Only one disk can be moved among the towers at any given time.
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the
larger one. There are other variations of the puzzle where the number of disks increase, but the tower
count remains the same.
UNIT-III Page 15
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Rules
Only one disk can be moved among the towers at any given time.
Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation shows that a
puzzle with 3 disks has taken 23 - 1 = 7 steps.
UNIT-III Page 16