尝试在第一个元素之前插入节点时,c中的单链表不起作用

尝试在第一个元素之前插入节点时,c中的单链表不起作用,c,linked-list,C,Linked List,在我使用下面的insertBefore()函数之前,C中的单链表实现工作正常。在insertBefore()中,当我尝试在第一个元素之前插入节点时,发生了一件奇怪的事情。我尝试从insertBefore()内部打印列表,它正确地打印了列表。但是当我试图先回来的时候,似乎有些事情不对。因为在返回main之后,当我尝试打印相同的列表时,它将进入无限循环。关键是,在返回main之后,当我尝试先打印value.next->data时,它显示的值与first.data的值相同。这段代码适用于所有其他情况,

在我使用下面的insertBefore()函数之前,C中的单链表实现工作正常。在insertBefore()中,当我尝试在第一个元素之前插入节点时,发生了一件奇怪的事情。我尝试从insertBefore()内部打印列表,它正确地打印了列表。但是当我试图先回来的时候,似乎有些事情不对。因为在返回main之后,当我尝试打印相同的列表时,它将进入无限循环。关键是,在返回main之后,当我尝试先打印value.next->data时,它显示的值与first.data的值相同。这段代码适用于所有其他情况,比如insertAfter()甚至insertbefore(),当我尝试在除第一个节点之前之外的任何其他位置插入节点时

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
   int data;
   node *next;
};

void printList(node *);

node * insertFirst(node *first, int x)
{
 node *ptr = (node *)malloc(sizeof(node));
 ptr->data = x;
 ptr->next = NULL;
 first = ptr;
 return first;
}

node *insertAfter(node *first, int x, int k)
{
  node *p = first;
  node *ptr = (node *)malloc(sizeof(node));
  ptr->data = x;

  while(p != NULL)
  {

         if(p->data == k)
                    break;
         p = p->next;       
  }

  if(p == NULL)
      printf("Element not found\n");
  else
  {
     ptr->next = p->next;
     p->next = ptr;
  }
  printList(first);
  return first;
}

node *insertBefore(node *first, int x, int k)
{
  node *ptr = (node *)malloc(sizeof(node));
  ptr->data = x;
  node *p = first, *follow = NULL;
  while(p != NULL)
  {
         if(p->data == k)
                    break;
         follow = p;
         p = p->next;
  }
  if(p == NULL)
      printf("Element not found\n");
  else
  {
     if(p == first)
     {
          ptr->next = first;
          first = ptr;
     }
     else
     {
         ptr->next = p;
         follow->next = ptr;
     }     
  }
  printList(first);
  printf("first->nxt %u", first->next->data);
  return first;

}

void printList(node *first)
{
  node *p = first;
  while(p != NULL)
  {
         printf(" %d",p->data);
         p = p->next;
  }
  printf("\n");
}

main()
{
  struct node first, *p;

  int i, x, y, t=1;

  while(t)
  {
          printf("1:insertFirst 2:insertAfter 3:insertBefore 4:printList 5:exit\n");
          scanf("%d", &i);
          switch(i)
          {
                   case 1:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        first = *insertFirst(&first, x);

                        break;
                   case 2:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        printf("Enter element after which to insert node\n");
                        scanf("%d", &y);
                        first = *insertAfter(&first, x, y);
                        break;
                   case 3:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        printf("Enter element before which to insert node\n");
                        scanf("%d", &y);
                        first = *insertBefore(&first, x, y);
                        printf("first: %d", first.data);  
                        printf("first.nxt %d", first.next->data);

                        printList(&first);                     
                        break;

                   case 4:
                        printf("Linked list:");
                        printList(&first);
                        break;             
                   case 5:
                        t = 0;
                        break;
          }

  }
  getch();
}
#包括
#包括
#包括
结构节点
{
int数据;
节点*下一步;
};
作废打印列表(节点*);
节点*insertFirst(节点*first,int x)
{
node*ptr=(node*)malloc(sizeof(node));
ptr->data=x;
ptr->next=NULL;
第一个=ptr;
先返回;
}
节点*insertAfter(节点*first,int x,int k)
{
节点*p=第一;
node*ptr=(node*)malloc(sizeof(node));
ptr->data=x;
while(p!=NULL)
{
如果(p->data==k)
打破
p=p->next;
}
if(p==NULL)
printf(“未找到元素\n”);
其他的
{
ptr->next=p->next;
p->next=ptr;
}
打印列表(第一);
先返回;
}
节点*insertBefore(节点*first,int x,int k)
{
node*ptr=(node*)malloc(sizeof(node));
ptr->data=x;
节点*p=first,*follow=NULL;
while(p!=NULL)
{
如果(p->data==k)
打破
follow=p;
p=p->next;
}
if(p==NULL)
printf(“未找到元素\n”);
其他的
{
如果(p==第一个)
{
ptr->next=第一;
第一个=ptr;
}
其他的
{
ptr->next=p;
follow->next=ptr;
}     
}
打印列表(第一);
printf(“第一个->nxt%u”,第一个->下一个->数据);
先返回;
}
无效打印列表(节点*第一个)
{
节点*p=第一;
while(p!=NULL)
{
printf(“%d”,p->data);
p=p->next;
}
printf(“\n”);
}
main()
{
首先是结构节点,*p;
int i,x,y,t=1;
while(t)
{
printf(“1:insertFirst 2:insertAfter 3:insertBefore 4:printList 5:exit\n”);
scanf(“%d”、&i);
开关(一)
{
案例1:
printf(“输入要插入的元素\n”);
scanf(“%d”和&x);
first=*insertFirst(&first,x);
打破
案例2:
printf(“输入要插入的元素\n”);
scanf(“%d”和&x);
printf(“输入要插入节点的元素\n”);
scanf(“%d”和“&y”);
first=*插入后面的(&first,x,y);
打破
案例3:
printf(“输入要插入的元素\n”);
scanf(“%d”和&x);
printf(“输入要插入节点的元素\n”);
scanf(“%d”和“&y”);
first=*插入前(&first,x,y);
printf(“第一个:%d”,第一个.data);
printf(“first.nxt%d”,first.next->data);
打印列表(&first);
打破
案例4:
printf(“链接列表:”);
打印列表(&first);
打破
案例5:
t=0;
打破
}
}
getch();
}

我建议首先将
转换为指针(
node*
),然后重新访问所有您执行以下操作的地方:

first = *insertBefore(&first, x, y);
应改为:

first = insertBefore(first, x, y);
否则,您将从左到右和从中央泄漏内存,并创建无限循环的可能性(其中
first.next
指向
first

您还需要确保正确初始化了
first
,并将
first.X
的所有用法替换为
first->X

<强>编辑< /强>:考虑以下代码:

node * insertFirst(node *first, int x)
{
 node *ptr = (node *)malloc(sizeof(node));
 ...
 return ptr;
}

struct node first;
...
first = *insertFirst(&first, x);
insertFirst()
的结果被取消引用,返回的结构被复制到
first
。一旦赋值完成执行,
malloc()
ed指针将永远丢失


类似的内存泄漏也会影响其他功能。

我认为
insertFirst
应该在
first
之前插入新节点。但是,这不是因为新节点的
next
指针应该指向旧的第一个节点

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
   int data;
   node *next;
};

void printList(node *);

node * insertFirst(node *first, int x)
{
 node *ptr = (node *)malloc(sizeof(node));
 ptr->data = x;
 ptr->next = NULL;
 first = ptr;
 return first;
}

node *insertAfter(node *first, int x, int k)
{
  node *p = first;
  node *ptr = (node *)malloc(sizeof(node));
  ptr->data = x;

  while(p != NULL)
  {

         if(p->data == k)
                    break;
         p = p->next;       
  }

  if(p == NULL)
      printf("Element not found\n");
  else
  {
     ptr->next = p->next;
     p->next = ptr;
  }
  printList(first);
  return first;
}

node *insertBefore(node *first, int x, int k)
{
  node *ptr = (node *)malloc(sizeof(node));
  ptr->data = x;
  node *p = first, *follow = NULL;
  while(p != NULL)
  {
         if(p->data == k)
                    break;
         follow = p;
         p = p->next;
  }
  if(p == NULL)
      printf("Element not found\n");
  else
  {
     if(p == first)
     {
          ptr->next = first;
          first = ptr;
     }
     else
     {
         ptr->next = p;
         follow->next = ptr;
     }     
  }
  printList(first);
  printf("first->nxt %u", first->next->data);
  return first;

}

void printList(node *first)
{
  node *p = first;
  while(p != NULL)
  {
         printf(" %d",p->data);
         p = p->next;
  }
  printf("\n");
}

main()
{
  struct node first, *p;

  int i, x, y, t=1;

  while(t)
  {
          printf("1:insertFirst 2:insertAfter 3:insertBefore 4:printList 5:exit\n");
          scanf("%d", &i);
          switch(i)
          {
                   case 1:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        first = *insertFirst(&first, x);

                        break;
                   case 2:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        printf("Enter element after which to insert node\n");
                        scanf("%d", &y);
                        first = *insertAfter(&first, x, y);
                        break;
                   case 3:
                        printf("Enter element to be inserted\n");
                        scanf("%d", &x);
                        printf("Enter element before which to insert node\n");
                        scanf("%d", &y);
                        first = *insertBefore(&first, x, y);
                        printf("first: %d", first.data);  
                        printf("first.nxt %d", first.next->data);

                        printList(&first);                     
                        break;

                   case 4:
                        printf("Linked list:");
                        printList(&first);
                        break;             
                   case 5:
                        t = 0;
                        break;
          }

  }
  getch();
}
请尝试以下方法:

node * insertFirst(node *first, int x)
{
    node *ptr = (node *)malloc(sizeof(node));
    ptr->data = x;
    ptr->next = first;  /* The old first node is now second first */
    return ptr;  /* Return the new first node */
}

之前的插入内容应如下所示。假设您将
head
维护为全局,并将新节点的
head
节点和
data
作为参数传递

NODE * addfront(NODE *head, int data)
{
        NODE * newnode = (NODE *) malloc(sizeof(NODE));
        newnode->data = data;
        newnode->next = head;

        head = newnode;
        return head;
}
上述代码应按如下方式使用:

NODE * head = NULL;
head = addfront(head, 9);

非常感谢。成功了。但你能详细解释一下“你在左、右、中泄漏内存”是什么意思吗?到底为什么是第一个。下一个在返回main后指向第一个。因为当我通过先传递从insertBefore()函数打印列表时,它确实正确地打印了列表。