建立连表并输出,不知道为什么总是只输出第一个结构体后面就成了乱码
#include "stdio.h"#include "stdlib.h"
#define LEN sizeof(struct student)
#define NUL 0
struct student
{
char num[5];
char name[20];
float score;
struct student*next;
};
void print(struct student*p)
{
printf("连表是:\n");
printf("学号 姓名 成绩\n");
do
{printf("%5s%10s%10.2f\n",p->num,p->name,p->score);
p++;}while(p->next!=NUL);
}
struct student *creat(struct student*p,int n)
{
struct student *p1,*p2;
int N=1;
if(n==0)
p=NUL;
else
{p1=p;
do
{printf("第%d个学生:\n",N);
printf("学号:");
scanf("%s",&p1->num);
printf("姓名:");
scanf("%s",&p1->name);
printf("成绩:");
scanf("%f",&p1->score);
if(n==1)
p->next=NUL;
else
{p2=p1;
p1=(struct student*)malloc(LEN);
p2->next=p1;}
N++;} while(N<=n);
if(n!=1)
p2->next=NUL;}
return(p);
}
main()
{
struct student *pa,a;
int na;
pa=&a;
printf("链表结点数:");
scanf("%d",&na);
pa=creat(pa,na);
print(pa);
}
[[italic] 本帖最后由 llp108 于 2008-1-6 17:18 编辑 [/italic]]