C++ implementation of fixed top stack

C++ implementation of fixed top stack

#include<iostream.h>
#include<conio.h>
#definesize 10
class stack
{
    int tos;
    int item[size];
    int pos;
    public:
        stack(){tos=0;pos=0;}
        void push();
        void pop();
        void display();
};
void stack::push()
{
    int data;
    if(pos==size) {cout<<"Stackis full";}
    else
    {
        cin>>data;
        for(int i=pos;i>0;i--)
        {
            item[i]=item[i-1];
        }
        pos++;
        item[tos]=data;
    }
}
void stack::pop()
{
    if(pos==0) {cout<<"Stackis empty";}
    else
    {
        cout<<"The item is"<<item[tos];
        for(int i=0;i<pos;i++)
        {
            item[i]=item[i+1];
        }
        pos--;
    }
}
void stack::display()
{
    for(int i=0;i<pos;i++)
        cout<<item[i]<<'\t';
}

void main()
{
    int ch;
    stack stk;
    do{
    clrscr();
    cout<<"Menu"<<endl<<"1.Push"<<endl<<"2.Pop"<<endl<<"3.Display"<<endl;
    cout<<"Enter the choice";
    cin>>ch;
    switch(ch)
    {
        case 1:
            stk.push();
            break;
        case 2:
            stk.pop();
            break;
        case 3:
            stk.display();
            break;
    }
        getch();
    }while(ch<=3);
}


No comments :

Post a Comment

Your Comment and Question will help to make this blog better...