Search This Blog

Tuesday 2 February 2016

Stack Implementation using C++

// Do not forget to take a look at that both stack-queue class implementation ! label is stackqueue


#include<iostream>

using namespace std;

class stack
{
    int top;
    int a[5];
  
    public:
    void set()
    {
        top=-1;
    }
    int full()
    {
        if(top==4)
            return 1;
        else
            return 0;
    }
  
    int empty()
    {
        if(top==-1)
            return 1;
        else
            return 0;
    }
  
    void push(int i)
    {
        a[++top]=i;
    }
    void peep()
    {
        cout<<"\nTop element is :"<<a[top];
    }
  
    void display()
    {
        int i;
        for(i=top;i>=0;i--)
        {
            cout<<"\n";
            cout<<a[i];
        }
        cout<<"\n";
    }
    void pop()
    {
        top--;
    }
  
};

int main()
{
    stack s;
    int ch,i;
    s.set();
    do{
  
    cout<<"\n1.push\n2.peep\n3.pop\n4.display\n5.enter 0 for exit:";
    cin>>ch;
    switch(ch)
    {
        case 1:if(s.full()){
                cout<<"\nFull";
            }else
            {
        cout<<"\nEnter element:";
        cin>>i;
        s.push(i);
        }
        break;
  
        case 2:s.peep();
        break;
      
        case 3:if(s.empty())
        {
            cout<<"\nstack is empty";
        }else
        s.pop();
        break;
      
        case 4:s.display();
        break;
      
        default:cout<<"\nProper choice plz!";
    }
    }while(ch!=0);
  
    return 0;
}

No comments:

Post a Comment