您的位置:首页 > 其它

建立一个链表存放学生信息并打印信息

2014-04-01 22:29 387 查看
#include<stdio.h>
#include<stdlib.h>
#define len sizeof(pNode)

typedef struct student
{
long num;
float score;
struct student *next;
}*pNode;

int n;/*一个全局变量*/

pNode creat()
{
pNode head,p1,p2;
n=0;
p1=p2=(pNode)malloc(len);
head=NULL;
scanf("%ld %f",&p1->num,&p1->score);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(pNode)malloc(len);
scanf("%ld %f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

void print(pNode head)
{
pNode p;
printf("Now,these %d records are :\n",n);
p=head;
if(head!=NULL)
{
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
}

int main()
{
pNode head;
head=creat();
print(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 链表
相关文章推荐