Professional Documents
Culture Documents
Aman Kumar
UpGrad
Stack :Everyday Examples
▪ Stack of books
▪ Stack of plates
▪ Stack of neatly folded shirts
▪ Stack of pebbles
▪ Stack of bangles
▪ Stack of tortillas
What’s
Common?
What is common characteristic among the items in collection?
▪ Pop
– Remove the element at the top of the stack
top
B
top top
A A A
top
To illustrate the workings of a stack, consider the following
sequence of operations:
▪ push(“abe”)
▪ push(“amy”)
▪ push(“andy”)
▪ pop()
▪ push(“anne”)
▪ push(“alfred”)
▪ pop()
▪ pop()
Push Operation:
▪ Before inserting the value, we must first check if TOP = MAX–1
First check if TOP=MAX–1. If the condition is false, then increment the value of
TOP
and store the new element at the position given by stack[TOP].
First check if TOP= NULL. If the condition is false, then decrement the value of
TOP
Before deleting the value, we must first In case TOP!=NULL, then we will delete the
check if TOP=NULL, because if it is true, then node pointed by TOP, and make TOP
it means that the stack is empty and no point to the second element of the
more deletions can be done. linked stack.