链表释放空间

xidianxiancai 2009-03-23 03:32:12
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define elemtype int

typedef struct Node
{
elemtype data;
struct Node *next;
}Node;


int main(void)
{
Node *head, *p1, *p2, *p3;
p1 = p2 = (Node *)malloc(sizeof(Node *));
if (p1 == NULL)
{
printf("failure in memory.\n");
exit(0);
}
head = p1;
scanf("%d",&p1->data);
p1->next = NULL;
if (p1->data == 0)
{
printf("List is empty.\n");
return 0;
}
while (p2->data != 0)
{
if (!(p2 = (Node *)malloc(sizeof(Node *))))
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p2->data);
getchar();
p2->next = NULL;
p3 = p1;
p1->next = p2;
p1 = p1->next;
}
p3->next = NULL;
printf("List is made successfully.\n");
//以下是对该建立好的链表进行空间的释放,但是释放不成功,请问该如何改?如何进行释放呢?
p2 = head->next;
p1 = head;
free(p1);
head = NULL;
p1 = p2;
while (p1!=NULL)
{
p1 = p1->next;
free(p2);
p2 = p1;
}
return 0;
}
...全文
678 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyanchao 2009-03-23
  • 打赏
  • 举报
回复
没成功 所以没释放
ypb362148418 2009-03-23
  • 打赏
  • 举报
回复
你用malloc那就用free释放啊
sjhui19840325 2009-03-23
  • 打赏
  • 举报
回复
up 2楼写的精练



#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define elemtype int

typedef struct Node
{
elemtype data;
struct Node *next;
}Node;


int main(void)
{
Node *head, *p1, *p2; //一直没明白lz用p3做什么
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL)
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p1->data);
p1->next = NULL;
if (p1->data == 0)
{
printf("List is empty.\n");
free(p1); // 你在退出前要释放空间
return 0;
}
head = p1;
while (p1->data != 0)
{
if (!(p2 = (Node *)malloc(sizeof(Node))))
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p2->data);
getchar();
p2->next = NULL;
p1->next = p2;
p1 = p1->next;
}
printf("List is made successfully.\n");

while (head!=NULL)
{
p2 = head;
head = head->next;
free(p2);
}

return 0;
}
bfhtian 2009-03-23
  • 打赏
  • 举报
回复
分配内存就没有成功
liliangbao 2009-03-23
  • 打赏
  • 举报
回复
1.(Node *)malloc(sizeof(Node *)都要改为(Node *)malloc(sizeof(Node))
2.释放链表:
p1 = head;
while(p1 != NULL)
{
p2 = p1 -> next;
free(p1);
p1 = p2;
}
即可!
3.看一看如何建立链表的常用的两种方法:头插法和尾插法
比较与你的不同你会受益匪浅的呦!
KevinHo 2009-03-23
  • 打赏
  • 举报
回复
第一个问题,分配内存大小的计算是sizeof(Node),不是sizeof(Node*),因为你是给结构体变量指针分配指针!
另外你的编程逻辑真的不是太好……当然这只是给你提意见,总觉得你的思路不清晰
breezes2008 2009-03-23
  • 打赏
  • 举报
回复
释放链表:
struct node *p,*start,*temp;
for(p=start;p!=NULL;p=temp)
{
temp=p->next;
free(p);
}

====================================================================
常见这样“释放”:for(p=start;p!=NULL;p=p->next) //错误,发生内存泄漏.
fengogo 2009-03-23
  • 打赏
  • 举报
回复
p1 = p2 = (Node *)malloc(sizeof(Node *)); -> p1 = (Node *)malloc(sizeof(Node));

do
{
if (!(p2 = (Node *)malloc(sizeof(Node))))
{
printf("failure in memory.\n");
exit(0);
}
scanf("%d",&p2->data);
getchar();
p2->next = NULL;
p1->next = p2;
p1 = p1->next;
} while (p2 -> data != 0);

p1 = head;
while(p1 != NULL)
{
p2 = p1 -> next;
free(p1);
p1 = p2;
}
head = NULL;
ainijing 2009-03-23
  • 打赏
  • 举报
回复
void cleardata()
{
Node *tmp;
while( tableinfo!= NULL )
{
tmp=( Node )tableinfo->np;
free( tableinfo );
tableinfo = tmp;
}
}

70,018

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧