[英]How to print a stack in C++
so for an assignment of mine we have to modify a stack class and test it. 因此,对于我的作业,我们必须修改堆栈类并对其进行测试。 I have successfully added all the necessary methods, but what I am stuck on is how best to print out what is in the stack when I demonstrate it.
我已经成功添加了所有必需的方法,但是当我演示它时,我所坚持的是如何最好地打印出堆栈中的内容。 So basically, I'm going to fill the stack with random data and test the various methods I implemented, but I don't know how to print out the stack to the console to show that I've actually done what I'm supposed.
因此,基本上,我将用随机数据填充堆栈并测试实现的各种方法,但是我不知道如何将堆栈打印到控制台以表明我实际上已经完成了我应该做的事情。 I know this is a pretty basic question, but I'm just stuck here.
我知道这是一个非常基本的问题,但是我只是停留在这里。 I am guessing I have to use the method getTop somehow, but how to I use that to print what's in the stack.
我猜我必须以某种方式使用getTop方法,但是如何使用它来打印堆栈中的内容。 Here are all my files:
这是我的所有文件:
StackP.cpp StackP.cpp
#include <cassert>
using namespace std;
#include "StackP.h"
Stack::Stack()
: topPtr(0) {
}
Stack::Stack(const Stack& aStack)
throw(OutOfStorageException) {
// Original list is empty
if (aStack.topPtr == 0) {
topPtr = 0;
}
else {
try {
// Copy first node
topPtr = new StackNode;
topPtr->item = aStack.topPtr->item;
// Copy rest of list
StackNode* newPtr = topPtr; // Pointer to new list
for (StackNode* origPtr = aStack.topPtr->next;
origPtr != 0;
origPtr = origPtr->next) {
newPtr->next = new StackNode;
newPtr = newPtr->next;
newPtr->item = origPtr->item;
}
newPtr->next = 0;
}
catch (const bad_alloc&) {
// Release all memory successfully allocated in this copy
while (!isEmpty() ) {
pop();
}
throw OutOfStorageException("Out of memory");
}
}
}
Stack::~Stack() {
// Pop until stack is empty
while (!isEmpty() ) {
pop();
}
assert(topPtr == 0);
}
bool Stack::isEmpty() const {
return topPtr == 0;
}
void Stack::push(const StackItemType& newItem)
throw(OutOfStorageException) {
try {
StackNode* newPtr = new StackNode;
newPtr->item = newItem;
newPtr->next = topPtr;
topPtr = newPtr;
}
catch (const bad_alloc&) {
throw OutOfStorageException("Out of memory");
}
}
}
void Stack::pop()
throw(OutOfDataException) {
if (isEmpty() ) {
throw OutOfDataException("Cannot pop an empty stack.");
}
StackNode* temp = topPtr;
topPtr = topPtr->next;
temp->next = 0; // safeguard
delete temp;
}
void Stack::pop(StackItemType& stackTop)
throw(OutOfDataException) {
if (isEmpty() ) {
throw OutOfDataException("Cannot pop an empty stack.");
}
stackTop = topPtr->item;
StackNode* temp = topPtr;
topPtr = topPtr->next;
temp->next = 0; // safeguard
delete temp;
}
void Stack::getTop(StackItemType& stackTop) const
throw(OutOfDataException) {
if (isEmpty() ) {
throw OutOfDataException("Cannot get the top of an empty stack.");
}
stackTop = topPtr->item;
}
void Stack::popAndDiscard(int count) {
while (count>0 && !isEmpty()) {
pop();
count--;
}//end while
}// end popAndDiscard
StackP.h: StackP.h:
#ifndef STACKP_H
#define STACKP_H 1
#include "OutOfStorageException.h"
#include "OutOfDataException.h"
typedef int StackItemType;
class Stack {
public:
Stack();
Stack(const Stack& aStack)
throw(OutOfStorageException);
~Stack();
bool isEmpty() const;
void push(const StackItemType& newItem)
throw(OutOfStorageException);
void pop()
throw(OutOfDataException);
void pop(StackItemType& stackTop)
throw(OutOfDataException);
void getTop(StackItemType& stackTop) const
throw(OutOfDataException);
void popAndDiscard(int count);
private:
struct StackNode {
StackItemType item;
StackNode* next;
};
StackNode* topPtr;
};
#endif
Thanks in advance for your help! 在此先感谢您的帮助!
You should add print function as @WhozCraig suggested: 您应该按照@WhozCraig的建议添加打印功能:
void print(std::ostream& os)
{
StackNode* current = topPtr;
while(current != NULL)
{
os<<current->item<< " ";
current = current->next;
}
}
don't forget to #include <ostream>
不要忘记
#include <ostream>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.