C++ implementation of top varying stack

C++ implementation of top varying stack

#include <iostream.h>
#include <conio.h>
#define size 10
using namespace std;
class stack
{
    int tos;
    int item[size];
    public:
        stack(){tos=0;}
        void push();
        void pop();
        void display();
};
void stack::push()
{
    int data;
    if(tos==size) {cout<<"Stackis full";}
    else
    {
        cin>>data;
        item[tos]=data;
        tos++;
    }
}
void stack::pop()
{
    if(tos==0) {cout<<"Stackis empty";}
    else
    {
        tos--;
        cout<<"The item is"<<item[tos];
    }
}
void stack::display()
{
    for(int i=0;i<tos;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...