2015-04-06 81 views
0

我已經編寫了一個用於堆棧數據結構的程序。 顯示堆棧元素時顯示錯誤。當堆棧具有多於2個值在C++中顯示堆棧元素時出現無限循環

#include <iostream> 
#include <string> 

using namespace std; 

節點類

class Node 
{ 
public: 
    int data; 
    Node *previous; 
}; 
class Stack 
{ 
private: 
    Node* head,start; 
public: 
    Stack(); 
    Node* getNode(); 
    void parseValue(); 
    void push(Node *); 
    void display(); 
}; 

堆棧類

Stack::Stack() 
{ 
    head = NULL; 
} 
void Stack::parseValue() 
{ 
    char choice; 
    Node *newNode = NULL; 
     while (1) 
     { 
      cout << "Enter Data in the List (Enter N to cancel) "; 
      cin >> choice; 
      if (choice == 'n' || choice == 'N') 
      { 
       break; 
      } 
      newNode = getNode(); 
      push(newNode); 
     } 
    } 

int main() 
{ 
    Stack a; 
    a.parseValue(); 
    a.display(); 

} 

回答

1

嘗試此無限循環在稱爲啓動時顯示的功能。

#include <iostream> 
#include <string> 

using namespace std; 

class Node 
{ 
public: 
    int data; 
    Node *previous; 
}; 
class Stack 
{ 
private: 
    Node *head, *start; 
// Node* head,start; // This is same with "Node start, *head"; 
public: 
    Stack(); 
    Node* getNode(); 
    void parseValue(); 
    void push(Node *); 
    void display(); 
}; 

Stack::Stack() 
{ 
    head = NULL; 
} 
Node *Stack::getNode() 
{ 
    Node *temp = new Node; 
    cin >> temp->data; 
    temp->previous = NULL; 
    return temp; 
} 
void Stack::parseValue() 
{ 
    char choice; 
    Node *newNode = NULL; 
     while (1) 
     { 
      cout << "Enter Data in the List (Enter N to cancel) "; 
      cin >> choice; 
      if (choice == 'n' || choice == 'N') 
      { 
       break; 
      } 
      newNode = getNode(); 
      push(newNode); 
     } 
    } 

void Stack::push(Node* newNode) 
{ 
    if(head == NULL) 
    { 
     head = newNode; 
     cout << newNode->data << " added to stack" << endl; 
    } 
    else 
    { 
     // change sequencs to this 
     newNode->previous = head; 
     head = newNode; 

     /* 
     head = newNode; // if you do this first, 
     newNode->previous = head; // then newNode becomes head node 
     // and results like this: head->previous = head, 
     // so head->head->head->... 
     */ 
    } 

} 

void Stack::display() 
{ 
    if(head == NULL) 
    { 
     cout << "No data in the stack to display."; 
    } 
    else 
    { 
     Node * temp = head; 
     while(temp != NULL) // changed; you should check temp's state because it points the ndoe you want 
     { 
      cout << temp->data << "\t"; 
      temp = temp->previous; 
     } 
    } 
} 
int main() 
{ 
    Stack a; 
    a.parseValue(); 
    a.display(); 
    return 0; // it is better to end main with return statement 
} 

編輯:

這是更好的堆棧模型。嘗試一下。

#include <iostream> 
using namespace std; 

// I don't know what you learned, so I'll try 
// to explain my code as easy as possible. 

// define stack's status constant 
const int STACK_IS_EMPTY = -1; // -1 or whatever you want 

// Node 
// If you learned C++ template, replace it with that 
class Node { 
    // It is better to encapsulate member variable(field) 
    // than to let fields in pulic 
    int _data; 
    Node *_next; 
public: 

    // constructor 
    // * advice: try to find 'member initializer' 
    Node(int data) { 
     _data = data; 
    } 
    /* same constructor written with member initializer 
    Node(int data): _data(data) {} 
    */ 

    // member getter & setter method to encapsulation 
    int data() { return _data; } 
    void setData(int data) { _data = data; } 
    Node *next() { return _next; } 
    void setNext(Node *next) { _next = next; } 
}; 

// Stack 
class Stack { 
    // fields 
    Node *head; 

public: 
    Stack(); // constructor 
    ~Stack(); // destructor 
    // push and pop functions are stack basic functions 
    void push(int data); 
    int pop(); 
    // convenient functions 
    bool is_empty(); // return true when stack is empty, false when not 
    int top(); // return stack's top node data 
}; 

// constructor 
Stack::Stack() { 
    // * advice: C++11 or higher supports nullptr keywords; try this after 
    // head = nullptr; 
    head = NULL; 
} 

void Stack::push(int data) { 
    // make new node 
    Node *newNode = new Node(data); 

    if (is_empty() == true) { // if stack is empty 
     head = newNode; 
    } 
    else { // stack is not empty 
     newNode->setNext(head); 
     head = newNode; 
    } 
} 
int Stack::pop() { 
    if (is_empty() == true) { 
     return STACK_IS_EMPTY; 
    } 
    Node *delNode = head; 
    head = delNode->next(); 
    int data = delNode->data(); 
    delete delNode; 
    return data; 
} 

// convenient functions 
bool Stack::is_empty() { 
    return (head == NULL); 
} 
int Stack::top() { 
    if (head == NULL) { 
     return STACK_IS_EMPTY; 
    } 
    return head->data(); 
} 

// destructor 
Stack::~Stack() { 
    // you must deallocate dynamically allocated object 
    // if you don't do this, it results to serious leak of memories 

    if (is_empty() == true) { // if stack is empty 
     return; // just return 
    } 

    // deallocate all nodes 
    while (is_empty() == false) { // while stack is not empty 
     pop(); // keep popping 
    } 
} 

// Demonstration of stack usage 
int main() { 
    int input; 
    Stack stack; 

    while (true) { // keep getting input number and pushing it into stack 
     cout<<"Enter natural number (0 when quit): "; 
     cin>>input; 

     if (input == 0) { // when quit number has entered 
      break; // quit 
     } 

     stack.push(input); 
    } 

    while (stack.is_empty() == false) { // while stack is not empty 
     int data = stack.pop(); // pop stack's node and get it's data 
     cout<<data<<endl; // print it 
    } 

    return 0; 
} 
  • 我的母語不是英語,所以我可以說,奇怪的句子..
+0

老實說,我想告訴你更好的堆棧模型,但它超出範圍,這個問題... – 2015-04-11 05:13:12

+0

這剛剛工作。 顯示我想知道更好的堆棧模型。 – 2015-04-11 05:47:39

+0

你應該知道這只是爲了學習,這不會用在真實的項目中;我們使用STL(如std :: stack )。 – 2015-04-11 09:00:18