c - 为什么我的双向链表的 C 实现会创建重复值?

标签 c doubly-linked-list

我用 C 语言编写了双向链表实现的代码。在插入值之后,我得到了重复的值。即我给出的最后一个值在所有列表项中重复。

我的代码如下

header.h

#include<stdio.h>
#include<stdlib.h>
typedef struct doubly_list
{
 int id;
 char *name;
 struct doubly_list *next;
 struct doubly_list *prev;
}node;
void insertfirst(node **,int ,char *);
void insertlast(node **,int ,char *);

double_list_insert.c

#include"header.h"
    void insertfirst(node **head,int id,char *name)
    {
     node *tmp=(node *)malloc(sizeof(node));
     if(NULL == tmp)
     {
      printf("\nMemory allocation failed\n");
      exit(1);
     }
     tmp->id=id;
     tmp->name=name;
     tmp->prev=NULL;
     if(*head== NULL)
     {
      tmp->next=NULL;
      *head=tmp;
     }
     else
     {
      tmp->next=*head;
      (*head)->prev=tmp;
      *head=tmp;
     }
    }

    void insertlast(node **head,int id,char *name)
    {
     if(*head==NULL)
     {
      insertfirst(head,id,name);
      return;
     }
     node *last=*head;
     node *tmp=(node *)malloc(sizeof(node));
     if(NULL == tmp)
     {
      printf("\nMemory allocation failed\n");
      exit(1);
     }
     tmp->id=id;
     tmp->name=name;
     tmp->next=NULL;
     while(last->next!=NULL)
     {
      last=last->next;
     }
     last->next=tmp;
     tmp->prev=last;
    }

double_list_traverse.c

#include"header.h"
void traverse(node *head)
{
 node *tmp=head;
 if(head==NULL)
 {
  printf("\nList is empty\n");
  exit(1);
 }
 while(tmp!=NULL)
 {
  printf("%d --> %s\n",tmp->id,tmp->name);
  tmp=tmp->next;
 }
}

主文件来了,

ma​​in.c

#include"header.h"
int main()
{
 int choice;
 int id;
 char name[15];
 node *root=NULL;
 system("clear");
 while(1)
 {
  printf("\n1.Insert First\n");
  printf("\n2.Insert Last\n");
  printf("\n3.Traverse\n");
  printf("\n4.Exit\n");
  printf("\nEnter your choice : ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
        printf("\nEnter the employee id : ");
        scanf("%d",&id);
        printf("\nEnter the employee name : ");
        scanf("%s",name);
        insertfirst(&root,id,name);
        break;
   case 2:
        printf("\nEnter the employee id : ");
        scanf("%d",&id);
        printf("\nEnter the employee name : ");
        scanf("%s",name);
        insertlast(&root,id,name);
        break;

   case 3:
        traverse(root);
        break;
   case 4:
        return 0;
        break;
   default:
        printf("\nPlease enter valid choices\n");
  }
 }
}

在执行期间,如果我在第一个或最后一个只插入一个数据,它会正确地从我那里获取输入。

但是如果我插入第二个,就会出现问题。 就我而言,id 值保持不变。但是第二个输入的名称值在第一个值中重复。

为什么会发生这种情况?传递参数有什么问题吗?

最佳答案

创建新节点时,只需将指针复制到名称即可设置节点名称。您必须复制字符串而不是指针。 strdup函数非常适合这个:

tmp->name=strdup(name);

记住free释放节点时的名称。

编辑

当您调用 insertfirst 时会发生什么第一次,是name第一个节点的字段指向 name数组位于 main 。当您获取第二个节点的名称时,main 中的数组内容使用新名称进行更新,并且由于第一个节点中的指针指向该数组,因此该名称似乎是重复的。

关于c - 为什么我的双向链表的 C 实现会创建重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8340531/

相关文章:

c - 共享全局变量: interaction between main, lib.a和dll

java - 将有序二叉树转换为双循环链表

c++ - 双链表 - 不能删除第一个节点

c++ - 如何打印一个简单的链表(C++)?

algorithm - 带有一些操作的双向链表

c - 如何在 C 函数中定义一个数组?

python - 如何静态链接python解释器?

c++ - 增量后左值误差

c - 尝试找到 middel 但程序崩溃了 c 中带有两个指针的链表