这是我应该做的:
将以下操作添加到class stackType
:
void reverseStack(stackType &otherStack);
此操作将堆栈中的元素以相反的顺序复制到 另一个堆栈。
请考虑以下语句。
stackType stack1;
stackType stack2;
声明
stack1.reverseStack(stack2);
以相反的顺序将stack1
的元素复制到stack2
上。那就是
stack1
的顶部元素是stack2
的底部元素,依此类推。
stack2
的旧内容被破坏,stack1
不变。
这是我要的代码。
#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;
}
这本书给我的代码是这样的:
#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;
}