0% found this document useful (0 votes)
3 views

Stacks

Uploaded by

Ddevil from hel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Stacks

Uploaded by

Ddevil from hel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

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

 last item in, first item out

 first item in, last item out

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

Algorithm displaystack(struct stack *s)


Pre : struct stack *s : pointer to the stack structure
post : Display the contents of the stack
1.[intialize]
int x=count;
2. Repeat while x>=0
1 . Display s->a[x]
2. x--;
Application of stacks
• Region in memory within which the programs
temporarily store data as they execute.
• Evaluation of expressions:
– Process of writing the operators of an expression
either before their operands or after their operands
is called as “polish notation”
– 3 forms of polish notation
• Prefix form : the operators come before operands
• Postfix form : the operators come after operands
• Infix form: the operator come in between operands
Algorithm for converting infix to postfix
Algorithm infix_to_postfix() A) if operator in the stack A has
1. Push “(“ onto STACK and add “)” to same precedence or higher
the end of expression A. precedence than the
2. Scan expression Q from left to right operator encountered then
and repeat 1 to 6 for each element 1. Repeatedly pop
of Q until the stack is empty. the operators
1. If an operand is encountered , from the STACK A and
add it to Stack B add to Stack B each
operator
2. If a “(“ is encountered push it
B) Add the encountered
onto the stack A.
operator to STACK A.
3. If an operator is encountered 6. If “)” is encountered then
then A) Repeatedly pop from the
STACK A and add to B each
operator(on the top of
STACK) until a “(“ is
encountered.
B) Remove the “(“
7. Exit
Evaluation of postfix expression
Algorithm evaluate_postfix()
3. result =popStack(stack)
1. createStack(stack)
4. Return result
2. Loop(for each character)
End evaluate_postfix
If(character is operand)
1. PushStack(stack,character
)
else
A. set oper2= popStack(stack)
B. set oper1=popStack(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

You might also like