C 链表可以';t连接节点
我创建了一个列表,并通过countnodes函数检查节点是否已连接。countnodes函数给了我正确的答案,我认为一切都很好。但是,当我试图删除一个节点时,我意识到这些节点甚至没有连接到头部。我知道问题出在insert函数中,因为cur总是给出与零不同的东西,insert函数返回零,并且节点从不相互连接C 链表可以';t连接节点,c,list,C,List,我创建了一个列表,并通过countnodes函数检查节点是否已连接。countnodes函数给了我正确的答案,我认为一切都很好。但是,当我试图删除一个节点时,我意识到这些节点甚至没有连接到头部。我知道问题出在insert函数中,因为cur总是给出与零不同的东西,insert函数返回零,并且节点从不相互连接 #include <stdio.h> #include <stdlib.h> #include <string.h> struct node {
#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;
}
#包括
#包括
#包括
结构节点
{
int基准;
结构节点*下一步;
};
结构节点*搜索(结构节点*头,int-id,结构节点**prev)
{
结构节点*tmp=0;
tmp=(结构节点*)malloc(sizeof(结构节点));
*prev=(结构节点*)malloc(sizeof(结构节点));
tmp=头部;
printf(“\n\ntmp->datum=%d”,tmp->datum);
while(tmp!=NULL&&tmp->datumnext;
}
如果(tmp==NULL | | tmp->datum!=id)
{
返回NULL;
}
返回tmp;
};
int insert(结构节点**H,结构节点*tmp)
{
结构节点*cur=0,*prev=0;
//tmp=(结构节点*)malloc(sizeof(结构节点));
cur=(结构节点*)malloc(sizeof(结构节点));
printf(“\n\n插入数据=%d\n\n”,tmp->datum);
cur=搜索(*H,tmp->基准和上一步);
如果(cur)返回0;
printf(“\nox\n”);
if(prev==NULL)
{
printf(“\nNULL\n”);
tmp->next=*H;
*H=&tmp;
}
其他的
{
printf(“\nELSE\n”);
tmp->next=(上一个->下一个);
上一个->下一个=tmp;
}
返回1;
}
整数删除(结构节点**h,整数价格)
{
结构节点*cur,*prev;
cur=(结构节点*)malloc(sizeof(结构节点));
cur=搜索(*h、价格和上一个);
如果(!cur)返回0;
如果(上一个)
{
上一步->下一步=当前->下一步;
免费(cur);
printf(“\n\n简单删除\n\n”);
}
其他的
{
*h=当前->下一步;
免费(cur);
printf(“\n\n头删除\n\n”);
}
返回1;
}
int countnodes(结构节点*h)
{
int n=0;
结构节点*tmp=0;
tmp=(结构节点*)malloc(sizeof(结构节点));
tmp=h;
while(tmp!=NULL)
{
n++;
printf(“\n\n数据=%d\n”,tmp->datum);
tmp=tmp->next;
}
printf(“\n\n\n节点=%d\n”,n);
返回n;
}
int main()
{
结构节点*头部;
结构节点*cur;
int i=0;
head=(结构节点*)malloc(sizeof(结构节点));
cur=(结构节点*)malloc(sizeof(结构节点));
水头->基准=i;
head->next=NULL;
cur=头部;
对于(i=1;inxt=malloc(sizeof(struct node));
插入(&head,cur);
cur=cur->next;
cur->datum=i;
cur->next=0;
}
删除(&head,0);
//计数节点(头);
返回0;
}
我在您的代码中看到的问题:
搜索中
:
tmp = (struct node*)malloc(sizeof(struct node));
*prev = (struct node*)malloc(sizeof(struct node));
这些是不必要的malloc
s。它们也是内存泄漏。将它们更改为:
*prev = NULL;
中插入:
*H = &tmp;
这行是错误的。两边是不同的指针类型。这可能是一个打字错误。它需要是:
*H = tmp;
删除中
:
cur = (struct node*)malloc(sizeof(struct node));
这也是不必要的malloc
和内存泄漏countnodes
中:
tmp = (struct node*)malloc(sizeof(struct node));
这也是不必要的malloc
和内存泄漏main
中:
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));
如几条注释所示,问题代码揭示了对malloc()和指针的不正确理解 下面是问题代码的表示形式,带有一个
malloc()
:
#包括
#包括
#包括
结构节点
{
int基准;
结构节点*下一步;
};
结构节点*搜索(结构节点*cur,int-id,结构节点**prev)
{
while(cur&&cur->datumnext;
}
如果(当前)
如果(当前->基准!=id)
cur=NULL;
返回(cur);
};
整数删除(结构节点**头,整数价格)
{
结构节点*cur,*prev=NULL;
cur=搜索(*标题、价格和上一页);
if(NULL==cur)
返回0;
如果(上一个)
{
上一步->下一步=当前->下一步;
免费(cur);
}
其他的
{
*head=cur->next;
免费(cur);
}
返回1;
}
int countnodes(结构节点*tmp)
{
int n=0;
对于(;tmp;tmp=tmp->next)
n++;
返回(n);
}
int insert(结构节点**头,结构节点*新)
{
结构节点*cur=NULL;
结构节点*prev=NULL;
如果(*标题)
{
cur=搜索(*头部、新->基准面和上一个);
如果(当前)
返回(0);
}
如果(上一个)
{
新建->下一步=上一步->下一步;
上一步->下一步=新建;
}
其他的
{
新建->下一步=*头部;
*头=新的;
}
申报表(1);
}
int main()
{
结构节点*head=NULL;
int i;
对于(i=0;idatum=i;
新建->下一步=空;
printf(“插入数据=%d\n”,新建->数据);
插入(&头,新);
}
删除(&head,0);
printf(“countnodes=%d\n”,countnodes(head));
返回0;
}
轻松使用malloc
。您的程序中有八个内存分配实例。应该只有一个实例,即创建新节点时。在其他情况下,您的指针应该指向现有节点或为NULL
。这些指针构成了列表中的链接。我知道您是对的,但如果我愿意的话将malloc从机器上拆下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int datum;
struct node *next;
};
struct node *search(struct node *cur, int id, struct node **prev)
{
while(cur && cur->datum < id)
{
*prev = cur;
cur = cur->next;
}
if(cur)
if(cur->datum != id)
cur=NULL;
return(cur);
};
int delete(struct node **head, int price)
{
struct node *cur, *prev=NULL;
cur = search(*head, price, &prev);
if(NULL == cur)
return 0;
if(prev)
{
prev->next = cur->next;
free(cur);
}
else
{
*head = cur->next;
free(cur);
}
return 1;
}
int countnodes(struct node *tmp)
{
int n=0;
for(;tmp; tmp = tmp->next)
n++;
return(n);
}
int insert(struct node **head, struct node *new)
{
struct node *cur = NULL;
struct node *prev = NULL;
if(*head)
{
cur = search(*head, new->datum, &prev);
if(cur)
return(0);
}
if(prev)
{
new->next = prev->next;
prev->next = new;
}
else
{
new->next = *head;
*head = new;
}
return(1);
}
int main()
{
struct node *head = NULL;
int i;
for(i=0;i<5;i++)
{
struct node *new = malloc(sizeof(*new));
if(NULL == new)
{
fprintf(stderr, "malloc() failed\n");
exit(1);
}
new->datum = i;
new->next = NULL;
printf("insert datum = %d\n", new->datum);
insert(&head, new);
}
delete(&head,0);
printf("countnodes = %d\n", countnodes(head));
return 0;
}