#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student *create();
void print(struct Student *head);
struct Student{
int num;
float score;
struct Student *next;
};
int n;
void main(){
struct Student *stu;
stu=create();
print(stu);
printf("\n\n");
system("pause");
}
struct Student *create(){
struct Student *head;
struct Student *p1,*p2;
p1=p2=(struct Student *) malloc(LEN);
printf("Please enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score");
scanf("%f",&p1->score);
head=NULL;
n=0;
while (p1->num!=0)
{
n++;
if(n==1){
head = p1;
}else{
p2->next=p1;
}
p2=p1;
p1=(struct Student *)malloc(LEN);
printf("\nPlease enter the num");
scanf("%d",&p1->num);
printf("Please enter the score");;
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct Student *head){
struct Student *p;
printf("\nTher are %d records!\n\n",n);
p=head;
if(head){
do{
printf("学号为%d的成绩是:%f",p->num,p->score);
p=p->next;
}while(p);
}
}
C语言创建动态链表
最新推荐文章于 2023-12-18 23:28:54 发布