Stack
Introduction
■
A stack is a non primitive linear data
structure.
■
It is an ordered list in which addition of
Why stack is called LIFO?
■
All the deletion and insertion in a stack
is done from top of the stack.
■
The last added element will be the first
to be removed from the stack.
■
Example-plates in marriage party or
Stages of stack top during insertion and
deletion
20 20 13 20 13 68 20 13 68 20 13 20 Stack Empty First element second element0 0 0 1 2 1 Third element 0 2 1 1 0
Stack implementation
■
Static
■ Using array
■ Size of stack has to be declared during program
design. Problem in insertion and deletion.
■
Dynamic
Stack Operations
■
Push
■ Process of adding a new element to the top
of stack. Top is increment after push
operation. In case of stack full called stack overflow condition.
■
Pop
■ Process of deleting an element from the
Stack as an abstract data type
■ Stack can also be defined as abstract data
types.
■ A stack of elements of any particular type is a
finite sequence of elements of that type together with the following operations:
■ Initialize stack to be empty
■ Determine whether stack is empty or not ■ Determine if stack is full or not
■ If stack is not full, then add a new node at the
end
■ If stack is not empty, then delete the node at its
Algorithm for Push operation
Push (stack[maxsize], item)
1. Set top=-1
2. Repeat steps 3to 5 until top<maxsize-1 3. Read item
4. Set top=top+1
Algorithm for Pop operation
Pop (stack[maxsize], item)
1.
Repeat steps 2to 4 until top>=0
2.
Set item =stack[top]
3.
Set top=top-1
4.
Print, no deleted is, item
Drawbacks of sequential
representation of stack data structure
■
Finite capacity of the stack
■
Checking for STACK_FULL condition
Linked Stack
■
Linked stack is a linear list of elements
commonly implemented as a singly
Linked Stack
C B A
a b c c b a
Top
Top
Linked stack operations
b
d c b a
c b a
Top
Push ‘d’ into stack
Top Top
a Top
Top
Algorithm for Push operation on a
linked stack
Push_linkstack(Top, Item)
1.
Call GETNODE(x)
/allot a node of the desired str. And
address of the node viz. x/
2
Data(x)=Item
3
Link(x)=Top
Algorithm for Pop operation on a
linked stack
Pop_linkstack(Top, Item)
1. If (top=0)
Call Linkstack_empty /linked stack is empty/
2 Else
Temp=Top
Item=Data(Top) Top=Link(Top)
Applications
■ Stack frame
■ Reversing a string
■ Calculation of postfix expression
Note:-(OPERATOR PRECEDENCE) ^
+,-Infix, Postfix, Prefix
■ Infix
■ Operator is written in-between the operands. A+B
■ Prefix
■ Operator is written before the operands, also
called polish notation. +AB
■ Postfix
■ Operators are written after the operands, also
Rules for infix to postfix conversion
■ Parenthesize the expression starting from left
to right
■ During parenthesizing the expression, the
operands associated with operator having higher precedence are first parenthesized.
■ The sub expression which has been
converted into postfix is to be treated as single operand
■ Once the expression is converted to postfix
infix to postfix
(A+B)*C/D AB+*C/D AB+C*/D AB+C*D/
(A+B)*C/D+E^F/G
AB+*C/D+E^F/G
AB+*C/D+EF^/G
AB+C*/D+EF^/G
AB+C*D/+EF^G/
infix to prefix
A*B+C
*AB+C
+*ABC
A/B^C+D
A/^BC+D
/A^BC+D
+/A^BCD
A/B^C-D
? (A*B+(C/D))-F
(A*B+/CD)-F
(*AB+/CD)-F
+*AB/CD-F
Algorithm for converting infix to
postfix
Postfix(Q, P)
Suppose Q is an arithmetic expression in infix and equivalent postfix expression P
1. Push “(” into stack and add “)” to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until
the stack is empty
3. If an operand is encountered add it to P
4. If a left parenthesis is encountered, push it onto stack 5. If an operator Ø is encountered, then:
(a) add Ø to stack
(b) Repeatedly pop from stack and add P each operator which has the same
Algorithm for converting infix
to postfix
6. If a right parenthesis is encountered then,
(a) Repeatedly pop from stack and add to P each operator until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
[end of IF structure]
[End of step 2 loop]
Algorithm for converting infix
to prefix
1. Reverse the input string
2. Examine the next element in the input
3. If it is operand, add it to the output string 4. If it is closing parenthesis, push it on stack 5. If it is an operator, then
1. If stack is empty, push operation on the stack
2. If the top of stack is closing parenthesis , push operator on
stack
3. Else pop the operator from the stack and add it to output
Algorithm for converting infix
to prefix
6. If it is a opening parenthesis, pop operator
from stack and add them to S until a closing parenthesis is encountered . Pop and discard the closing parenthesis.
7. If there is more input goto step 2
8. If there is no more input, unstack the
remaining operators and add them
(A+B*C)
■ Reverse (C*B+A)
Algorithm for evaluate a
postfix expression
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat steps 3 and 4 for each
element of P until the “)” is encountered.
3. If an operand is encountered , put it on Stack.
4. If an operator Ø is encountered, then
a) Remove the two top elements of stack, where A is the top
element and B is the next to top element
b) Evaluate B Ø A
c) Place the result of (b) back on stack
[end of if structure] [end of step 2 loop]
5. Set value equal to the top element on stack
Evaluate expression ab+c*d
if a=2, b=3, c=4 and d=5
a a
b
5 5
c
20 20
Binary Expression Tree
■
Binary expression tree does not contain
parenthesis the reason that for
evaluating an expression using
Conversion with expression
tree
■
Prefix->Infix
■ Create the expression tree from prefix ■ Run in order traversal on the tree
■
Prefix-> Postfix
Algorithm for creating expression
tree from a prefix expression
1. Reverse the prefix expression
2. Examine the next element in the input
3. If it is operand then
a) Create a leaf node
b) Copy the operand in data part c) Push node’s address on stack
4. If it is an operator, then
a) Create a node
b) Copy the operator in data part
Algorithm for creating expression
tree from a prefix expression
5.
If there is no more input, pop the
address from stack, which is the
-A-BC
CB-A-B C
-A
-Algorithm for creating expression
tree from a postfix expression
1. Examine the next element in the input
2. If it is operand then
a) Create a leaf node
b) Copy the operand in data part c) Push node’s address on stack
3. If it is an operator, then
a) Create a node
b) Copy the operator in data part
Algorithm for creating expression
tree from a postfix expression
4.
If there is more input goto step 1
5.
If there is no more input, pop the
address from stack, which is the
Number System
DTOB(n)
1. Creating empty stack
2. Repeat while(n!=0) pushing remainder
1. Set rem<- n%2
2. Call push( s, rem)
3. n <- n/2
3. Repeat while(! Is empty stack)
1. Rem=pop(S)
2. Display(rem)
Assignment 3
1. Evaluate the expression 5 6 2+*12 4 /- in
tabular form showing stack after every step.
2. Write a program to convert infix to postfix
and then evaluate the postfix expression.
3. Translate by inspection and hand, each infix
expression into its equivalent postfix expression:
1. (A-B)*(D/E)
Assignment 3
4. Consider the following Stack , where STK is
allocated N=6 memory cells:
STK: PPP,QQQ, RRR,SSS,TTT describe the stack as the following operations take place: