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说明
已经到链尾了。