CS301 assignmenr Complete solution cpp

CS301 assignmenr solution cpp

CS301 assignmenr solution cpp Download Below

#include <iostream> 
using namespace std;

class  Stack  

    public: 
        Stack() 
{
size = 3; current = -1;
}     //constructor 
        int pop()
{
return A[current--];
}        // The pop function     
        void push(int x)
{
A[++current] = x;
}        // The push function 
        int top()
{

return A[current];
}          // The top function 
        int isEmpty()
{
return ( current == -1 );
}   // Will return true when stack is empty 
        int isFull()

return ( current == size-1);
} // Will return true when stack is full 

     private: 
       int   object;           // The data element 
       int   current;          // Index of the array 
       int   size;             // max size of the array 
       int   A[3];            // Array of 10 elements     
};  
//----------------------------------------------------------------------------------------

Stack pushStack(int num);
void popStack(Stack stack);

//----------------------------------------------------------------------------------------

// The main method 
int main() 

    int num;
    Stack stackFirst; 
    Stack stackSec;
    Stack stackThird;
    
    int sFPop=0;
    int sSPop=0;
    int sTPop=0;
       
         
//-----------------------------------------------------------------------------------------
    cout<< "Enter a number to push in stack 1: ";
    cin>> num;
    sFPop = num;
    stackFirst = pushStack(num);

cout<< "Enter a number to push in stack 2: ";
    cin>> num;
    sSPop = num;
    stackSec = pushStack(num);

    cout<< "Enter a number to push in stack 3: ";
    cin>> num;
    sTPop = num;
    stackThird = pushStack(num);
    
//-----------------------------------------------------------------------------------------     

    // pop the elements at the stack
cout << "\nStacks before Balancing... " ;

cout << "\nStack 1 numbers:  ";

    popStack(stackFirst);

cout << "\nStack 2 numbers:  ";
    
    popStack(stackSec);

cout << "\nStack 3 numbers:  ";
 
    popStack(stackThird);
    
//-----------------------------------------------------------------------------------------    
            

stackFirst.pop();
stackSec.pop();
stackThird.pop();

                sFPop = stackFirst.pop();
sSPop = stackSec.pop();
sTPop = stackThird.pop();

        stackFirst.push(sSPop);
        stackFirst.push(sTPop);
        
        stackSec.push(sTPop);
        stackSec.push(sFPop);
        
        stackThird.push(sSPop);
        stackThird.push(sFPop);
                  
//----------------------------------------------------------------------------------------- 
                                   
          // pop the elements at the stack
cout << "\n\n\nStacks After Balancing... " ;

cout << "\nStack 1 numbers:  ";

    popStack(stackFirst);

cout << "\nStack 2 numbers:  ";
    
    popStack(stackSec);

cout << "\nStack 3 numbers:  ";
 
    popStack(stackThird);  

cout <<endl<<endl<<endl;
system("pause");   


//------------------------------------------------------------------------------------------
Stack pushStack(int num){
Stack stack;
for(int i = 0; i <=2; i++)  

         if(!stack.isFull())  {            // checking stack is full or not 
                  stack.push(num);                            
         }else 
                  cout <<"\n Stack is full, can't insert new element"; 
    } 
    return stack;
}
//------------------------------------------------------------------------------------------
    void popStack(Stack stack){
        for (int i = 0; i <=2; i++) 
    { 
        if(!stack.isEmpty())  {         // checking stack is empty or not 
                  cout << stack.pop()<<"\t"; 

       } else 
                  cout <<"\n Stack is empty, can't pop "; 
    } 
    
}