c语言高手来
这个程序是用链表做一个学生学号的系统但是删除插入功能都不好用,请高手帮忙看下~谢谢#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define len sizeof(struct student)
int n=0;
struct student //结构体
{
float num;
struct student *next;
};
struct student *creat(void) //创建链表
{
struct student *p1,*p2,*head;
p1=p2=(struct student *)malloc(len);
head=NULL;
printf("请输入第%d名学生的学号:",n);
scanf("%f",&p1->num);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
p1=(struct student *)malloc(len);
p2->next=p1;
p2=p1;
printf("请输入第%d名学生的学号:",n);
scanf("%f",&p1->num);
}
p2->next=NULL;
return head;
}
struct student *del(struct student *head,struct student *stu) //删除链表
{
struct student *p1,*p2,*p;
p=stu;
if(head==NULL)
{
printf("系统中没有数据!\n");
return (head);
}
p1=head;
while((p1->num!=p->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p1->num==p->num)
{
if(p1->next==NULL)
head=p1->next;
else
p2->next=p1->next;
printf("删除%d学生数据!\n",p->num);
n=n-1;
}
else
printf("找不到该数据!\n");
return (head);
}
struct student *insert(struct student *head,struct student *stu) //插入链表
{
struct student *p,*p1,*p2;
p1=head;
p=stu;
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
while((p->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
if(p->num<=p1->num)
{
if(head==p1)
head=p;
else
p2->next=p;
p->next=p1;
}
else
{
p1->next=p;
p->next=NULL;
}
}
}
n=n+1;
return (head);
}
void menu(void)
{
printf("欢迎使用本系统!\n");
printf("1.输入数据\n");
printf("2.显示数据\n");
printf("3.删除数据\n");
printf("4.插入数据\n");
printf("5.退出本系统!\n");
printf("请做出选择!\n");
}
void display(struct student *head) //显示链表
{
struct student *p;
p=head;
printf("%d student record:\n",n);
if(head->next!=NULL)
do
{
printf("%5.1f\n",p->num);
p=p->next;
}
while(p->num!=0);
}
int main(void)
{
struct student *head,*stu;
int rev=0;
while(1)
{
menu();
scanf("%d",&rev);
switch (rev)
{
case 1:
head=creat();
break;
case 2:
display(head);
break;
case 3:
printf("请输入要删除学生的学号:");
scanf("%f",&stu->num);
head=del(head,stu);
display(head);
break;
case 4:
printf("请输入要插入学生的学号:");
stu=(struct student *)malloc(len);
scanf("%f",&stu->num);
head=insert(head,stu);
display(head);
break;
case 5:
printf("感谢使用本系统!\n");
return 0;
}
}
return 0;
}