New Android apps UniqueKey

Saturday 21 March 2015

Stack operations


#include<iostream>
using namespace std;
int size;
class stack{
        public:
                int stck[100];
                int top;
                int i;
                int element;


                void push(void)
                {
                        cout<<"\nenter the element to push into th stack\n";
                        cin>>element;
                        stck[++top]=element;

                }
                void display(void)
                {
                        for(i=top;i>=0;i--)
                        {
                        if(i==top)
                        {
                        cout<<"TOP OF STACK-->"<<stck[i];
                        }
                        else
                        {
                        cout<<"\t\n"<<stck[i];
                        }
                        }
                }
                int pop(void)
                {
                        return(stck[top--]);
                }
int isfull()
                {
                        if(top==size-1)
                        {
                        return 1;
                        }
                        else
                        {
                        return 0;
                        }
                }
                int isempty()
                {
                        if(top==-1)
                        {
                                return 1;
                        }
                        else
                        {
                                return 0;
                        }
                }
                stack()
                {
                        stck[100]={0};
                        top=-1;
                }

};
int main()
{
        int choice;
        stack stack;
        cout<<"\nenter the size of stack\n";
        cin>>size;
        do
        {
        cout<<"\n..MENU..\n1.push\n2.pop\n3.display.\n4.EXIT";
        cin>>choice;
        switch(choice)
        {
        case 1:
                if(!(stack.isfull()))
                {
                        stack.push();
                }
                else
                {
                        cout<<"\nSTACK IS FULL\n";
                }
                break;
        case 2:
                if(!(stack.isempty()))
                {
                        cout<<"\nTHE POPPED OUT ELEMENT IS :"<<stack.pop();
                }
                else
                {
                        cout<<"\nSTACK IS EMPTY\n";
                }
                break;
        case 3:
                stack.display();
                break;
        case 4:
                cout<<"\nYOU HAVE EXITED\n";
                break;

        }
 }while(choice!=4);
        return 0;
}

No comments:

Post a Comment