Page 128 - DCAP407_DATA_STRUCTURE
P. 128
Unit 7: Stacks
operation as it is the last item recently added. After adding 52, the stack is full or it is in stack overflow
condition. No more items can be added in this stack.
The syntax used for Push operation is PUSH (stack, item).
Algorithm to Implement Push Operation on Stack
PUSH (STACK, n, top, item) /* n = size of stack*/
if (top = n) then STACK_FULL; /* checks for stack overflow */
else
{ top = top+1; /* increases the top by 1 */
STACK [top] = item ;} /* inserts item in the new top position */
end PUSH
7.2.2 Pop Operation
The procedure to delete an element from the top of the stack is called pop operation. After every pop
operation, the ‘Top’ is decremented by one. When there is no element in the stack, the status of the stack
is called empty stack or stack underflow. The pop operation cannot be performed when it is in stack
underflow condition.
Figure 7.3: Pop Operation
Figure 7.3 shows the pop operation in stack. The stack initially has three items, 25, 37 and 18. The ‘Top’
points to the last item, 18. After the pop operation, item 18 is deleted from stack. Now, the ‘Top’ points
to 37.
The syntax used for Pop operation is POP (stack).
Algorithm to Implement Pop Operation in a Stack
POP (STACK, top, item)
if (top = 0) then STACK_EMPTY; /* check for stack underflow*/
else { item = STACK [top]; /* remove top element*/
top = top – 1; /* decrement stack top*/
}
end POP
LOVELY PROFESSIONAL UNIVERSITY 121