标题:[求助]不知道怎么说~~~
取消只看楼主
kingcat_1985
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2004-11-7
 问题点数:0 回复次数:2 
[求助]不知道怎么说~~~

这段程序:

#include "stdio.h" #include "stdlib.h" #include "malloc.h" #define ST struct student

struct student {int info; struct student *link; }; struct student *del(struct student *head, int num) { struct student *p1,*p2=0; if(!head) printf("\nlist null!\n"); else { p1=head; while((p1->info!=num)&&p1) { p2=p1; p1=p1->link; }

printf("line 25: %d,%d\n",p2->info,p1->info);

if(p1->info==num) { printf("num==p1->info\n"); printf("delete:%d\n",num); if(p1==head) { printf("p1==head\n");

head=p1->link;

free(p1); num==0; /*如果不加这一句的话,num的值会无端端变成0*/ } else { printf("p1!=head\n");

(p2->link)=(p1->link);

p2=head; while(p2) { printf("line 50: %d\n",p2->info); p2=p2->link; }

free(p1);

putchar('\n'); p2=head; while(p2) { printf("line 59: %d\n",p2->info); /*如果输入2,这里会死循环;如果输入3、4、5,这里的数据会出错。*/ p2=p2->link; }

} printf("delete:%d\n",num);

} else printf("%d not been found!\n",num); } return (head); } main() { ST k[5],*p=k; int i;

for(i=0;i<5;i++) { k[i].info=i+1; k[i].link=&k[i+1]; } k[i-1].link=0;

while(p) { printf("line 86: %d\n",p->info); p=p->link; }

p=k;

printf("请输入要删除的节点\n"); scanf("%d",&i); p=del(p,i); while(p) { printf("%d\n",p->info); p=p->link; } getch(); }

如果不使用free()函数,就没有问题,可是怎么可能不用它来释放空间的呢?很头疼~~~各位高手帮帮忙~~~
搜索更多相关主题的帖子: include null 
2005-06-23 11:39
kingcat_1985
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2004-11-7
得分:0 
啊?我没有用free()来释放k[]的空间啊!我是在自定义的del()里面用free()来释放*p1的空间啊~~~
大哥~~~求求你可不可以解释清楚一点~~~我理解不了~~~

我是菜鸟~~~   各位大哥要照住小弟我啊~~~
2005-06-24 18:26
kingcat_1985
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2004-11-7
得分:0 
原来是静态链表不可以这样删的~~~可是~~~新的问题又来了~~~ [QUOTE]#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define ST struct student #define SIZE sizeof(ST) struct student {int info; struct student *link; }; struct student *del(struct student *head, int num) { struct student *p1,*p2=head; if(!head) printf("\nlist null!\n"); else { p1=head; while((p1->info!=num)&&p1->link!=NULL) { p2=p1; p1=p1->link; } if(p1->info==num) { if(p1==head) { head=p1->link; free(p1); } else { p2->link=p1->link; free(p1); } printf("delete:%d\n",num); } else printf("%d not been found!\n",num); } return (head); } struct student *cre(struct student *head,int n) { int i; struct student *p; if(n>0) {if(!head) { head=(struct student *)calloc(n,SIZE); p=head; } else { p=head; while(p->link)p=p->link; p->link=(struct student *)calloc(n,SIZE); p=p->link; } printf("Insert datas:\n"); for(i=1;i<=n;i++) { scanf("%d",&(p->info)); if(n-i) { p->link=(p+1); p=p->link; } else p->link=0; }} return head; } main() { ST *p=0; printf("Insert the amount of records:\n"); p=cre(p,(getchar()-48)); printf("Insert the num you want to del:\n"); p=del(p,(getchar()-48)); /*这里的getch()为何不起作用?*/ while(p) { printf("%d\n",p->info); p=p->link; } getch(); }[/QUOTE] 麻烦各位大哥~~~

[此贴子已经被作者于2005-6-24 21:31:13编辑过]


我是菜鸟~~~   各位大哥要照住小弟我啊~~~
2005-06-24 21:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-21414-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.634130 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved