Stack
Stack
top 4 50
3 40
2 30
1 20
0 10
CD-BOX
IDLY PLATES
Operations on a Stack
Initial Top=-1
SIZE=5
4
TOP -1
Stack
Push (To insert an element on to the stack)
Algorithm
• Step 1 - Check whether stack is FULL.
(top == SIZE-1)
• Step 2 - If it is FULL, then display
"Stack is FULL!!! Insertion is not
possible!!!“ and terminate the function.
Push()
Push(60)
TOP=4 ,i.e. Stack is FULL
top 4 50
3 40
2 30
1 20
0 10
stack
Push()
• Step 3 - If it is NOT FULL, then
increment top value by one (top++)
and set stack[top] to value 4
(stack[top] = value). 3
1
TOP=-1 ,i.e. Stack is empty
0
TOP -1
Push (To insert an element on to the stack)
If (top == SIZE-1)
-1==4 1==4 2==4 3==4
0==4
4 4
4 4 4 top 50
40
3 3
3 3 3 40
top
2 30 2 30
2 2
top 2
30
20 20 20
1 1
top1 20 1 1
0 10 10 10 0 10
top 0 0 0
10
Push(40) Push(50)
Push(10) Push(20) Push(30)
Push()
Push(60) If (top == SIZE-1)
TOP=4 ,i.e. Stack is FULL Insertion not possible
top 4 50
3 40
2 30
1 20
0 10
Pop (To delete an element from the stack) Algorithm
TOP -1
Pop()
TOP=4 ,i.e. Stack is not empty
Step 3 - If it is NOT EMPTY, then
delete stack[top] and
50
decrement top value by one (top--). top 4
40
3
30
2
20
1
10
0
Pop (To delete an element from the stack)
If (top == -1)
4==-1 3==-1 2==-1 1==-1 0==-1
4 50 4 4
top 4
4
40 40
top 3
3 3 3
3
30 30 2
2 2 2 30
top 2
20 20 20
20 top 1
1 1 1
1
10 10 10 0 10
0 0 0 10
0 top
Pop(30) Pop(20)
Pop(50) Pop(40)
Pop (To delete an element from the stack)
4
4
3
3 Stack is empty
2
2
1
1
0
0
Top=-1 TOP -1
Pop(10)
Display (To display elements of the stack)
top 4 50
3 40
2 30
1 20
0 10