#include<stdio.h>
#include<stdlib.h>
/*
01 构建链表
02 初始化链表
03 输出链表
*/
typedef struct stu{
int num;
float score;
struct stu *next;
}STU,*PSTU;
PSTU create(){
PSTU head, p1, p2;
p2 = p1 = (PSTU)malloc(sizeof(STU));
//head = NULL;
printf("请输入节点编号:\n");
scanf("%d",&p1->num) ;
int n=0;
while (p1->num != 0){
n++;
printf("请输入节点num为%d的分数:\n",p1->num);
scanf("%f",&p1->score) ;
if(n==1)
head = p1;
else {
p2->next = p1; //指向尾节点
}
p2 = p1; //保持 p2一直是尾节点, p1是新加入节点
p1 = (PSTU)malloc(sizeof(STU));
printf("请输入节点编号:\n");
scanf("%d",&p1->num) ;
}
p2->next = NULL;
return head;
}
void print(PSTU p){
PSTU pstu;
pstu = p;
do{
printf("stu number: %d , score:%f\n",pstu->num, pstu->score);
pstu = pstu->next; //head->next = head
}while(pstu != NULL);
}
int main(){
PSTU pstu;
pstu = create();
print(pstu);
return 0;
}
C语言实现链表尾插法
猜你喜欢
转载自blog.csdn.net/qq_39463175/article/details/115343548
今日推荐
周排行