C++ Push and Pop Operation

- February 06, 2018
Diagram of push and pop operation

When a data item is added or inserted into a stack at its top position, the operation is called push operation. 
Before pushing a data item into the stack, it is ensured that there is an available location in the stack to store the data item. If there is no room, no further value is pushed on to the stack. This situation or condition is called “stack overflow”. In this case, “stack overflow” message is sent to the user.

Procedure Algorithm - Pushing Item

The following procedure sub-algorithm push a data item X into a stack "STK" which can store N elements.
PUSH (STK, X)
1. [Check stack whether it has space or not]
 IF TOP >= N THEN
 PRINT "Stack overflow"
 RETURN
 END IF
2. [Insert value X into stack]
  TOP = TOP + 1
   STK [TOP] = X
3. EXIT


What is Pop operation

When a data item is removed from the stack, the operatton is called pap operation. Before removing a data item from a stack, it is ensured that there is at least one item in the stack If there is no data item, the stack is declared as "empty" This situation or condition ts also called "‘istack underflow"

Procedure Algorithm - Popping Item

POP (STK, TOP)
1. [Check stack if it is empty]
 IF TOP >= 0 THEN
 PRINT "Stack is empty"
 RETURN
 END IF
2. [Remove data item from stack]
  STK [TOP] = NULL
  TOP = TOP - 1
 
3. RETURN

C++ Example Programs of Push & Pop operations


Example program 1: This source code of the program push and pop values in a stack
#include<iostream.h>
using namespace std;
class stack
{
  private:
   int top;
   int stk[5];
  public:
   stack (void) { top = -1; }
   // Prototypes of the functions of the class
   void push (int);
   void pop (void);
   void display (void);
 };
void main (void);
{
   stack st;
   int n, opt, loop = 1;
   while (loop)
  {
    cout<<"1 - Pushing stack"<<endl;
    cout<<"2 - Poping stack"<<endl;
    cout<<"3 - Display stack"<<endl;
    cout<<"4 - Exit stack"<<endl;
    cout<<"Enter your option [1-4] ?";
    cin>>opt;
   switch (opt)
  {
   case 1:
          cout<<"Enter value to insert? ";
          cin>>n;
          st .push(n);
          break;
   case 2:
          st .push();
          break;
   case 3:
          cout<<"Values in stack\n";
          st .display();
          break;
   case 4:
          loop = 0;
          break;
   default:
          cout<<"Invalid option";
          }
   }
}

// member function to push value into stack
void stack :: push (int x)
  {
   if (top == 4)
   {
      cout<<"Stack overflow";
      return 0;
      return;
   }
    top = top + 1;
    stk [top] = x;
 }

// member function to pop value from stack
void stack :: pop (void)
  {
   if (top == -1)
   {
      cout<<"Stack is empty";
      return;
   }
    val = stk [top];
    stk [top] = NULL;
    top = top-1;
    cout<<"Value "<<val<<" is removed "<<endl;
    getch();
 }
// member function to display values of stack
void stack :: display (void)
  {
   if (top == -1)
   {
      cout<<"Stack overflow";
      getch();
      return;
   }
    for (int x = top; x>=0; x--)
    cout<<"stk[x] <<endl;
    return 0;
 }
Example program 2: This source code of program convert a decimal integer value into binary and print result on the screen using stack technique to store remainder of each step of division and pop the values from stack.
#include<iostream>
using namespace std;
class stack
 {
   private:
      int top;
      int stk[5];
   public:
      stack (void) { top = -1;}
      //Functions of the class
      void bin (int x);
      void display (void);
 };
void main (void)
 {
   stack obj;
   int n;
   cout<<"Enter an integer value?";
   cin>>n;
   obj .bin (n);
    return 0;
 }
   // member function to convert numbers into binary and push to stack
void stack :: bin (int x)
 {
   int r;
   while (x>0)
  {
      r = x%2;
      x = x/2;
      top = top+1;
      stk [top] = r;
   }
}
   // member function to pop values from stack and display in binary format
void stack :: display (void)
  {
      cout<<"Value in binary = (";
      for (int i = top; i>=0; i--)
          cout<<stk[i];
      cout<<") 2";
}