该程序没有给出所需的输出。 FIFO 的错误实现?

发布于 2024-12-02 06:21:34 字数 1668 浏览 1 评论 0原文

这是一个使用链表的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2024-12-09 06:21:34

您正在构造队列,然后放弃它们。

您无法更新dataNode_P_A,因此您构建的不是列表,而是流苏

当您显然不知道它的含义时,您会调用dataNode_P_A++

您编写了一段又长又复杂的代码,但没有对其进行测试。

你应该从头开始,一步一步走下去。

You are constructing queues 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.

眼眸印温柔 2024-12-09 06:21:34

从哪里开始... 首先,队列数据结构并没有特别用于任何用途。但这不是你问题的根源。就在这里:

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

当迭代链接列表时,您可以通过导航到 ->previous 来移动到下一个元素:

 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; 
   } 
}

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:

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

When iterating through a linked list, you move to the next element by navigating to ->previous:

 void displayQueue() { 
   while( dataNode_P_A != NULL ) { 
       cout << dataNode_P_A->data  << endl; 
       dataNode_P_A = dataNode_P_A->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:

 void displayQueue() { 
   node *entry = dataNode_P_A;
   while( entry != NULL ) { 
       cout << entry->data  << endl; 
       entry = entry->previous; 
   } 
}
美羊羊 2024-12-09 06:21:34

您应该编辑您的 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;
}

并保持所有内容相同,同时遵循 @ Larry Osterman 和 @ Beta 的建议。

You should edit your enter_N_Data() function like:

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;
}

and keep everything same while following the suggestions by @ Larry Osterman and @ Beta.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文