该程序没有给出所需的输出。 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++;
}
}
This is a FIFO program using linked list
. The program does not give the desired output but generates a long loop which stops after sometime and there is a message that the program has stopped working. What is the problem ?
#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++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在构造
队列
,然后放弃它们。您无法更新
dataNode_P_A
,因此您构建的不是列表,而是流苏。当您显然不知道它的含义时,您会调用
dataNode_P_A++
。您编写了一段又长又复杂的代码,但没有对其进行测试。
你应该从头开始,一步一步走下去。
You are constructing
queue
s and then abandoning them.You fail to update
dataNode_P_A
, so that you are not constructing a list so much as a tassel.You invoke
dataNode_P_A++
when you clearly don't know what it means.You have written a long, complicated piece of code without testing it along the way.
You should start over, and go step by step.
从哪里开始... 首先,队列数据结构并没有特别用于任何用途。但这不是你问题的根源。就在这里:
当迭代链接列表时,您可以通过导航到 ->previous 来移动到下一个元素:
话虽如此,您正在做一些其他有问题的事情 - 例如修改全局列表(dataNode_P_A)。在您的示例中这不是问题,但如果您想对列表执行除显示之外的任何操作,则可能会出现问题。
这是 displayQueue 的另一个版本,没有这个问题:
Where to begin... First off the queue data structure isn't particularly used for anything. But that's not the root of your problem. That lies here:
When iterating through a linked list, you move to the next element by navigating to ->previous:
Having said that, you're doing some other things that are questionable - like modifying your global list (dataNode_P_A). That's not a problem in your example, but it can be a problem if you ever want to do anything to the list other than display it.
Here's another version of displayQueue that doesn't have that problem:
您应该编辑您的
enter_N_Data()
函数,如下所示:并保持所有内容相同,同时遵循 @ Larry Osterman 和 @ Beta 的建议。
You should edit your
enter_N_Data()
function like:and keep everything same while following the suggestions by @ Larry Osterman and @ Beta.