Stack Pop Operation : Remove Element from Stack
Basic Pop Operation On Stack : Data Structure
[ Watch Pop Operation Video On Youtube ] Pop Operation : Removing element from stack and decrementing top is called as “Pop Operation” .
Watch Following Video :
Pop Operation Arguments and Return Type :
- Argument : Variable of Type Stack.
- Return Type : Integer [ Removed Element ]
Steps in Pop Operation :
- Store Topmost Element in another Variable.
- Decrement Top by 1
- Return Topmost Element .
Pre-requisites : Stack Type Definition .: Click Here
Pop Function :
int pop(stack *s)
{
int x;
x = s->data[s->top];
s->top = s->top - 1;
return(x);
}
Note :
- Before Removing or Popping element from stack ensure that stack is not empty.
- Removing element from empty stack leads to problem called “Underflow of Stack“
How to Check Whether Stack is Empty of Not ?
Way 1 : Checking Inside pop()
int pop(stack *s)
{
int x;
if(s->top == -1)
{
printf("Stack is Empty");
exit(0);
}
x = s->data[s->top];
s->top = s->top - 1;
return(x);
}
Way 2 : Calling Independent empty()int empty(stack *s)
{
if(s->top == -1)
return(1);
else
return(0);
}