我创建了一个列表,并通过 countnodes 函数检查节点是否已连接。 countnodes 函数给了我正确的答案,我认为一切都好。但是,当我尝试删除一个节点时,我意识到这些节点甚至没有连接到头部。我知道问题出在插入函数中,因为 cur 总是给出与零不同的东西,而插入函数返回零,并且节点永远不会相互连接。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int datum;
struct node *next;
};
struct node *search(struct node *head, int id, struct node **prev)
{
struct node *tmp = 0;
tmp = (struct node*)malloc(sizeof(struct node));
*prev = (struct node*)malloc(sizeof(struct node));
tmp = head;
printf("\n\ntmp->datum = %d",tmp->datum);
while(tmp!=NULL && tmp->datum < id)
{
*prev = tmp;
tmp = tmp->next;
}
if(tmp==NULL || tmp->datum != id)
{
return NULL;
}
return tmp;
};
int insert(struct node **H, struct node *tmp)
{
struct node *cur = 0 , *prev = 0;
// tmp = (struct node*)malloc(sizeof(struct node));
cur = (struct node*)malloc(sizeof(struct node));
printf("\n\ninsert datum = %d\n\n",tmp->datum);
cur = search(*H,tmp->datum,&prev);
if(cur) return 0;
printf("\nox\n");
if(prev==NULL)
{
printf("\nNULL\n");
tmp->next = *H;
*H = &tmp;
}
else
{
printf("\nELSE\n");
tmp->next = (prev->next);
prev->next = tmp;
}
return 1;
}
int delete(struct node **h,int price)
{
struct node *cur, *prev;
cur = (struct node*)malloc(sizeof(struct node));
cur = search(*h,price,&prev);
if(!cur) return 0;
if(prev)
{
prev->next = cur->next;
free(cur);
printf("\n\nsimple delete\n\n");
}
else
{
*h = cur->next;
free(cur);
printf("\n\nhead delete\n\n");
}
return 1;
}
int countnodes(struct node *h)
{
int n=0;
struct node *tmp = 0;
tmp = (struct node*)malloc(sizeof(struct node));
tmp = h;
while(tmp!=NULL)
{
n++;
printf("\n\ndatum = %d\n",tmp->datum);
tmp = tmp->next;
}
printf("\n\n\nNodes = %d\n",n);
return n;
}
int main()
{
struct node *head;
struct node *cur;
int i=0;
head = (struct node*)malloc(sizeof(struct node));
cur = (struct node*)malloc(sizeof(struct node));
head->datum = i;
head->next = NULL;
cur = head;
for(i=1;i<5;i++)
{
cur->next = malloc(sizeof(struct node));
insert(&head,cur);
cur = cur->next;
cur->datum = i;
cur->next = 0;
}
delete(&head,0);
//countnodes(head);
return 0;
}
最佳答案
我在您的代码中看到的问题:
在
搜索
中:tmp = (struct node*)malloc(sizeof(struct node)); *prev = (struct node*)malloc(sizeof(struct node));
这些是不必要的
malloc
。它们也是内存泄漏。将它们更改为:*prev = NULL;
在
中插入
:*H = &tmp;
这一行是错误的。两侧是不同的指针类型。也许这是一个错字。它需要是:
*H = tmp;
在
删除
中:cur = (struct node*)malloc(sizeof(struct node));
这也是不必要的
malloc
和内存泄漏。在
计数节点
中:tmp = (struct node*)malloc(sizeof(struct node));
这也是不必要的
malloc
和内存泄漏。在
主
中:cur = (struct node*)malloc(sizeof(struct node));
这也是不必要的
malloc
和内存泄漏。此外,以下行没有任何作用。
cur = head;
在
for
循环中,您有:cur->next = malloc(sizeof(struct node)); insert(&head,cur); cur = cur->next; cur->datum = i; cur->next = 0;
我认为您想要做的是添加一个
数据
为i
的节点。但是,您在节点上设置数据之前调用insert
。您需要的是:cur = malloc(sizeof(struct node)); cur->datum = i; cur->next = 0; insert(&head,cur);
希望我没有错过任何东西。
更新
我错过了另一个不必要的 malloc
和内存泄漏。您不需要在 insert
中添加以下行。
cur = (struct node*)malloc(sizeof(struct node));
关于c - 链表无法连接节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921063/