2011-08-30 118 views
-2

這是一個使用linked list的FIFO程序。該程序沒有給出所需的輸出,但會產生一個長時間的循環,在某個時間後停止,並且有消息表明該程序已停止工作。問題是什麼 ?該程序沒有給出所需的輸出。錯誤的執行FIFO?

#include <iostream> 
using namespace std; 

struct node { 
     int data; 
     struct node* previous; // This pointer keeps track of the address of the previous node 
}; 

struct queue { 
     node* first; 
     node* last; 
}; 

node* dataNode_P_A; 

bool loop = true; 

struct node* enterData(); 
struct node* enter_N_Data(); 
void displayQueue(); 

int main() { 
    struct node* dataNode= enterData(); 

    while(loop) { 
     cout << "Want to enqueue ? Press y/n : "; 
     char ans; 
     cin >> ans; 
     if(ans == 'y') { 
      struct node* dataNode_N = enter_N_Data(); 
     } else { 
      break; 
     } 
    } 

    displayQueue(); 
} 

struct node* enterData() { 
    cout << "Enter the number : "; 
    dataNode_P_A = new node; // Now dataNode points to a chunk allocated to node 
    cin >> dataNode_P_A->data; 
    dataNode_P_A->previous = NULL; // this is set to NULL because no one follows till now 
    queue* q = new queue; 
    q->first = dataNode_P_A; // this pointer points to the first element 
    return dataNode_P_A; 
} 

struct node* enter_N_Data() { 
    cout << endl << "Enter the number : "; 
    node* dataNode = new node; 
    cin >> dataNode->data; 
    dataNode->previous = dataNode_P_A; 
    queue* q = new queue; 
    q->last = dataNode; // this pointer points to the last element 
    return dataNode; 
} 

void displayQueue() { 
    while(dataNode_P_A != NULL) { 
     cout << dataNode_P_A->data << endl; 
     dataNode_P_A++; 
    } 
} 
+3

從來沒有看到鏈接列表只有一個指向前一個節點的指針。 N –

+0

嗯,奇怪。通常情況下,你會跟蹤單鏈表中的下一個節點,而不是以前的... – trojanfoe

+0

期望的輸出是什麼?什麼是*實際*輸出?它究竟在哪裏停止工作?開始調試器的時間。 – razlebe

回答

6

您正在構建queue s,然後放棄它們。

您無法更新dataNode_P_A,使你沒有構建一個列表竟有如此流蘇

當你明確地不知道它的含義時,你調用dataNode_P_A++

您已經編寫了一段很長且複雜的代碼,而未對其進行測試。

你應該重新開始,並一步一步來。

+0

Oooh,很好 - 我錯過了enterData和Enter_N_Data都不更新dataNode_P_A的事實。 –

4

從哪裏開始......首先關閉隊列數據結構並不是特別用於任何事情。但這不是你問題的根源。這就出在這裏:

void displayQueue() { 
    while(dataNode_P_A != NULL) { 
     cout << dataNode_P_A->data << endl; 
     dataNode_P_A++; 
    } 
} 

當通過鏈表迭代,你通過導航到移動到下一個元素 - >前面:

void displayQueue() { 
    while(dataNode_P_A != NULL) { 
     cout << dataNode_P_A->data << endl; 
     dataNode_P_A = dataNode_P_A->previous; 
    } 
} 

說了這麼多,你正在做一些其他的事情,是有問題的 - 就像修改你的全局列表(dataNode_P_A)一樣。在你的例子中,這不是一個問題,但如果你想對列表進行任何操作而不是顯示它,這可能會成爲問題。

這裏的displayQueue的另一個版本,不存在這樣的問題:

void displayQueue() { 
    node *entry = dataNode_P_A; 
    while(entry != NULL) { 
     cout << entry->data << endl; 
     entry = entry->previous; 
    } 
} 
+1

「您可以通過導航到 - >上一個」 「來移動到下一個元素。易於記憶,易於使用。 – unkulunkulu

+0

'displayQueue'函數總是隻顯示** **輸入的第一個數字,因爲'dataNode_P_A'永遠不會改變。我怎樣才能克服這個問題? –

+0

我想我有點困惑的問題 - dataNode_P_A永遠不會爲空,因爲enterData在單個dataNode_P_A元素上創建。 –

0

您應該編輯您的enter_N_Data()功能,如:

node* temp; // global as others in your program 

struct node* enter_N_Data() { 
cout << endl << "Enter the number : "; 
node* dataNode = new node; 
cin >> dataNode->data; 
temp = new node; 
temp = dataNode_P_A; 
dataNode_P_A = dataNode; // update dataNode_P_A 

dataNode->previous = temp; 

queue* q = new queue; 
q->last = dataNode; // this pointer points to the last element 
return dataNode; 
} 

,並把一切都相同,而下面的建議@拉里奧斯特曼和@貝塔。