Tuesday 14 May 2013

C++ Stack - Part 3


Below is the manipulation of the objects created with the stack data type inside the main function. This final C++ stack source code is used in collaboration with part 1 and part 2.


#include<iostream>
#include "part1_filename.h"    

using namespace std;


int main()
{
     int choice;
     stack Abacus;
     int element;
     while(1)
        {
             cout <<"Select a number for an action you want to perform";
             cout<<endl;
             cout<<endl;
             cout <<"1 - Insert an element into the stack";
             cout<<endl;
             cout<<"2 - Remove an element from the stack";
             cout<<endl;
             cout<<"3 - View the elements of the stack";
             cout<<endl;
             cout<<"4 - See the size of the stack";
             cout<<endl;
             cout<<"5 - End";
             cout<<endl;
             cout<<endl;
             cout <<"Enter your choice: ";
             cin >> choice;
             switch(choice)
           {
               case 1:  cout <<"Input a number: ";
                        cin >> element;
                        cout <<"\n";
                        Abacus.Push(element);
                        break;
                        
               case 2:   
                         Abacus.Pop(); 
                         break;
                   
               case 3:
                         Abacus.Print();
                         break;
                    
               case 4: 
                         Abacus.getSize();
                         break;
                      
               case 5:   exit(0);
               }
         }
         system("pause");
         return 0;
}

The code and its associates are fully functional. There are a few changes to be made and the code can be run on any c++ compiler. 

Feel free to suggest some changes on the source codes.


No comments:

Post a Comment