引入
前面我们学习了头插法创建单链表,既然有头插法就有尾插法,跟头插法思路相同,尾插法是在表尾加入新结点,我们一起来看一下。
尾插法创建单链表
头插法学习完之后,我们发现输入和输出的内容是相反的?为什么呢?因为头插法是在链表的头部插入数据,先插入的数据在尾部,后插入的数据再头部,所以最终保存的链表顺序跟输入顺序是相反的。尾插法就不存在这个问题,输入顺序跟输出顺序相同。
代码实现
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student{
int num;
char name[20];
float score;
struct Student *next;
};
struct Student *create(void){
struct Student *a,*b,*head;
a=(struct Student *)malloc(LEN); //创建结点
head=(struct Student *)malloc(LEN); //创建头部
b=head; //b 指向尾巴,现在指向头部
printf("请输入学生信息:\n");
scanf("%d %s %f",&a->num,a->name,&a->score);
while(a->num!=0){
b->next=a; //b在队尾,b 的 next 指向尾巴 a
//也可以理解为:让 b 始终指向最后一个结点
b=a; //将 b 指向新节点,永远都指向新生成的节点
//也可以理解为:让 r 始终在最后
a=(struct Student *)malloc(LEN);
scanf("%d %s %f",&a->num,a->name,&a->score);
}
b->next=NULL;
return head;
}
void print(struct Student * p){
p=p->next;
while(p!=NULL){
printf("%d号%s的成绩是:%3.1f\n",p->num,p->name,p->score);
p=p->next;
}
}
int main(){
struct Student* stu_p;
stu_p=create();
print(stu_p);
return 0;
}

