Question

In: Computer Science

C++ Please complete based on the code below. Declare another stack object to the code in...

C++

Please complete based on the code below.

Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack.

#include

using namespace std;

#define MAXSize 10

class Stack {
int top;
public:

int stk[MAXSize];
Stack() { top = -1; }
bool push(int element);
int pop();
bool Roll();
bool ReversePrint();
bool isEmpty();
};

int main() {
int choice, itemVal;
Stack obj;

do {
cout << "\n\n---------------- Stack Interface-----------\n";
cout << "************** 1) Push in stack *************\n";
cout << "************** 2) Pop from stack **************\n";
cout << "************** 3) Print in reverse order **************\n";
cout << "************** 4) Roll Opeation *****************\n";
cout << "************** 5) Exit ****************\n";

cout << "\nEnter choice: ";
cin >> choice;

switch (choice)
{
case 1:
{
cout << "Enter value to be pushed: ";
cin >> itemVal;
if (obj.push(itemVal))
cout << "Element " << itemVal << " is pushed into stack\n";
break;
}
case 2:
{
int popElment;
popElment = obj.pop();

if (popElment != 0)
cout << "Element " << popElment << " is popped from the stack ";
break;
}
case 3:
{
obj.ReversePrint();
break;
}
case 4:
{
int indexRoll = obj.Roll();
if (!indexRoll)
cout << "Stack contains less than two elements, So Roll operation is not possible";
break;
}
case 5:
{
cout << "Exit";
exit(1);
//break;
}
default:
{
cout << "Invalid Choice" << endl;
}
}
} while (choice != 5);
return 0;
}

bool Stack::push(int element) {
if (top >= (MAXSize - 1)) {
cout << "Stack is full (Overflow)";
return false;
}
else {
top++;
stk[top] = element;
return true;
}
}

int Stack::pop() {
if (isEmpty()) {
cout << "Stack is empty (Underflow)";
return 0;
}
else {
int element = stk[top];
top--;
return element;
}
}

bool Stack::isEmpty() {
if (top < 0)
return true;
else
return false;
}


bool Stack::ReversePrint() {
if (isEmpty()) {
cout << "Stack is empty (Underflow)";
return false;
}
else {
cout << "\nElement of stack in reverse order are:";
for (int loopcounter = top; loopcounter >= 0; loopcounter--)
cout << " " << stk[loopcounter];

return true;
}

}

bool Stack::Roll() {
if (top <= 0) {

return false;
}
else {
int temp;
temp = stk[top - 1];
stk[top - 1] = stk[top];
stk[top] = temp;
cout << "\nTop two elements " << stk[top] << " and " << stk[top - 1] << " of the stack are rolled \n";
return true;
}
}

Solutions

Expert Solution

#include"iostream"
using namespace std;

#define MAXSize 10

class Stack {
   int top;
public:

   int stk[MAXSize];
   Stack()
   {
       top = -1;
   }
  
   bool push(int element);
   int pop();
   bool Roll();
   bool ReversePrint();
   bool isEmpty();
   void copyStack(Stack*);       // this is the new copyStack function
};

int main() {
   int choice, itemVal;
   Stack obj;
   Stack anotherStack;

   do {
       cout << "\n\n---------------- Stack Interface-----------\n";
       cout << "************** 1) Push in stack 1 *************\n";
       cout << "************** 2) Pop from stack 1 **************\n";
       cout << "************** 3) Print stack 1 in reverse order **************\n";
       cout << "************** 4) Roll Opeation on stack 1 *****************\n";
       cout << "************** 5) Push in stack 2 *************\n";
       cout << "************** 6) Pop from stack 2 **************\n";
       cout << "************** 7) Print stack 2 in reverse order **************\n";
       cout << "************** 8) Roll Opeation on stack 2 *****************\n";
       cout << "************** 9) Copy stack 1 to stack 2 *****************\n";
       cout << "************** 10) Exit ****************\n";
      
       cout << "\nEnter choice: ";
       cin >> choice;
      
       switch (choice)
       {
           case 1:
           {
               cout << "Enter value to be pushed: ";
               cin >> itemVal;
               if (obj.push(itemVal))
                   cout << "Element " << itemVal << " is pushed into stack\n";
               break;
           }
           case 2:
           {
               int popElment;
               popElment = obj.pop();
              
               if (popElment != 0)
                   cout << "Element " << popElment << " is popped from the stack ";
               break;
           }
           case 3:
           {
               obj.ReversePrint();
               break;
           }
           case 4:
           {
               int indexRoll = obj.Roll();
               if (!indexRoll)
                   cout << "Stack contains less than two elements, So Roll operation is not possible";
               break;
           }
           case 5:
           {
               cout << "Enter value to be pushed: ";
               cin >> itemVal;
               if (anotherStack.push(itemVal))
                   cout << "Element " << itemVal << " is pushed into stack\n";
               break;
           }
           case 6:
           {
               int popElment;
               popElment = anotherStack.pop();
              
               if (popElment != 0)
                   cout << "Element " << popElment << " is popped from the stack ";
               break;
           }
           case 7:
           {
               anotherStack.ReversePrint();
               break;
           }
           case 8:
           {
               int indexRoll = anotherStack.Roll();
               if (!indexRoll)
                   cout << "Stack contains less than two elements, So Roll operation is not possible";
               break;
           }
           case 9:
           {
               obj.copyStack(&anotherStack);
               break;
           }
           case 10:
           {
               cout << "Exit";
               exit(1);
           }
           default:
           {
               cout << "Invalid Choice" << endl;
           }
       }
   }while (choice != 10);
  
   return 0;
}

// this function will copy all the elements in our stack into an array
// and then push them back onto the stacks
void Stack::copyStack(Stack* obj){
   Stack temp;
   int elt;
   int arr[MAXSize];
   int i = 0;
  
   // pop element from stack and insert it into array
   while(this->isEmpty() == false){
       elt = this->pop();
       arr[i++] = elt;
   }
  
   // start pushing elements into another stack and original stack from array
   int j = i - 1;
   while(j != -1){
       this->push(arr[j]);
       obj->push(arr[j]);
       j--;
   }
}


bool Stack::push(int element) {
   if (top >= (MAXSize - 1)) {
       cout << "Stack is full (Overflow)";
       return false;
   }
   else {
       top++;
       stk[top] = element;
       return true;
   }
}

int Stack::pop() {
   if (isEmpty()) {
       cout << "Stack is empty (Underflow)";
       return 0;
   }
   else {
       int element = stk[top];
       top--;
       return element;
   }
}

bool Stack::isEmpty() {
   if (top < 0)
       return true;
   else
       return false;
}


bool Stack::ReversePrint() {
   if (isEmpty()) {
       cout << "Stack is empty (Underflow)";
       return false;
   }
   else {
       cout << "\nElement of stack in reverse order are:";
       for (int loopcounter = top; loopcounter >= 0; loopcounter--)
           cout << " " << stk[loopcounter];

       return true;
   }

}

bool Stack::Roll() {
   if (top <= 0) {

       return false;
   }
   else {
       int temp;
       temp = stk[top - 1];
       stk[top - 1] = stk[top];
       stk[top] = temp;
       cout << "\nTop two elements " << stk[top] << " and " << stk[top - 1] << " of the stack are rolled \n";
       return true;
   }
}

Output:


Related Solutions

Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that....
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that. (Don't edit main.c or challenge.h, only edit challenge.c) The instructions are in the comments. Hint: the_person is declared in main, so you need to define it in challenge.c using extern challenge.c #include "challenge.h" //return: struct //param: (struct person p1, struct person p2) //TODO: create a function that returns the person who has a higher GPA. // 1. if GPAs are equal, then return the...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
CODE IN C++ PLEASE Create a structure (object) named Person that has the following characteristics: •...
CODE IN C++ PLEASE Create a structure (object) named Person that has the following characteristics: • full name • age • family members full names represent by an array of size 10. Write a program that creates an array of Person of size 5. Populate the array of objects with the information given by user input according to the following specifications: • With input from the user i) Populate only 3 positions of the array of Person object. ii) Populate...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT