Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Create a STACK class with operation for initialization, push and pop. Support for checking underflow condition are also to be provided.


Source Code :

#include<iostream.h>
class stack {
      int st[100];
      int top;
      public:
      void init() {
            top=0;
      }
      void push(int var) {
            st[++top]=var;
      }
      int pop() {
            return st[top--];
      }
};

int  main() {
      int ch,fl=0,ctr=0;
      stack s1;
      while(true) {
            cout<<endl<<"   MAIN MENU";
            cout<<endl<<"1. Initialize";
            cout<<endl<<"2. Push";
            cout<<endl<<"3. Pop";
            cout<<endl<<"4. Exit";
            cout<<endl<<"Enter Choice : ";
            cin>>ch;
            switch(ch) {
                  case 1: {
                        s1.init();
                        cout<<endl<<"Stack Initialization Successful"<<endl;
                        fl=1;
                  }break;
                  case 2: {
                        if(fl==1) {
                              int n;
                              cout<<endl<<"Enter Element : ";
                              cin>>n;
                              if(ctr<100) {
                                    s1.push(n);
                                    ctr++;
                              }
                              else
                                    cout<<endl<<"Stack Overflow"<<endl;
                        }
                        else
                              cout<<endl<<"Stack not initialized"<<endl;
                  }break;
                  case 3: {
                        if(fl==1) {
                              if(ctr>0) {
                                    cout<<endl<<"Element Popped : "<<s1.pop();
                                    cout<<endl<<"Element Remaining : "<<--ctr;
                              }
                              else
                                    cout<<endl<<"Stack Underflow"<<endl;
                        }
                        else
                              cout<<endl<<"Stack not initialized"<<endl;
                  }break;
                  case 4: {
                        exit(0);
                  }break;
                  default: {
                        cout<<endl<<"INVALID CHOICE"<<endl;
                  }
            }
      }
}


Output :

MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 1

Stack Initialization Successful
     
MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 2

Enter Element : 4

MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 2

Enter Element : 8

MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 3

Element Popped : 8
Element Remaining : 1

MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 3

Element Popped : 4
Element Remaining : 0

MAIN MENU
1.  Initialize
2.  Push
3.  Pop
4.  Exit
Enter Choice : 3

Stack Underflow

No comments:

Post a Comment