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 ]
- Store Topmost Element in another Variable.
- Decrement Top by 1
- Return Topmost Element .
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"
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); }
0 Comments:
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email