Wednesday 26 October 2011

implementation of stack

//program is implemented by shadab khan
#include<conio.h>
#include<iostream.h>
#include<process.h>

void main()
{
            int stack[150],top,n,i,max_stack,choice,element,n_pop;
            max_stack=150;
            cout<<"How many elements are in the stack: ";
            cin>>n;
            cout<<"Enter "<<" elements.\n";
            for(top=0;top>stack[top];top++)
            {
                        cin>>stack[top];
            }
            cout<<"\nStack is implemented.\nThe stack is\n";
            for(i=top-1;i>=0;i--)
                        cout<<"   ";
            getch();
            while(1)
            {
                        clrscr();
                        cout<<"1: PUSH\n2: POP\n3: Display Stack\n4: Exit\nEnter your choice: ";
                        cin>>choice;
                        switch(choice)
                        {
                                    case 1:     //  PUSH
                                                cout<<"\nPUSH OPERATION\n";
                                                if(top>=max_stack)
                {
                                                            cout<<"Stack is full!";
                                                            getch();
                                                            break;
                                                }
                                                cout<<"Enter an element: ";
                                                cin>>element;
                                                stack[top]=element;
                top++;
                                                cout<<"Item Inserted!";
                                                getch();
                                                break;
                                    case 2:             // POP
                                                cout<<"POP OPERATION\n";
                                                if(top<0)
                                                {
                                                            cout<<"Stack is empty";
                                                            getch();
                                                            break;
                                                }
                                                cout<<"How many elements you want to pop: ";
                cin>>n_pop;
                                                if(n_pop>top)
                                                {
                                                            cout<<"\nError!\nStack is small.";
                                                            getch();
                   break;
                                                }
                                                top=top-n_pop;
                                                cout<<"Items POPed";
                                                getch();
                                                break;
                                    case 3:       //  Display
                                                cout<<"\The stack is\n";
                                                for(i=top-1;i>=0;i--)
                                                            cout<< stack[i] <<"   ";
                                                getch();
                                                break;
                                    case 4:
                                                exit(0);
                                                getch();
                                    default:
                                                cout<<"RE-enter your choice!";
                        }          // End of switch
            }                      // End of while
            getch();
}

No comments:

Post a Comment