Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

Stacks->Array Implementation->Pop operation

POP is the synonym for delete when it comes to Stack. So, if you're taking an array as the stack, remember that you'll return an error message, "Stack underflow", if an attempt is made to Pop in an empty Stack. OK.

Algorithm:-
Step-1: If the Stack is empty then give the alert "Stack underflow" and quit; or else go to step-2
Step-2: a) Hold the value for the element pointed by the TOP
            b) Put a NULL value instead
            c) Decrement the TOP by 1

C implementation:-
static int stack[uplimit];
int top=-1;
..
..
main()
{
..
..
poped_val = pop();
..
poped_val = pop();
..
}

int push()
{
int del_val = 0;
if(top == -1) printf("Stack underflow"); /*step-1*/
  else
     {
      del_val = stack[top];  /*step-2*/
      stack[top] = NULL;
      top = top -1;
     } 
return(del_val);
}

Note:- Step-2:(b) signifies that the respective element has been deleted. In array implementation, I have taken TOP = -1 to signify the empty stack. This simplifies your implementation.

Related Operations:
Concept
Array Implementation - Push() and Pop()
Linked List Implementation - Push() and Pop()
Application of Stacks

Index || Doubts / Clarifications || Related Topics || Web Links