Question: Add the following operation to the class stackType. void reverseStack(stackType &otherStack); This operation copies the elements of a stack in reverse order onto another stack.
Add the following operation to the class stackType.
void reverseStack(stackType
This operation copies the elements of a stack in reverse order onto another stack.
Consider the following statements: stackType
The statement: stack1.reverseStack(stack2); copies the elements of stack1 onto stack2 in reverse order. That is, the top element of stack1 is the bottom element of stack2, and so on. The old contents of stack2 are destroyed, and stack1 is unchanged.
Write the definition of the function template to implement the operation reverseStack.
//Code
#include#include using namespace std; template class stackADT { public: virtual void initializeStack() = 0; virtual bool isEmptyStack() const = 0; virtual bool isFullStack() const = 0; virtual void push(const Type& newItem) = 0; virtual Type top() const = 0; virtual void pop() = 0; }; template class stackType: public stackADT { private: int maxStackSize; int stackTop; Type *list; public: void initializeStack() { stackTop = 0; cout << "stackTop " << stackTop << endl; } void print() { for(int i=0; i & otherStack) { list = NULL; copyStack(otherStack); } ~stackType() { delete [] list; } const stackType & operator=(const stackType & otherStack) { if (this != &otherStack) { copyStack(otherStack); } return *this; } bool operator==(const stackType & otherStack) const { if (this == &otherStack) { return true; } else { if (stackTop != otherStack.stackTop) { return false; } else { for (int i = 0; i < stackTop; i++) { if (list[i] != otherStack.list[i]) { return false; } return true; } } } } void copyStack(const stackType & otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; //copy otherStack into this stack. for (int j = 0; j < stackTop; j++) { list[j] = otherStack.list[j]; } } }; int main() { stackType stack1(50); stackType stack2(50); stack1.initializeStack(); stack1.push(23); stack1.push(45); stack1.push(38); stack1.print(); stack2 = stack1; if (stack1 == stack2) cout << "stack1 and stack2 are identical" << endl; else cout << "stack1 and stack2 are not identical" << endl; stack2.pop(); stack2.push(38); cout << "**** After pop and push operations on stack2 ****" << endl; if (stack1 == stack2) cout << "stack1 and stack2 are identical" << endl; else cout << "stack1 and stack2 are not identical" << endl; stack2.push(11); cout << "**** After another push operation on stack2 ****" << endl; if (stack1 == stack2) cout << "stack1 and stack2 are identical" << endl; else cout << "stack1 and stack2 are not identical" << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
