在c语言链表中在为什么先要把链表的头节点和尾节点都置成NULL呢?

如题所述

不会吧?一般只会把尾节点置成NULL,
作为链表结束标志,头置成NULL,还
怎么进去啊。代码贴来研究一下。追问

struct stu{
int num;
char name[10];
struct stu *stu *next;
};
struct stu * create()
{
chan;
struct stu *head,*tail,*now;
head=NULL;
taik=NULL;

now=(struct stu *)malloc(sizeof(struct stu));
printf("学号:");
scanf("%d",&now->num);
printf("姓名:");
scanf("%s",now->name);
now->next=NUL;
}
tint main (void)
{
struct stu *s=NULL;
s=create();
}程序有点写不开所以只写了个大体意思

追答

你好,我个人觉得哈:
应该是这样,注释掉的是我加的。
struct stu{
int num;
char name[10];
struct stu *stu *next;
};
struct stu * create()
{
chan;
struct stu *head,*tail,*now;
head=NULL;
taik=NULL; //这个感觉没用

now=(struct stu *)malloc(sizeof(struct stu));
//head = now;
printf("学号:");
scanf("%d",&now->num);
printf("姓名:");
scanf("%s",now->name);
now->next=NUL;
//return now
}
tint main (void)
{
struct stu *s=NULL;
s=create();
}

追问

输入完姓名之后后面的那个now->next=NULL是什么意思呢?

追答

申明:个人意见
我觉得这个函数仅用于创建一个链表头,
那么现在这个now就是第一个也是最后
一个节点,最后一个节点的next指针
是要指向NULL的,来标识链表尾。

抱歉,好像我理解错了。
create()函数里head 和tail指针都没用啊,
因为当create()函数返回的时候,head
和tail都消亡了啊,而且在函数体内也
都没有用到他们。所以他们可以都去掉。
仅留一个now指针,用来返回链表头。

追问

那尾节点用什么来表示呢?又怎样来表示这个链表已经结束了呢?

追答

当遍历链表的时候,
就是判断每个节点
的next是否为NULL
如果为NULL说明
已经到链尾了。

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网