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
I am currently displaying
23
45
38
I want to display in reverse order like this
38
45
23
//here is the program
#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 { cout << list[i] << endl; } } bool isEmptyStack() const { return(stackTop == 0); } bool isFullStack() const { return(stackTop == maxStackSize); } void push(const Type& newItem) { if (!isFullStack()) { list[stackTop] = newItem; stackTop++; } else { cout << "Cannot add to a full stack." << endl; } cout << "stacktop: " << stackTop << endl; system("pause"); } Type top() const { assert(stackTop != 0); //if stack is empty, terminate the program. return list[stackTop - 1]; } void pop() { if (!isEmptyStack()) stackTop--; else cout << "Cannot remove from an empty stack." << endl; cout << "pop: " << stackTop << endl; } stackType(int stackSize = 100) { if (stackSize <= 0) { cout << "Size of the array to hold the stack must be positive." << endl; cout << "Creating an array of size 100." << endl; maxStackSize = 100; } else { maxStackSize = stackSize; cout << "maxStackSize " << maxStackSize << endl; } stackTop = 0; list = new Type[maxStackSize]; } stackType(const stackType { list = NULL; copyStack(otherStack); } ~stackType() { delete[] list; } const stackType { if (this != &otherStack) { copyStack(otherStack); } return *this; } bool operator==(const stackType { 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 { 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 stackType 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
