关于c语言的链表的问题
#include<stdio.h>structbook{intdate;structbook*next;};structbook*creatlist(){structbo...
#include<stdio.h>
struct book
{
int date;
struct book *next;
};
struct book*creatlist()
{
struct book *head;
head=(struct book*)malloc(sizeof(struct book));
head->date=0;
head->next=0;
return head;
}
void addlist(struct book *head,int date1)
{
struct book *p,*q;
p=head;
while(p->next!=0)
{
p=p->next;
}
q=(struct book*)malloc(sizeof(struct book));
p->next=q;
q->date=date1;
}
main()
{
int i;
struct book *pcbook;
pcbook=creatlist();
for(i=1;i<5;i++)
{
addlist(pcbook,i);
}
for(i=1;i<5;i++)
{
pcbook=pcbook->next;
printf("%d\t\t%d\n",i,pcbook->date);
}
} 展开
struct book
{
int date;
struct book *next;
};
struct book*creatlist()
{
struct book *head;
head=(struct book*)malloc(sizeof(struct book));
head->date=0;
head->next=0;
return head;
}
void addlist(struct book *head,int date1)
{
struct book *p,*q;
p=head;
while(p->next!=0)
{
p=p->next;
}
q=(struct book*)malloc(sizeof(struct book));
p->next=q;
q->date=date1;
}
main()
{
int i;
struct book *pcbook;
pcbook=creatlist();
for(i=1;i<5;i++)
{
addlist(pcbook,i);
}
for(i=1;i<5;i++)
{
pcbook=pcbook->next;
printf("%d\t\t%d\n",i,pcbook->date);
}
} 展开
1个回答
展开全部
struct book*creatlist()
{
struct book *head;
head=(struct book*)malloc(sizeof(struct book));
head->date=0;
head->next=0;
return head;
}
这里返回空了
void addlist(struct book *head,int date1)
这里用到的q最后也是空,
还有使用指针前要确保他不为空
{
struct book *head;
head=(struct book*)malloc(sizeof(struct book));
head->date=0;
head->next=0;
return head;
}
这里返回空了
void addlist(struct book *head,int date1)
这里用到的q最后也是空,
还有使用指针前要确保他不为空
追问
什么返回空了?我的空了?还是应该空了?
追答
你在一个函数内申请空间,也就是head,他是局部变量。当这个函数调用完后,他对应的空间会自动释放。所以返回的head确切的说不是空,是根本是没初始化的指针
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询