Monday, 21 August 2017

STACK AS AN ARRAY

#include<iostream.h>
#include<process.h>
const int size=5
class stack
{           int arr[size];
            int top;             //top will point to the last item pushed onto the stack
    public:
            stack()           //constructor to create an empty stack
           { top=-1; }      // top=-1 indicate that no item is present in the array
            void push(int item)
            {           if(top==size-1)
                        cout<<”stack is full, given item cannot be added”;
                        else
                        {           top++;
                                    arr[top]=item;
                        }
           }
           void  pop()
           { if (top== -1)
                 cout<<”Stack is empty “;
             else
                 { cout<<arr[top];
                 top – -; }
           }
           void display()
           {
            for(int i=top;i>=0;i–)
                        cout<<arr[top];
           }
};
void main()
{           stack s1;
            int ch,data;
            while(1)
            {
           cout<<”1.push”<<”\n 2.pop”<<”\n3.display”<<”\n 4.exit”;
            cout<<”Enter choice :”;
            cin>>ch;
            if (ch==1)
            {           cout<<”enter item to be pushed in the stack”;
                        cin>>data;
                        s1.push(data);
            }
            else if (ch == 2)
                        {  s1.pop();  }
            else if (ch==3)
                        {  s1.display();  }
            else
                        exit(-1);
            } }

No comments:

Post a Comment