新人求助;链表的一段小程序
求大家帮我看看这段程序哪里错了。程序有些长,不过都是最基本的知识。谭浩强的C语言教程我们还没学完呢。。谢谢了!!!程序的用途是输入学生学号,姓名,分数,并按照学号顺序排列。而且还可以随时对链表进行修改。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define NULL 0
struct student
{
int num;
char name[20];
float score;
struct student *next;
};
int n=0; //用来计算一共多少人
void main()
{
struct student* get();
struct student* insert(struct student *);
struct student* del(struct student *);
void print(struct student *);
void paixu(struct student *);
struct student *head;
char t;
int temp;
head=get();
paixu(*head);
printf("number name score\n");
print(head);
change:printf("<是否需要修改数据y/n>");
scanf("%c",&t);
if(t=='y')
{
printf("请选择修改的种类:\n1:插入新的数据 2:删除已有数据 3:放弃修改\n");
scanf("%d",&temp);
if(temp==1)
head=insert(head);
else if(temp==2)
head=del(head);
goto change;
}
printf("number name score\n");
print(head);
}
struct student *get()
{
struct student *head,*p1,*p2;
head=p1=p2=(struct student *)malloc(sizeof(struct student));
printf("如需结束,请在输入学号时输入:0\n");
do
{
p2=p1;
printf("请输入学号:");
scanf("%d",&p2->num);
if(p2->num==0)
goto loop;
printf("请输入姓名:");
scanf("%s",p2->name);
printf("请输入分数:");
scanf("%f",&p2->score);
n++;
p1=(struct student *)malloc(sizeof(struct student));
loop: p2->next=p1;
}
while(p2->num!=0);
p2->next=NULL;
return(head);
}
struct student *del(struct student *head)
{
struct student *p=head;
int temp;
printf("请输入需要删除人的学号:");
scanf("%d",&temp);
while(p->next!=NULL)
{
if(temp==p->num)
break;
else
p=p->next;
}
if(temp==p->num)
{
if(p==head) //删除第一个
head=head->next;
else if(p->next==NULL) //删除最后一个
(--p)->next=NULL;
else
(--p)->next=(++(++p));
n--;
}
else
printf("输入的学号有误!");
return(head);
}
struct student *insert(struct student *head)
{
struct student *p=(struct student *)malloc(sizeof(struct student));
struct student *p1=head,p2;
printf("请输入学号:");
scanf("%d",&p->num);
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入分数:");
scanf("%f",&p->score);
n++;
if(p->num<head->num) //插入的在链表头部
{
p-next=head;
head=p;
}
while(p->num>p1->num)
{
p2=p1;
p1=p1->next;
}
if(p1->next==NULL) //插入的在链表尾部
{
p1-next=p;
p->next=NULL;
}
else
{
p2->next=p;
p->next=p1;
}
return(head);
}
void print(struct student *head)
{
while(head->next!=NULL)
{
printf("%-10d%-20s%-10.1f\n",head->num,head->name,head->score);
head=head->next;
}
}
void paixu(struct student *head) //学号从小到大排
{
char mingzi[20];
int xuehao;
float fenshu;
struct student *p1,*p2;
p1=head;
for(;p1->next!=NULL;p1=p1->next)
for(p2=p1->next;p2->next!=NULL;p2=p2->next)
if(p1->num>p2->num)
{
strcmp(mingzi,p1->name);
strcmp(p1->name,p2->name);
strcmp(p2->name,mingzi);
xuehao=p1->num;
p1->num=p2->num;
p2->num=xuehao;
fenshu=p1->score;
p1->score=p2->score;
p1->score=fenshu;
}
}
如果可以长期进行辅导,请加我QQ;274862826,价格好商量。