Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Stack Operations using arrays

C++ program for implementation of Stack (push,pop & display) operation.Stack based on the LIFO (Last In First Out) behavior means, the last element is pushed(insert) inside the stack , is the first element to get pop(deleted).Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. Here we implement the PUSH, POP, DISPLAY stack operations using the array.
Source Code:-


#include<iostream.h>
#include<conio.h>

int stack[20],top=-1; //global declaration
void push();
void pop();
void display();
void main()
{ clrscr();
  int ch;
  cout<<"Program for Stack Operations by-Tarun Rawat\n\n";
  do
  {   cout<<"\n1.Push";
      cout<<"\n2.Pop";
      cout<<"\n3.Display";
      cout<<"\n4.Exit";
      cout<<"\nEnter your choice : ";
      cin>>ch;
      if(ch==1)
    push();
      else if(ch==2)
    pop();
      else if(ch==3)
    display();
      else if(ch==4)
       cout<<"\nEnd Of program";
  }while(ch!=4);
}
//to push elements in a stack
void push()
{
  if(top>9)
    cout<<"\nStack Overflow";
  else
  {   top=top+1;
      cout<<"\nEnter a value : ";
      cin>>stack[top];
  }

}

//to pop/delete stack elements
void pop()
{
  if(top==-1)
   cout<<"\nStack Overflow. No Elements to pop";
  else
  {
      cout<<stack[top]<<" is deleted..";
      top--;
  }
}

//displaying stack elements
void display()
{
    int i;
    if(top==-1)
      cout<<"\nStack underflow. No elements to display..";
    else
    {
    cout<<"\nThe stack elements are : ";
    for(i=0;i<=top;i++)
    {
      cout<<"\t"<<stack[i];
    }
      cout<<"\n";

    }
}

1 comment:

  1. Pop operation in Stack
    This operation removes the data value on top of the stack. Your artile about Stack POP Operation in C++ is helpful for me and all college strudents.

    ReplyDelete

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)