2013-03-11 1 views
1

그래서 우리는 스택 클래스를 수정하고 테스트해야합니다. 필자는 필요한 모든 메소드를 성공적으로 추가했지만 스택에 무엇이 가장 좋은지를 증명할 때이를 증명할 수 있습니다. 그래서 기본적으로 무작위 데이터로 스택을 채우고 구현 한 다양한 방법을 테스트 할 것입니다. 그러나 실제로 스택을 콘솔에 인쇄하여 내가 실제로 한 것을 수행했음을 보여줄 수는 없습니다. . 나는 이것이 아주 기본적인 질문이라는 것을 알고 있지만, 나는 여기서 막혀있다. 어떻게 든 getTop 메서드를 사용해야한다고 생각하지만 스택에있는 내용을 출력하는 방법을 사용합니다. 여기에 내 모든 파일은 다음과 같습니다C++에서 스택을 출력하는 방법

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 

감사합니다! @WhozCraig가 제안

+4

하는 것을 잊지 마세요, 당신은 또한 소모하지 않는 한 당신은 스택을 검사 할 수 없습니다. 그게 전부입니다. –

+0

스택 오버플로에 오신 것을 환영합니다. 여러 가지 이유로 숙제 표는 권장하지 않습니다. –

+2

스택 오버플로에 오신 것을 환영합니다. 문제를 표시하는 데 필요한 최소 수준으로 코드를 줄이십시오. 감사. – thiton

답변

2

당신은 인쇄 기능을 추가해야합니다 :

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

는 일반적으로 #include <ostream>

관련 문제