大一c语言链表错误怎么改?
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
int score;
struct student*next;
};
struct student*creat();
void display(struct student*head);
struct student*insert(struct student*head,struct student*newp);
struct student*deletep(struct student*head,long num);
choose();
void average(struct student*head);
void main()
{
struct student*creat();
void display(struct student*head);
printf("the menu choose:1 inset 2 delete 3 print");
choose();
average(struct student*creat());
}
int count=0;
struct student*creat()
{
struct student*head=NULL,*newp,*tail;
newp=tail=(struct student*)malloc(LEN);
printf("please input the data:\n");
scanf("%ld%s%d",&newp->num,newp->name,&newp->score);
while(newp->num!=0)
{
count++;
if(count==1)
head=newp;
else
tail->next=newp;
tail=newp;
newp=(struct student*)malloc(LEN);
scanf("%ld%s%d",&newp->num,newp->name,&newp->score);
}
tail->next=NULL;
return(head);
}
void display(struct student*head)
{
struct student*p=head;
while(p!=NULL)
{
printf("%ld\t%s\t%d\n",p->num,p->name,p->score);
p=p->next;
}
}
struct student*insert(struct student*head,struct student*newp)
{
struct student*p,*q;
p=q=head;
if(head==NULL)
{
head=newp;
newp->next=NULL;
}
else
{
while((newp->num>p->num)&&(p->num!=NULL))
{
q=p;
p=p->next;
}
if(newp->num<=p->num)
{
newp->next=p;
if(head==p)
head=newp;
else
q->next=newp;
}
else
{
p->next=newp;
newp->next=NULL;
}
}
count++;
return(head);
}
struct student*deletep(struct student*head,long num)
{
struct student*p,*q;
p=q=head;
while(p->num!=num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->num==num)
{
if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
count--;
}
else
printf("%ld cannot found\n",num);
return(head);
}
choose()
{
int i;
pirnf("your choice");
scanf("%d",&i);
switch(i)
{
case 1 :
struct student*insert(struct student*head,struct student*newp);
break;
case 2 :
struct student*deletep(struct student*head,long num);
break;
case 3 :void display(struct student*head);
break;
}
}
void average(struct student*head)
{
int sum=0;
int ave=0;
int n;
struct student*p;
p=head;
while(p->next!=NULL)
sum=sum+p->num;
printf("the number of date\n");
scanf("%d",&n);
ave=sum/n;
printf("%d",&ave);
}
求大神帮忙改一下,有错误
顺便将其排序