链表的创建有关问题
链表的创建问题
#define LEN sizeof(struct student)
#define NULL 0
struct student {
int score;
char no[10];
struct student *next;
};
struct student *creat(){
struct student *head, *newly, *tail;
int count = 0;
for ( ; ; ) {
newly = (struct student*) malloc (LEN);
cin >> newly->no;
if (strcmp (newly->no , "000000") == 0){
free (newly);
break;
}
cin >> newly->score;
count ++;
newly->next = NULL;
if (count == 1){
head = newly;
}
else
tail->next = newly;
tail = newly; //这一句不懂是什么意思,解释是:设置新的尾结点,但上一句不是把newly赋给tail->next了吗?
}
return head;
}
------解决方案--------------------
tail->next = newly;
// 是为了把链表连起来,让tail和最新的newly连起来;
tail = newly;
// 是为了让tail停到结尾,方便下回循环时连接新的newly;
------解决方案--------------------
执行tail->next = newly;时,tail还是原来链表的尾部,这句话的意思就是把原来链表的下一个指针指向现在的newly;
tail = newly;
这一句就是重新设定链表的尾部
------解决方案--------------------
#define LEN sizeof(struct student)
#define NULL 0
struct student {
int score;
char no[10];
struct student *next;
};
struct student *creat(){
struct student *head, *newly, *tail;
int count = 0;
for ( ; ; ) {
newly = (struct student*) malloc (LEN);
cin >> newly->no;
if (strcmp (newly->no , "000000") == 0){
free (newly);
break;
}
cin >> newly->score;
count ++;
newly->next = NULL;
if (count == 1){
head = newly;
}
else
tail->next = newly;
tail = newly; //这一句不懂是什么意思,解释是:设置新的尾结点,但上一句不是把newly赋给tail->next了吗?
}
return head;
}
------解决方案--------------------
tail->next = newly;
// 是为了把链表连起来,让tail和最新的newly连起来;
tail = newly;
// 是为了让tail停到结尾,方便下回循环时连接新的newly;
------解决方案--------------------
执行tail->next = newly;时,tail还是原来链表的尾部,这句话的意思就是把原来链表的下一个指针指向现在的newly;
tail = newly;
这一句就是重新设定链表的尾部
------解决方案--------------------