C双重链接循环列表插入

时间:2021-04-17 19:27:22

Doesn't insert and doesn't keep adresses of next and prev node: I'm trying to read input from file; It can read all data corectly and based on every line, creates an Aeronava object. It seems that the insert doesn't work.any suggestions?

不插入并且不保留next和prev节点的地址:我正在尝试从文件中读取输入;它可以核心地读取所有数据并基于每一行,创建一个Aeronava对象。似乎插入不起作用。有什么建议吗?

void insertFAV_Av(FAVnode*list, Aeronava *av){
        FAVnode* nn = (FAVnode*)malloc(sizeof(FAVnode));
        //first = list;
        nn->infoUtil = (Aeronava*)malloc(sizeof(Aeronava));
        nn->infoUtil->idAeronava = (char*)malloc(strlen(av->idAeronava) + 1);
        //strcpy(nn->infoUtil->idAeronava, av->idAeronava);
        nn->infoUtil = av;
        if (first == NULL){
            nn->prev = nn->next = nn;
            first = nn;
        }
        else{
            list = first;
            while (list->next != first){
                list = list->next;
            }
            nn->prev = list;
            list->next = nn;
            nn->next = first;

        }
}


struct Aeronava{
    char* idAeronava;
    tipA tipAero;
    short int nrLocuri;
    double greutateMaxima;
};

struct FAVnode{
    FAVnode*next;
    FAVnode*prev;
    Aeronava* infoUtil;
};

2 个解决方案

#1


2  

        nn->prev = list;   // 1
        list->next = nn;   // 2
        nn->next = first;  // 3

Lines 1 and 2 link nn to list in both directions, but line 3 links nn with first in one direction only. You lack the opposite link update here:

第1行和第2行将nn链接到两个方向的列表,但第3行仅将nn与第一个方向链接。你缺少相反的链接更新:

        first->prev = nn;

#2


0  

I see a few problems here. First is this:

我在这看到一些问题。首先是这样的:

     else{
        list = first;
        while (list->next != first){
            list = list->next;
        }
        nn->prev = list;
        list->next = nn;
        nn->next = first;

You are assigning first to list. List is the passed parameter. By doing this, you are clobbering it. A proper insert routine looks like this:

您将首先分配到列表。 List是传递的参数。通过这样做,你正在破坏它。正确的插入例程如下所示:

void insert_node(node_t **head, node_t **tail, nodedata_t *data)
  {
    /* Do some stuff */


    /* Insert Node */
    if (*list == NULL)
        {
          *head = data;
          *tail = data;
      else
        {
          *tail->next = data;
          *tail->next->prev = *tail;
          *tail->next->next = NULL;
          *tail = data;
        }
  }

This inserts at the end of the list. For the middle of the list, you would change the code in the else to the following:

这将插入列表的末尾。对于列表的中间部分,您可以将else中的代码更改为以下内容:

data->next = curr->next;
data->prev = curr;
data->next->prev = data;
curr->next = data;

The variable curr is being defined as "node_t *curr;" The way this works is that you have a pointer to data, and you have a pointer to the current node, and the node after current exists, which we call blue. So you set next in data to point to blue and prev in data to point to curr. Then you set prev in blue to point to data, and next in curr to point to data. Then the list insert is done. There is another special case that this didn't address, which is insertion at end of list. But I will leave that as an exercise for the reader.

变量curr被定义为“node_t * curr;”它的工作方式是你有一个指向数据的指针,你有一个指向当前节点的指针,以及当前存在的节点,我们称之为蓝色。因此,您在数据中设置下一个指向蓝色,并将数据中的prev指向curr。然后将prev设置为蓝色以指向数据,然后将其设置为curr以指向数据。然后完成列表插入。还有另一个特殊情况,即没有解决,这是在列表末尾插入。但我会将其作为读者的练习。

One more thing I noticed. There is no need to typecast the pointer result from malloc. It's a void type which is compatible with all pointer types.

还有一点我注意到了。不需要对malloc的指针结果进行类型转换。它是一种与所有指针类型兼容的void类型。

#1


2  

        nn->prev = list;   // 1
        list->next = nn;   // 2
        nn->next = first;  // 3

Lines 1 and 2 link nn to list in both directions, but line 3 links nn with first in one direction only. You lack the opposite link update here:

第1行和第2行将nn链接到两个方向的列表,但第3行仅将nn与第一个方向链接。你缺少相反的链接更新:

        first->prev = nn;

#2


0  

I see a few problems here. First is this:

我在这看到一些问题。首先是这样的:

     else{
        list = first;
        while (list->next != first){
            list = list->next;
        }
        nn->prev = list;
        list->next = nn;
        nn->next = first;

You are assigning first to list. List is the passed parameter. By doing this, you are clobbering it. A proper insert routine looks like this:

您将首先分配到列表。 List是传递的参数。通过这样做,你正在破坏它。正确的插入例程如下所示:

void insert_node(node_t **head, node_t **tail, nodedata_t *data)
  {
    /* Do some stuff */


    /* Insert Node */
    if (*list == NULL)
        {
          *head = data;
          *tail = data;
      else
        {
          *tail->next = data;
          *tail->next->prev = *tail;
          *tail->next->next = NULL;
          *tail = data;
        }
  }

This inserts at the end of the list. For the middle of the list, you would change the code in the else to the following:

这将插入列表的末尾。对于列表的中间部分,您可以将else中的代码更改为以下内容:

data->next = curr->next;
data->prev = curr;
data->next->prev = data;
curr->next = data;

The variable curr is being defined as "node_t *curr;" The way this works is that you have a pointer to data, and you have a pointer to the current node, and the node after current exists, which we call blue. So you set next in data to point to blue and prev in data to point to curr. Then you set prev in blue to point to data, and next in curr to point to data. Then the list insert is done. There is another special case that this didn't address, which is insertion at end of list. But I will leave that as an exercise for the reader.

变量curr被定义为“node_t * curr;”它的工作方式是你有一个指向数据的指针,你有一个指向当前节点的指针,以及当前存在的节点,我们称之为蓝色。因此,您在数据中设置下一个指向蓝色,并将数据中的prev指向curr。然后将prev设置为蓝色以指向数据,然后将其设置为curr以指向数据。然后完成列表插入。还有另一个特殊情况,即没有解决,这是在列表末尾插入。但我会将其作为读者的练习。

One more thing I noticed. There is no need to typecast the pointer result from malloc. It's a void type which is compatible with all pointer types.

还有一点我注意到了。不需要对malloc的指针结果进行类型转换。它是一种与所有指针类型兼容的void类型。