Data Structures & Algorithms
Unit -3
Stack Using Array
Pankaj Chandra
Assistant Professor
IT GGV
Stack using Array
A stack data structure can be implemented using a one-
dimensional array.
But stack implemented using array stores only a fixed
number of data values.
This implementation is very simple.
Just define a one dimensional array of specific size and insert
or delete the values into that array by using LIFO
principle with the help of a variable called 'top'.
Stack using Array
Initially, the top is set to 0.
Whenever we want to insert a value into the stack,
increment the top value by one and then insert.
Whenever we want to delete a value from the stack,
then delete the top value and decrement the top
value by one.
Stack Operations using Array - PUSH
In a stack, push is a function used to insert an element into
the stack. In a stack, the new element is always inserted
at top position. We can use the following steps to push an
element on to the stack.
Algorithm
Step 1 - Check whether stack is FULL. If (Top == Size of Stack) then
display "Stack is FULL, Insertion is not possible" and Exit.
Step 2 - If it is NOT FULL, then increment Top value by setting
Top=Top+1
Step 3 - Set Stack[top] = ITEM
Step 4 - Exit
Stack Operations using Array - PUSH
Example – Stack is Linear Array Stack of
Size 5.
Initially Top==0 means Stack is empty.
Stack Operations using Array - PUSH
Insert A
Set Top=Top+1 = 1
Set Stack[Top]=A so Stack[1]=A
Stack Operations using Array - PUSH
Insert B
Set Top=Top+1 = 2
Set Stack[Top]=B so Stack[2]=B
Stack Operations using Array - PUSH
Insert C
Set Top=Top+1 = 3
Set Stack[Top]=C so Stack[3]=C
Stack Operations using Array - PUSH
Insert A
Set Top=Top+1 = 4
Set Stack[Top]=D so Stack[4]=D
Stack Operations using Array - PUSH
Insert E
Set Top=Top+1 = 5
Set Stack[Top]=E so Stack[5]=E
Now Top==5 (Size of Array) means Stack is Full.
Stack Operations using Array - POP
In a stack, pop is a function used to delete an element from
the stack. In a stack, the element is always deleted
from top position. We can use the following steps to pop an
element from the stack.
Algorithm
Step 1 - Check whether stack is EMPTY. If (Top == 0) then
display "Stack is EMPTY ,deletion is not possible!!!" and exit.
Step 2 - If it is NOT EMPTY, then delete element from
Top and set ITEM=Stack[Top].
Step 3 - Decrement top value by setting Top=Top-1.
Step 4 – Exit.
Stack Operations using Array - POP
Example – Given Array Stack of Size 5
Top==5 (Size of Stack) means array is full.
Stack Operations using Array - POP
Delete E
Set ITEM=Stack[Top]=Stack[5]=E
Set Top=Top-1 = 4
Stack Operations using Array - POP
Delete D
Set ITEM=Stack[Top]=Stack[4]=D
Set Top=Top-1 = 3
Stack Operations using Array - POP
Delete C
Set ITEM=Stack[Top]=Stack[3]=C
Set Top=Top-1 = 2
Stack Operations using Array - POP
Delete B
Set ITEM=Stack[Top]=Stack[1]=B
Set Top=Top-1 = 1
Stack Operations using Array - POP
Delete A
Set ITEM=Stack[Top]=Stack[1]=A
Set Top=Top-1 = 0
Now Top==0(Null) means Stack is empty
Thank You