1

そのため、私の課題では、スタック クラスを変更してテストする必要があります。必要なすべてのメソッドを正常に追加しましたが、デモンストレーションするときにスタックにあるものをどのように出力するのが最善かということに行き詰まっています。基本的に、スタックにランダムなデータを入力し、実装したさまざまなメソッドをテストしますが、想定どおりに実際に実行したことを示すためにスタックをコンソールに出力する方法がわかりません。 . これはかなり基本的な質問であることはわかっていますが、ここで立ち往生しています。どうにか getTop メソッドを使用する必要があると思いますが、それを使用してスタックにあるものを出力する方法を教えてください。これが私のすべてのファイルです:

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:

#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 

よろしくお願いします。

4

1 に答える 1

2

@WhozCraig が提案したように、印刷機能を追加する必要があります。

void print(std::ostream& os)
{
    StackNode* current = topPtr;
    while(current != NULL)
    {
        os<<current->item<< " ";
        current = current->next;
    }
}

忘れないで#include <ostream>

于 2013-03-11T20:41:48.913 に答える