Stack New
Stack New
Patel
Stack:-
A stack is an ordered collection of items into which new items may be inserted & from which items
may be deleted at one end called “top of stack”.
Stack is a dynamic constantly changing object. Stack is also called LIFO because whichever item is
inserted last will be deleted first.
The most and least accessible elements in a stack are known as the top and bottom of the stack
respectively.
The new item may be put on the top of the stack or item may be removed from the top of the stack
The above two changes which can be made to a stack are given special names when an item is added
to a stack it is pushed on the stack and when an item is removed it is popped from the stack.
D Top
C
The value is
B B Top popped & top
is decremented
A A
Example:-
An example of stack is the “in” tray of a busy executive. The files pile up in the tray and
whenever the executive has time to clear the files, he/she takes it off from the top. That is, files
are added at the top and removed from the top. Therefore stacks are sometimes referred to
as LIFO Structure.
Considered a stack of plates placed on the counter in the restaurant during the dinner time,
customers take plate from the top of the stack and waiter puts washed plate on the top of the
stack.
1) PUSH Operation:
It means to insert the elements in the stack. In executing the procedure push, one must first
test whether there is the room in stack for the new item, if not, then the condition is known as
overflow.
Algorithm:
Push (Item,n,value)
value=item to push, n=size of stack
2) POP Operation:
It means to take elements from stack. In executing the procedure pop, one must first test
whether there is an element in the stack to be deleted. If not, then condition is known as
underflow.
Algorithm:
3) PEEP Operation:
If only information store at some location in a stack is needed, then this operation can be
performed. We move logically to desired location & then fetch the information associated with
that location. To access desire elements from top of the stack is known as Peep Operation. For
example suppose we have to access 3 element from top than it is 2=c, ith=desired element=3 and
top=4.the formula is :
E
Top = top-i+1
D =4-3+1
= 1+1
C 2=C
=2
B 2=C
Algorithm:
Peep(i)
4) UPDATE Operation
Algorithm: update(i,value)
Stack Application:-
1) Polish expression
2) Recursive
3) Stack machines, certain computers perform stack operation at the hardware or machine level.
Polish Expression:
The process of writing the operators of an expression either before the operand or after the
operand is called Polish Notation.
When operators are written after the operands, it is called Reverse Polish Notation.
“Pre”,”Post” &”In” refer to the relative position of the operator with respect to the two operands.
1) Infix notation:-
= a+(*BC)
=+a*bc
In this notation the order of operations to be performed is determined by the use of
parenthesis or by the use of some operator precedence conventions.
The levels of precedence are (highest to lowest):
Priority Operator
1 (
2 ^,$,
3 %
4 / or * (left to right)
5 + or –(left to right)
6 )
During conversion of an infix notation we must begin with inner parenthesis and then
precede towards, the outside expression i.e. the operations with higher precedence are
converted first.
2) Prefix notation:-
BMU- BMCCA Page 4 of 14
Data Structures UNIT : STACK By : Sneha T. Patel
Example: z + (y * x - (w / v ^ u) *t) *s
V *)*)^ Stuv
/ *)*)/ stuv^
w *)*)/ stuv^w
( *)* stuv^w/
- *)- stuv^w/*
X *)- stuv^w/*x
* *)-* stuv^w/*x
Y *)-* stuv^w/*xy
( * stuv^w/*xy*-
+ + stuv^w/*xy*-*
Z + stuv^w/*xy*-*z
(stack is empty) stuv^w/*xy*-*z+
Example: (a + b * c ^ d / e) / (f * g - h)
= (A+B)*(C-D)
Example: ab+cd*-
Input Stack
A A
B a,b
+ +ab
C +ab,c
D +ab,c,d
* +ab,*cd
- -+ab*cd
Example: 9 5 + 7 4 - *
Input Stack
Two values are popped;
9 9 summation is done &
pushed again in stack
5 9,5
+ 14 Two values are popped
7 14,7 from stack; subtraction
is done & result pushed
4 14,7,4 again in stack
- 14,3
Two values are popped;
* 42 multiplied & result
pushed into stack
Example: ab+cd-*
Input Stack
A A
B a,b
+ a+b
C a+b,c
D a+b,c,d
* a+b,c*d
- a+b-c*d
Advantage of Recursion:
Step-1: [Preface]
Save the argument & return address.
Step-2: [Body of function]
If a base criterion is completed go to step-3.
Else perform partial computation & go to step-1.
Step-3: [Last Section]
Restore the most recently saved parameters & return address.
Algorithm:
Start
Preface
Local variables save
formal parameters
return address value on
stack
Intermediate Intermediate
Test? computation
Level
Main
Function
Body Not in
Final intermediate
computation level
Function call
itself
Restore formal
parameters, local
variable, return address.
Last
Section
End
➢ Tower Of Hanoi:-
The tower of Hanoi problem is very popular example in recursion and in stack data structure .The
basic problem is that you have three pole source, aux and destination and in the source pole there
are N different sized disk. Now you have to move these disks from source to destination using aux
pole (The object is to move all the disks over to another pole) but the Rules are:
1) Only one disk may be moved at a time
2) Each move consists of taking the upper disk from one of the pole and sliding it onto another
pole, on top of the other disks that may already be present on that pole.
3) you cannot place a larger disk onto a smaller disk
#include<stdio.h>
#include<conio.h>
#include<math.h>
void hanoi(int x, char from,char to,char aux)
{
if(x==1)
{
printf("Move Disk [%d] From= [%c] to= [%c] ==> [%c]\n",x,from,to,aux);
}
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk [%d] From= [%c] to= [%c] ==> [%c]\n",x,from,to,aux);
hanoi(x-1,aux,to,from);
}
}
void main()
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'L','C','R');
getch();
}
BMU- BMCCA Page 12 of 14
Data Structures UNIT : STACK By : Sneha T. Patel