Algorithm for inserting an item into the stack (PUSH)


Step 1: IF TOP = MAX-1
	   PRINT OVERFLOW
	[END OF IF]
Step 2: SET TOP = TOP+1
Step 3: SET STACK[TOP] = VALUE
Step 4: END

Explanation

  • In Step 1, we first check for the OVERFLOW condition.
  • In Step 2, TOP is incremented so that it points to the next location in the array.
  • In Step 3, the value is stored in the stack at the location pointed by TOP.

The function for the stack push operation in C is as follows. Considering stack is declared as int Stack[5], top=-1;

void push( )
{
  int item ;
  if (top < 4)
  {
    printf ("Enter the number") ;
	scanf ("%d", & item) ;
	top = top + 1 ;
	stack [top] = item ;
  }
  else
    printf ("Stack overflow") ;
}