|
发表于 2015-5-25 21:37:52
|
显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
int n=0;
//学生结构体(学生号,成绩)
struct student
{
long num;
float score;
struct student *next;
};
// 链表输出函数
void OutputLinklist(student* phead)
{
while (phead)
{
printf("num is %ld\n", phead->num);
printf("scord is %f\n\n", phead->score);
phead = phead->next;
}
}
int main()
{
struct student *head,*p1,*p2;//*p2是表尾
head=NULL;
p1=p2=(struct student *)malloc(LEN);
//*p1结点输入
printf("Please input num: ");
scanf("%ld",&p1->num);
printf("Please input score: ");
scanf("%f",&p1->score);
while(p1->num)//以num=0作结束标志
{
n=n+1;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
p2=p1;
}
//结点输入
p1=(struct student *)malloc(LEN);
printf("Please input num: ");
scanf("%ld",&p1->num);
printf("Please input score: ");
scanf("%f",&p1->score);
}
p2->next=NULL;
OutputLinklist(head);
free(p1) ;
free(p2) ;
return 0;
} |
|