Stack Using Array

#include<iostream.h>
#include<conio.h>
class stack
 {
private:
  int s[10],top,n,x,i;
public:
 stack()
  { 
cout<<"Enter the No.of elements:";
cin>>n;
top=0;
  }
 void push()
       {
cout<<"Enter the element:";
cin>>x;
if(top>=n)
            {
    cout<<"Stack is full";
 }
     else
                 {
         top=top+1;
               s[top]=x;
            }
      }
int pop()
      {
          if(top==0)
           {
              cout<<"Stack is empty";
                   return(0);
                }
         else
     {
                   int y;
                   y=s[top];
                   top=top-1;
                  return(y);
                }
           }
void display()
           {
     for(i=top;i>0;i--)
     cout<<s[i]<<endl;
      }
  };


 void main()
  {
     clrscr();
     stack s;
int choice;
     do
        {
           cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
           cout<<"3.Display"<<endl;
           cout<<"4.Exit"<<endl;
           cout<<"Enter the choice:";
           cin>>choice;
           if(choice==1)
            {
               s.push();
               cout<<"\nThe element after push operation are:"<<endl;
               s.display();
            }
           if(choice==2)
            {
               int p;
               p=s.pop();
               cout<<"\nThe poped element is:"<<p;
               cout<<"\nThe element after pop operation are:"<<endl;
               s.display();
            }
           if(choice==3)
            {
               cout<<"The stack elements are"<<endl;
               s.display();
            }
        }while(choice!=4);
     getch();
 }

No comments:

Related Posts with Thumbnails