#include
#define LEN sizeof(STU)
typedef struct student
{
int num;
float score;
struct *next;
}STU;
STU *creat();//创建一个链表
void print(STU *head);//打印一个链表
STU *del(STU *head,int num);//删除一个链表
STU *insert(STU *head,STU stu2);//插入一个链表
int n=0;
void main()
{
STU *stu,*stu2,*p;
int n;
stu=creat();
p=stu;
print(p);
printf("请输入要删除的学号:");
scanf("%d",&n);
print(del(p,n));
printf("请输入要插入的学号:");
scanf("%d",&stu2->num);
printf("请输入要插入的成绩:");
scanf("%f",&stu2->score);
p = insert(stu,&stu2);
print(p);
printf("\n");
}
STU *creat()
{
STU *head,*p1,*p2;
head=NULL;
p1=p2=(STU *)malloc(LEN);
printf("输入信息:");
scanf("%d %f",&p1->num,&p1->score);
while(p1->num!=0)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(STU *)malloc(LEN);
printf("请输入信息:");
scanf("%d %f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void print(STU *head)
{
STU *p;
p = head;
if(head)
{
do
{
printf("%d %.1f\n",p->num,p->score);
p=p->next;
}
while(p);
}
}
STU *del(STU *head,int num)
{
STU *p1,*p2;
if(head == NULL)
{
printf("表为空");
exit(0);
}
else
{
p1=head;
while(p1->num != num && p1->next != NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num == num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next=p1->next;
}
n=n-1;
printf("学号删除成功!\n");
}
else
{
printf("找不到信息!\n");
}
}
return head;
}
STU *insert (STU *head,STU *stu2)
{
STU *p0,*p1,*p2;
p1=head;
p0=stu2;
if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while( p0->num > p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p0->num <= p1->num)
{
if(p1 == head)
{
head=p0;
p0->next=p1;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}