Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Push and Pop Operation in C++ - Explain with example programs


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

#inclde

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)

  {

    clrscr ();

    cout
    cout
    cout
    cout
    cout
    cin>>opt;

   switch (opt)

  {

   case 1:

          cout
          cin>>n;

          st .push(n);

          break;

   case 2:

          st .push();

          break;

   case 3:

          cout
          st .display();

          break;

   case 4:

          loop = 0;

          break;

   default:

          cout
          }

   }

}

// member function to push value into stack

void stack :: push (int x)

  {

   if (top == 4)

   {

      cout
      getch();

      return;

   }

    top = top + 1;

    stk [top] = x;

 }

// member function to pop value from stack

void stack :: pop (void)

  {

   if (top == -1)

   {

      cout
      getch();

      return;

   }

    val = stk [top];

    stk [top] = NULL;

    top = top-1;

    cout
    getch();

 }

// member function to display values of stack

void stack :: display (void)

  {

   if (top == -1)

   {

      cout
      getch();

      return;

   }

    for (int x = top; x>=0; x--)

    cout
    getch ();

 }


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

#include

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;

   clrscr();

   cout
   cin>>n;

   obj .bin (n);

   getch ();

 }

   // 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
      for (int i = top; i>=0; i--)

          cout
      cout
}



This post first appeared on Programming Explain, please read the originial post: here

Share the post

Push and Pop Operation in C++ - Explain with example programs

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×