This is what im supposed to do:
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.
Consider the following statements.
stackType stack1;
stackType stack2;
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 is destroyed and stack1 is unchanged.
And here is the code i have for it.
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class Type>
class stackType
{
Type s[10];
int top, n;
public:
stackType()
{
top = -1;
n = 50;
}
stackType(int size)
{
top = -1;
n = size;
}
void push(Type elt)
{
if (top < n - 1)
s[++top] = elt;
else
cout << "\n\tstack is full.Can't insert " << elt << endl;
}
void pop()
{
if (top < 0)
cout << "\n\tstack is empty.\n";
else
cout << "\n\tPoped elt : " << s[top--];
}
void display() {
for (int i = 0; i <= top; i++) {
cout << s[i] << " ";
}
cout << endl;
}
void reverseStack(stackType<Type> &otherStack) {
//Destroy content of other stack by making top to -1
otherStack.top = -1;
//Iterate the current stack and push the elements to other stack
for (int i = top; i >= 0; i--) {
otherStack.push(s[i]);
}
}
};
int main()
{
stackType<int> stack1;
stack1.push(10);
stack1.push(20);
stack1.push(30);
cout << "Stack1 content: \n";
stack1.display();
stackType<int> stack2;
stack1.reverseStack(stack2);
cout << "Stack2 content: \n";
stack2.display();
return 0;
}
The code the book gave me was this:
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
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 Type>
class stackType : public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
void initializeStack()
{
stackTop = 0;
cout << "stackTop " << stackTop << endl;
}
void print()
{
for (int i = 0; i < stackTop; 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<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
~stackType()
{
delete[] list;
}
const stackType<Type>& operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
{
copyStack(otherStack);
}
return *this;
}
bool operator==(const stackType<Type>& 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<Type>& 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<int> stack1(50);
stackType<int> 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;
}