链表题,谁给续写一下

来源:百度知道 编辑:UC知道 时间:2024/06/12 04:27:47
编写一个建立单向链表的函数,存放学生数据
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct stu)

struct stu
{
long int num;
float score;
struct stu *next;
};

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

struct stu //定义存储数据节点
{
long int num; //应该是学号
float score; //分数
struct stu *next; //指向下一个存储节点的指针
};

int n;
struct stu *creat(void) //创建链表
{
struct stu *head,*p1,*p2; //head头节点指针,p2为尾指针,p1为当前节点指针
n=0;
p1=p2=(struct stu*)malloc(LEN); //动态分配存储空间,第一个节点
scanf("%ld,%f",&p1->num,&p1->score); //输入数据
head=NULL;
while (p1->num!=0) //遍历链表,将建立的节点即p1指向的节点,新加入到链表尾端
{
n=n+1; //节点数自增1
if(n==1)head=p1; //若n为1表示,即链表中还未加入节点
else p2->next=p1;
p2=p1;
p1=(struct stu *)malloc(LEN); //建立新节点
scanf("%ld,%f",&p1->num,&p1->score); //输入数据
}
p2->next=NULL;
return(head);
}

基本正确,只需将"int n; "放入定义的函数中即可.

直接说你要实现什么完整功能的程序