2011-09-29 113 views
-1

我剛剛完成了這個程序的工作,但從一開始我就注意到它不會打印出應有的所有內容。 需要從頂部開始在堆棧中打印整數,然後從底部開始重新執行。 它正確地打印從上到下,但出於某種原因,它只打印底部到底部的最底部的數字。例如,如果堆棧包含整數1,2,3,4,5,6, ,其中1位於堆棧的底部,6是最高號碼。 程序應該打印以下: 頂{6 5 4 3 2 1}底部底部{1 2 3 4 5 6}頂堆棧程序打印功能不能正常工作

但它打印以下: 頂{6 5 4 3 2 1}底底部{1}重要

這裏是打印功能:

void Print() const  
// Prints stack contents to stdout in both top-to-bottom and bottom-to-top order 
{      
    Node* temp = topPtr; 
    cout << "Top { "; 

    // Forward print 
    while (temp != NULL) 
    { 
    cout << temp->data << " "; 

    if (temp->next == NULL) 
     break; 

    temp = temp->next; 
    } 
    cout << "} Bottom  Bottom { "; 

    // Reverse print 
    while (temp != NULL) 
    { 
    cout << temp->data << " "; 
    temp = temp->previous; 
    } 
    cout << "} Top" << endl; 
} // End Print() 

}; //末級堆棧

,如果你需要在這裏的任何進一步引用是主要的()

#include <iostream> 
#include <fstream> 
#include <new> 
#include <cstddef> 
#include "stack.h" 

using namespace std; 

int main(int argc, char* argv[]) 
{ 
    ifstream inputs;     // Input file for commands 
    char op;        // Hold operation and  optional char input 
    int value;      // Value input from file 
    string comment;     // Holds comment from file 
    Stack* sPtr = NULL;     // Will point to stack object 

    // Output usage message if one input file name is not provided 
    if (argc != 2) 
     { 
     cout << "Usage:\n project03 <inputfile>\n"; 
    return 1; 
    } 

    // Attempt to open input file -- terminate if file does not open 
    inputs.open(argv[1]); 
    if (!inputs) 
    { 
    cout << "Error - unable to open input file" << endl; 
return 1; 
    } 

    // Input and echo header comment from file 
    getline(inputs, comment);   // Input and echo the comment appearing in the test file 
    cout << endl << '#' << comment << endl; 

    // Process commands from input file 
    inputs >> op;      // Attempt to input first command 
    while (inputs) 
    { 
    switch (op)      // Process operation input from file 
    { 
     case '#': // Test file comment 
       getline(inputs, comment);  // Input and echo the comment appearing in the test file 
      cout << '#' << comment << endl; 
      break; 

    case 'c': // Constructor 
      cout << endl << "Stack()"; 
      try 
      { 
       sPtr = new Stack(); // Attempt to create an empty stack object 
       cout << " -- Successful" << endl; 
      } 
      catch (std::bad_alloc) 
      { 
       cout << "Failed : Terminating now..." << endl; 
       return 1; 
      } 
      break; 

    case '+': // Push 
      inputs >> value; 
      cout << "Push(" << value << ")"; 
      try 
      { 
       sPtr->Push(value); 
       cout << " -- successful"; 
      } 
      catch (StackFull) 
      { 
       cout << " -- Failed Full Stack"; 
      } 
      cout << endl; 
      break; 

    case '-': // Pop 
      cout << "Pop() -- "; 
      try 
      { 
       sPtr->Pop(); 
       cout << "successful"; 
      } 
      catch (StackEmpty) 
      { 
       cout << "Failed Empty Stack"; 
      } 
      cout << endl; 
      break; 

    case 'f': // IsFull 
      cout << "IsFull() -- "; 
      try 
      { 
       if (sPtr->IsFull()) 
        cout << "true"; 
       else 
        cout << "false"; 
      } 
      catch (...) 
      { 
       cout << "operation failed"; 
      } 
      cout << endl; 
      break; 

    case 'e': // IsEmpty 
      cout << "IsEmpty() -- "; 
      try 
      { 
        if (sPtr->IsEmpty()) 
         cout << "true"; 
        else 
         cout << "false"; 
      } 
      catch (...) 
      { 
        cout << "operation failed"; 
      } 
      cout << endl; 
      break; 

    case 'm': // Make Empty 
      sPtr->MakeEmpty(); 
      cout << "MakeEmpty()" << endl; 
      break;   

    case 'p': // Print Stack 
      cout << "Print() -- "; 
      sPtr->Print(); 
      break; 

    case 't': // Top of Stack 
      try 
      { 
       cout << "Top() -- " << sPtr->Top() << endl; 
      } 
      catch (StackEmpty) 
      { 
       cout << "Top() -- Failed Empty Stack" << endl; 
      } 
      break; 

    case '>': // Max value within Stack 
      try 
      { 
        cout << "Max() -- " << sPtr->Max() << endl; 
      } 
      catch (StackEmpty) 
      { 
        cout << "Max() -- Failed Empty Stack" << endl; 
      } 
      break; 

    case '<': // Min value within Stack 
      try 
      { 
        cout << "Min() -- " << sPtr->Min() << endl; 
      } 
      catch (StackEmpty) 
      { 
        cout << "Min() -- Failed Empty Stack" << endl; 
      } 
      break; 

    case '?': // Peek(n) Stack 
      inputs >> value; 
      try 
      { 
       cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl; 
      } 
      catch (StackInvalidPeek) 
      { 
       cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl; 
      } 
      break; 

    case 's': // Size of Stack 
      cout << "Size() -- " << sPtr->Size() << endl; 
      break; 

    case 'd': // Destructor 
      delete sPtr; 
      sPtr = NULL; 
      cout << "~Stack()" << endl << endl; 
      break; 

    default: // Error 
      cout << "Error - unrecognized operation '" << op << "'" << endl; 
      cout << "Terminating now..." << endl; 
      return 1; 
      break; 
} 

inputs >> op; // Attempt to input next command 
} 

    return 0; 
} // End main() 

,這裏是頭文件stack.cpp(stack.h)

// 
// stack.h 
// 
// Specification file for Stack class, a stack of integers implemented 
// using doubly-linked nodes. 
// 
// ***** DO NOT MODIFY THIS FILE ***** 
// 
#include <iostream> 
using namespace std; 


#ifndef STACK_H 
#define STACK_H 

class StackEmpty  { /* No Code */ }; 
// StackEmpty exception class - throw an object of this type when stack is empty 
// Hint: there is no code for exception classes 

class StackFull   { /* No Code */ }; 
// StackFull exception class - throw an object of this type when stack is full 

class StackInvalidPeek { /* No Code */ }; 
// StackInvalidPeek exception class - throw an object of this type when invalid peek position is used 


struct Node    // Node data type for storing a single stack entry along with pointers to 
{       // neighboring entries (previous and next) in the stack 
    Node* previous;   // Member variable that holds the address of the predessor node in the stack sequence 
    Node* next;    // Member variable that holds the address of the successor node in the stack sequence 
    int data;    // Member variable that holds the data value 
}; 


class Stack    // Implements stack of integers ADT using doubly-linked sequence of nodes 
{ 
    private: 
    Node* topPtr;   // Points to the top node on the stack array 

public: 
Stack();    // Default constructor initializes empty stack 


~Stack();    // Destructor deallocates all nodes from stack 
         // Must not create a memory leak 

void Push(int n);  // Pushes integer n onto top of stack. 
         // If unable to push, throws StackFull exception. 

void Pop();   // Removes top integer from stack 
         // If stack is already empty, throws StackEmpty exception 

bool IsEmpty() const; // Returns true if stack is empty; false otherwise 


bool IsFull() const; // Returns true if stack is full; false otherwise 


void MakeEmpty();  // Removes all nodes from stack leaving an empty, but usable stack 
         // Must not create a memory leak 

int Top() const;  // Returns value of top integer on stack WITHOUT modifying the stack 
         // If stack is empty, throws StackEmpty exception 

int Size() const;  // Returns number of items on stack WITHOUT modifying the stack 


int Max() const;  // Returns value of largest integer within stack WITHOUT modifying the stack 
         // If stack is empty, throws StackEmpty 

int Min() const;  // Returns value of smallest integer within stack WITHOUT modifying the stack 
         // If stack is empty, throws StackEmpty 

int Peek(int n) const; // Returns stack value n levels down from top of stack. Peek(0) = Top() 
         // If position n does not exist, throws StackInvalidPeek 


    . ./******* DO NOT MODIFY ANY OF THE CODE FOR PRINT()    *******/ 
/****** DO NOT PLACE A COPY OF PRINT() CODE IN STACK.CPP!!! *******/ 

void Print() const  
// Prints stack contents to stdout in both top-to-bottom and bottom-to-top order 
{      
    Node* temp = topPtr; 
    cout << "Top { "; 

    // Forward print 
    while (temp != NULL) 
    { 
    cout << temp->data << " "; 

    if (temp->next == NULL) 
     break; 

    temp = temp->next; 
    } 
    cout << "} Bottom  Bottom { "; 

    // Reverse print 
    while (temp != NULL) 
    { 
    cout << temp->data << " "; 
    temp = temp->previous; 
    } 
    cout << "} Top" << endl; 
} // End Print() 

}; // End Class Stack 

#endif 

最後這裏是stack.cpp,這是我創建的文件剩下的部分。

// 
// stack.cpp 
// 
// 
// Created by Otapia on 9/19/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#include <iostream> 
#include <new> 
#include "stack.h" 

Stack::Stack()   // Default constructor initializes empty stack 
{ 
topPtr = NULL; 
} 

Stack::~Stack()   // Destructor deallocates all nodes from stack 
        // Must not create a memory leak 
{ 
Node* tempPtr; 
while (topPtr != NULL) 
{ 
tempPtr = topPtr; 
topPtr = topPtr->next; 
delete tempPtr; 
} 
} 

void Stack::Push(int n) // Pushes integer n onto top of stack. 
        // If unable to push, throws StackFull exception. 
{ 
if(!IsFull()) 
{ 

Node* tempPtr = new Node; 
tempPtr->data = n; 
tempPtr->next = topPtr; 
topPtr = tempPtr; 

} 
else 
throw IsFull(); 
} 

void Stack::Pop()  // Removes top integer from stack 
        // If stack is already empty, throws StackEmpty exception 
{ 
    if (!IsEmpty()) 
    { 
    Node* tempPtr; 
    tempPtr = topPtr; 
    topPtr = topPtr->next; 
    delete tempPtr; 
} 
else 
throw StackEmpty(); 
} 

bool Stack::IsEmpty() const // Returns true if stack is empty; false otherwise 
{ 

    return(topPtr == NULL); 

} 

bool Stack::IsFull() const // Returns true if stack is full; false otherwise 
{ 

Node* location; 
try 
{ 
location = new Node; 
delete location; 
return false; 
} 
catch(std::bad_alloc) 

{return true; } 

} 

void Stack::MakeEmpty() // Removes all nodes from stack leaving an empty, but usable stack 
        // Must not create memory leak 
{ 

    Node* tempPtr; 
    while (topPtr != NULL) { 
    tempPtr = topPtr; 
    topPtr = topPtr->next; 
    delete tempPtr; 
} 
    topPtr = NULL; 

} 

int Stack::Top() const // Returns value of top integer on stack WITHOUT modifying the stack 
{ 
if(!IsEmpty()) 
return topPtr->data; 


throw StackEmpty(); 
} 

int Stack::Size() const // Returns number of items on stack WITHOUT modifying the stack 
{ 

Node* temp = topPtr; 
int count = 0; 
while (temp != NULL) 
{ 
temp = temp->next; 
count ++; 
} 
return count; 

} 

int Stack::Max() const // Returns value of largest integer within stack WITHOUT modifying the stack 
        // If stack is empty, throws StackEmpty 
{ 
int max = 0; 
int n; 
Node* temp = topPtr; 
    if(!IsEmpty()) 
{ 
while(temp != NULL) 
{ 
n = temp->data; 
if(n > max) 
{ 
max = n; 
} 
temp = temp->next; 
} 
return max;} 

else 
throw StackEmpty(); 
} 

int Stack::Min() const // Returns value of smallest integer within stack WITHOUT modifying the stack 
        // If stack is empty, throws StackEmpty 
{int min = 100; 
int n; 
Node* temp = topPtr; 
    if(!IsEmpty()) 
{ 
while(temp != NULL) 
{ 
n = temp->data; 
if(n < min) 
{ 
min = n; 
} 
temp = temp->next; 
} 
return min;} 

else 
throw StackEmpty(); 
} 

int Stack::Peek(int n) const // Returns stack value n levels down from top of stack.  Peek(0) = Top() 
          // If position n does not exist, throws StackInvalidPeek 
{ 

int num = 0; 
int x = 0; 
Node* temp = topPtr; 
    if(!IsEmpty()) 
{ 
while(temp != NULL) 
{ 

if (x >= n || temp->next == NULL) 
break; 
temp = temp->next; 

x++; 
} 
if (n <= x) 
{ 
num = temp->data; 
} 
else throw StackInvalidPeek(); 

} 
else 
throw StackInvalidPeek(); 
return num; 
} 
+0

這是一個堆棧,還是一個鏈表?你有什麼動機編寫自己的堆棧類,而不是std :: stack或std :: vector? – Pubby

+0

@ Pubby8用鏈表實現的堆棧? –

+0

@ Pubby8這是一個學校的基本程序,你可以使用什麼以及什麼不使用的限制,除了打印外,其它任何地方都可以使用。 –

回答

1

簡單。

while (temp != NULL) 
{ 
    cout << temp->data << " "; 
    temp = temp->previous; 
} 

從你的問題描述和error'd代碼,我假設temp->previous是不正確的。我認爲這被設置爲某種push函數。

void Stack::Push(int n) // Pushes integer n onto top of stack. 
       // If unable to push, throws StackFull exception. 
{ 
    if(!IsFull()) 
    { 
    Node* tempPtr = new Node; 
    tempPtr->data = n; 
    tempPtr->next = topPtr; 
    topPtr = tempPtr;  
    } else 
    throw IsFull(); 
} 

你從未previous,所以它留作(零)某個不確定的值。此外,previous未在您的cpp文件中設置或檢查任何地方。它應該在這裏設置,但實際上並不需要在其他地方。

最後,throw IsFull()是一個例外。您可能並不是想要throw函數調用的結果bool

+0

是的,它應該是拋出StackFull(); –