Stacks
Stacks
Learning Material
Stacks
Stack is a linear data structure.
Stack is an ordered collection of homogeneous data elements, where insertion and deletion
operations takes place at only one end called TOP.
The insertion operation is termed as PUSH and deletion operation is termed as POP
operation.
The PUSH and POP operations are performed at TOP of the stack.
An element in a stack is termed as ITEM.
push pop
Item 5 Top
Item 4
Item 3
Item 2
Item 1
Schematic diagram of a stack
The maximum number of elements that stack can accommodate is termed as SIZE of the
stack.
Stack follows LIFO principle. i.e. Last In First Out.
Representation of stack
There are two ways of representation of a stack.
1. Array representation of a stack.
2. Linked List representation of a stack.
l + i -1 Item i Top
l+2 Item 3
l+1 Item 2
l Item 1 Bottom
Stack overflow
Trying to PUSH an item into full stack is known as stack overflow.
Stack overflow condition is top >= size-1
Stack underflow
Trying to POP an item from empty stack is known as Stack underflow.
Stack underflow condition is top < 0 or top = = -1
Operations on Stack
PUSH : To insert element in to stack
POP : To delete element from stack
Status : To know present status of the stack
Algorithm Stack_POP( )
Input: Stack with some elements.
Output: item deleted at top most end.
1. if(top < 0)
a) print "stack is empty not possible to pop"
2. else
a) item = s[top]
b) top = top - 1
c) print(item)
3. end if
End Stack_POP
Algorithm Stack_Status( )
Input: Stack with some elements.
Output: Status of stack. i.e. Stack is empty or not, full or not, top most element in Stack.
1. if(top > = size-1)
a) print "stack is full"
2. else if(top < 0)
a) print "stack is empty"
3. else
a) print "top most item in stack is" s[top]
4. end if
End Stack_Status
header
X 45 i i-1 2 1 X
top
X X
2 1
X X
After PUSH
top
1. The link part of the new node is re placed with address of the previous top most node.
2. The link part of the header node is replaced with address of the new node.
3. Now the new node becomes top most node. So top is points to new node.
Algorithm Stack_POP_LL( )
Input: Stack with some elements.
Output: item deleted at top most end
1. if( headerlink = = NULL)
a) print "Stack is empty, unable to perform POP operation"
2. else
a) headerlink = toplink
b) item = topdata
c) top = headerlink
End Stack_POP_LL
header N4 N3 N2 N1
X X
2
top top
Before POP
header N3 N2 N1
X X
1. Link part of the header node is replaced with the address of the second node in the list.
2. After deletion of top most node from list, the second node becomes the top most node in the
list. So top points to the second node.
Algorithm Stack_Status_LL( )
Input: Stack with some elements.
Output: Status of stack. i.e. Stack is empty or not, top most element in Stack.
1. if( headerlink = = NULL || top = = NULL)
a) print "Stack is empty"
2. else
a) print "Element present at top of stack is" topdata
3. end if
End Stack_Status_LL
Applications of stack
1. Infix to postfix conversion
2. Evaluation of postfix expression
3. Reversing list
([(A - {B+C})*D]$E+F)
( [ ( A - BC+ ) * D ] $ E + F )
( [ABC+- * D ] $ E + F )
A * B- (C + D) + E
Ex: Convert the given infix expression to postfix expression using STACK
(A+B*(C-D))/E
( (
A ( A
+ (+ A
B (+ AB
* (+* AB
( (+*( AB
C (+*( ABC
- (+*(- ABC
D (+*(- ABCD
) (+* ABCD-
) ABCD-*+
/ / ABCD-*+
E / ABCD-*+E
End ABCD-*+E/
Algorithm PostfixExpressionEvaluation
Input: Postfix expression
Output: Result of Expression
1. Repeat the following steps while reading the postfix expression.
a) if the read symbol is operand, then push the value onto stack.
b) if the read symbol is operator then pop the top most two items of the stack and
apply the operator on them, and then push back the result to the stack.
2. Finally stack has only one item, after completion of reading the postfix expression. That
item is the result of expression.
End PostfixExpressionEvaluation