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() {...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal =...
Please complete it in C++ Part 1: Stack Create a New Project and give your project...
Please complete it in C++ Part 1: Stack Create a New Project and give your project a name, say Lab6a. Download the given source files StackArr.h and StackArr.cpp from Moodle and save them to your Lab6a folder. Also import them to your project. Add a source file to your project, called StackMain.cpp and implement your program according to the following: Prompt the user to input a program filename. Open the file and check if every right brace (i.e. }), bracket...
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...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
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...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT