Basic Push Operation : Insert Element Onto Stack


Push Operation On Stack

Tags : Push Operation , Data Structure Push Operation : Before Starting with Push Operation , Let us First make it clear , “How Stack Push Operation Works ?


Push Operation :
  1. Push Refers as “Adding Elements onto Stack“.
  2. Push Operation carried out in following 2 steps -
    • First Increment Variable “top so that it now refers to next memory location.
    • Secondly Add Element Onto Stack by accessing array.
  3. Main Function Should ensure that stack is not full before making call to push() in order to prevent “Stack Overflow

Push Function

void push(stack *s,int num)
{
s->top = s->top + 1;
s->data[s->top] = num;
}

What is Stack Overflow ?

  1. Pushing element onto stack which is already filled is refer as “Stack Overflow“.
  2. Using array representation of stack ,variable “MAX” is used to keep track of “Number of Maximum Elements

How to Check Whether Stack is Full or not ?

int full(stack *s)
{
if(s->top == MAX-1) //Stack is Full
return(1);
else
return(0);
}