Site icon DataFlair

Stack in DSA using C++

Program 1

#include<iostream>
#define MAXSIZE 10
#define clrscr() system("cls")
using namespace std;
class MyStack
{
      private:
       int stack[MAXSIZE];
       int top;
       public:
       MyStack()
       {
            top=-1;
       }
       int push();
       int pop();
       int display();
};
int MyStack::push()
{
       int n;
       if(top==MAXSIZE-1)
           cout<<"\nStack is overflow";
        else
        {
             cout<<"\nEnter an element for push";
             cin>>n;
             top++;
             stack[top]=n;
        } 
}
int MyStack::display()
{    int i;
        if(top==-1)
           cout<<"\nStack is Empty";
        else
        {
            cout<<"\n Stack elements :";
           for(i=top;i>=0;i--) 
             cout<<"\n"<<stack[i];
        }   
}

int MyStack::pop()
{
     int n;
        if(top==-1)
           cout<<"\nStack is Empty";
        else
        {
              n=stack[top];
              top--;
            cout<<"\n Poped element is :"<<n;  
        }
}
int main()
{
   clrscr();
   int choice;
   MyStack M;

 do
 {
   cout<<"\n--------------Stack Menu---------------------";
   cout<<"\n1. PUSH";
   cout<<"\n2. POP";
   cout<<"\n3. DISPLAY";
   cout<<"\n4.EXIT";
   cout<<"\n------------------------------------------------"; 
   cout<<"\nEnter your choice";
   cin>>choice;
   switch(choice)
   {
      case 1:M.push();break;
      case 2:M.pop();break;
      case 3:M.display();break;
      case 4:break;
      default:cout<<"\nInvalid choice";  
   }
 }while(choice!=4);   
return 0;
}

 

Exit mobile version