這是一個使用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++;
}
}
從來沒有看到鏈接列表只有一個指向前一個節點的指針。 N –
嗯,奇怪。通常情況下,你會跟蹤單鏈表中的下一個節點,而不是以前的... – trojanfoe
期望的輸出是什麼?什麼是*實際*輸出?它究竟在哪裏停止工作?開始調試器的時間。 – razlebe