In: Computer Science
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;
}
}
#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: