为什么将节点添加到双向链表会删除除第一个节点之外的所有节点?

Why adding a node to a doubly linked list deletes all nodes except first node?

我正在使用双链表在 c 中保存 4 条不同的记录。

我在为双向链表创建 space 时遇到问题。当我尝试添加超过 2 个节点时,我丢失了除第一个节点之外的所有节点,并且我看到了第一个和最后一个节点的数据。我看不到从第 2 到最后的节点。我尝试更改节点之间的链接,但没有成功。这是我的代码;

struct node {
    char name[100];
    char surname[100];
    char roll[100];
    char department[100];
    struct node *next;
    struct node *prev;
};

struct node *first_oto = NULL, *last_oto = NULL, *l;

 void insert() {
     int i, counter = 1;

     first_oto = (struct node*)malloc(sizeof(struct node));
     first_oto->prev = NULL;
     first_oto->next = NULL;

     printf(" %d- Name:", counter);
     scanf("%s", first_oto->name);
     printf(" %d-Surname", counter);
     scanf("%s", first_oto->surname);
     printf(" %d-Number", counter);
     scanf("%s", first_oto->roll);
     printf(" %d-Department", counter);
     scanf("%s", first_oto->department);
     first_oto->next = NULL;

     for (i = 1; i < n; i++) {
         counter++;

         l = (struct node*)malloc(sizeof(struct node));

         printf(" %d-Name:", counter);
         scanf("%s", l->name);
         printf(" %d-Surname:", counter);
         scanf("%s", l->surname);
         printf(" %d-Number:", counter);
         scanf("%s", l->roll);
         printf(" %d-Department:", counter);
         scanf("%s", l->department);
         printf("\n");

         l->next = NULL;
         l->prev = first_oto;
         first_oto->next = l;
    }

在插入函数的第一部分,我的代码正在获取第一个节点的记录。当我的程序运行 for 循环时,它需要其他节点但只添加最终节点。我应该怎么做才能解决它?

向列表添加元素的代码不完整。

    l->next=NULL;
    l->prev=first_oto;
    first_oto->next=l;
    /* There's another thing you need to do right here. */
}

试试这个

void insert(void)
{
    struct node *new_node, *temp;
    char ans = 'y';
    int pos;
    do {
        traverse();
        printf("Enter the element :\n");
        new_node = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &new_node->info);
        new_node->next = NULL;
        new_node->prev = NULL;
        if (start == NULL) {
            start = new_node;
            current = new_node; 
        } else {
            printf("Enter the position at which u wanna enter : (1-%d)\n", count + 1);
            scanf("%d", &pos);
            if (pos == 1) {
                start->prev = new_node;
                new_node->next = start;
                start = new_node;
            } else
            if (pos == count + 1) {
                current->next = new_node;
                new_node->prev = current;
                current = new_node;
            } else {
                int i = 0;  
                temp = start;
                while (i < pos - 2) {
                    temp = temp->next;
                    i++;
                }
                new_node->prev = temp;
                new_node->next = temp->next;
                temp->next = new_node;
            }
        }
        printf("Wanna enter more ??\n");
        scanf(" %c", &ans);
    } while (ans == 'y' || ans == 'Y');
}

应该有一个临时变量用于添加情况。

     struct node *temp;

在分配后的for循环中l->next=NULL我们必须为temp添加一些代码粒子。我们将第一个元素保留为 temp 并使用 temp 变量来不丢失数据。

在下面的代码中我们这样做了。

    temp = (struct node*)malloc(sizeof(struct node));
    temp = first_oto;
    while(temp->next!=NULL){
        temp = temp->next;
    }
    temp->next = l;
    l->prev = temp;
    last_oto = l;

写完这几行问题就解决了