Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I needed to make a function that would reverse a stack. I have the reverse function working, but I need help with making a while

I needed to make a function that would reverse a stack. I have the reverse function working, but I need help with making a while function in the driver to display the information of the stack. both header files and the driver is included.

stackADT.h

#pragma once 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;

};

stackType.h

#pragma once #include

#include"stackADT.h"

using namespace std;

template class stackType : public stackADT { public: const stackType& operator=(const stackType&);

void initializeStack();

bool isEmptyStack() const;

bool isFullStack() const;

void push(const Type& newItem);

Type top() const;

void pop();

stackType(int stackSize = 100);

bool operator==(const stackType& otherStack) const;

stackType(const stackType& otherStack); //Copy Constructor

~stackType(); //Destructor

void reverseStack(stackType& otherStack);

private: int maxStackSize; int stackTop; Type *list;

void copyStack(const stackType& otherStack);

};

template void stackType::initializeStack() { stackTop = 0; }

template bool stackType::isEmptyStack() const { return (stackTop == 0); }

template bool stackType::isFullStack() const { return(stackTop == maxStackSize); }

template void stackType::push(const Type& newItem) { if (!isFullStack()) { list[stackTop] = newItem; stackTop++;

} else { cout << "The stack is full." << endl; }

}

template Type stackType::top() const { if (stackTop != 0) return list[stackTop - 1]; }

template void stackType::pop() { if (!isEmptyStack()) stackTop--;

else cout << "Cannot remove from an empty stack." << endl; }

template stackType::stackType(int stackSize) { 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;

stackTop = 0; list = new Type[maxStackSize];

}

template stackType::~stackType() { delete[] list; }

template void stackType::copyStack(const stackType& otherStack) { delete[] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize];

for (int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; }

template stackType::stackType(const stackType& otherStack) { list = NULL; copyStack(otherStack); }

template const stackType& stackType::operator=(const stackType& otherStack) { if (this != &otherStack) copyStack(otherStack);

return *this; }

template bool stackType::operator==(const stackType& otherStack) const { if (this == &otherStack) return true; else if (maxStackSize != otherStack.maxStackSize || stackTop != otherStack.stackTop) return false; else { for (int i = 0; i < stackTop; i++) if (list[i] != otherStack.list[i]) return false; return true; } }

template void stackType::reverseStack(stackType& otherStack) { int i, q, temp; i = 0; q = maxStackSize-1; while(i

/*for (int i = 0; i < maxStackSize; i++) { cout << list[i] << endl; }

cout << "********************************************" << endl;

for (int i = 0; i

/*template void stackType::showStack() { while (&isEmptyStack()) { cout << stackTop() << endl; pop(); } }*/

driver.cpp

#pragma once #include "stackType.h" #include

using namespace std;

int main() {

int size, number; cout << "What is the size of the stack?" << endl; cin >> size;

stackType stack1(size), stack2;

cout << "Please enter a number: " << endl; cin >> number;

while (number != -9999) { stack1.push(number); cout << "Enter a number or -9999 if you you're done:" << endl; cin >> number;

} stack2 = stack1;

/*if (stack2 == stack1) cout << "These are two identical stacks." << endl; else cout << "These are two different stacks." << endl;*/

for (int i = 0;i

/*if (stack2 == stack1) cout << "These are two identical stacks." << endl; else cout << "These are two different stacks." << endl;*/

stack2.reverseStack(stack2);

/*for (int j = 0; j < size; j++) { cout << stack2.top() << endl; stack2.pop(); }

while (stack2.top() != 0) { cout << stack2.top() << endl; stack2.pop(); }*/

system("pause"); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions