Stacks
Stacks
What is a stack?
• Stores a set of elements in a particular order
• Stack principle: LAST IN FIRST OUT= LIFO
i.e the last element inserted is the first one to be
removed
• Example
– Stack of plates
– Stack of coins
stack data structure
Stacks often are drawn vertically:
push pop
3
Basic Stack Operations
• Push:
– Adds an item at the top of the stack.
– If the stack is full, no more data can be added to
the stack and the stack is said to be in the
overflow state.
– Diagram
Algorithm to push data into stack
Algorithm push(struct stack *s , int item)
To push the data into the stack using array implementation
pre: struct stack *s : pointer to the stack structure
item : data to be pushed in the stack
post: push the data into the array
1. if s->top = ARR - 1
display "STACK IS FULL"
return
2. [increment top by 1]
s->top =s->top+1
3. [insert data into the stack]
s->a[s->top]=item
Basic Stack Operations
• Pop:
– Removes an item at the top of the stack.
– When the last item is deleted , the stack must be
set to empty state. If pop() is called when the
stack is empty, it is said to be in the underflow
state.
– Diagram
Algorithm to pop data from the stack
Algorithm int pop(struct stack *s)
pre : To pop the data from the stack using array
implementation
struct stack *s : pointer to the stack structure
post : return the popped data to the main()
return data
1. [declare a variable]
int data
2. if s->top =-1
return NULL
3. [remove data from the top of the stack]
data=s->a[s->top]
s->top—
count--
return data
Basic Stack Operations
• Stack top or peep
– It returns the data at the top of the stack but does
not delete it. i.e it only reads the data.
– if the stack is empty, stack top can result in
underflow state.
Algorithm to peep/read data from the stack
Algorithm int peep(struct stack *s)
To peep/read the data from the stack using array
implementation
pre: struct stack *s : pointer to the stack structure
post: return the peeped data to the main()
Return data
Refer to pop() algorithm and make the necessary changes
Algorithm to display the stack
C. operator=character
D. set value = calculate
(oper1,operator,oper2)
E. pushStack(stack ,value)
endif
end loop
Algorithm for converting infix to prefix
Algorithm infix_to_prefix(s[]) add operator to the stack1
1. Get the infix expression s. go to step 8
2. set i=0 else
3. Set top1=top2=-1, indicating stacks p= pop the operator
are empty.
from the stack1
4. If s[i]=‘(‘ , push it in stack1, go to
step 8 O2=pop the operand from
5. If s[i]=operand , push it in stack2, go
stack2
to step 8 O1=pop the operand from
6. If s[i]=operator stack2
stack1 is empty or stack form the prefix expr
p,O1,O2
top elements has less push operator in stack2 and
priority as compared o go to step 8
s[i], End if
x Cont…
Algorithm for converting infix to prefix
7. If s[i]=‘)’ then step 7A
A) p=pop the operator B)
from stack1 remove “(“
O2=pop the go to step8
operand from 8. Increment i
stack2 9. If s[i] <>’\0’ then go to step 4
O1=pop the 10. Everytime pop one operator
from stack1, pop 2 operands
operand from
from stack 2 , form the prefix
stack2 expr ,O1, O2 , push in stack2
form the prefix expr and repeat till stack becomes
p,O1,O2 empty.
push in stack2 and go 11. Pop operand from stack2 and
to print it as expression
12. stop
More Applications of stacks
• Parenthesis matching
• Towers of Hanoi
• Rearranging Railroad cars
• Switch box routing
• Rat in a maze