|
楼主 |
发表于 2018-2-22 19:35:59
From FishC Mobile
|
显示全部楼层
人造人 发表于 2018-2-22 15:59
把代码发完整
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n;
int main()
{
struct student *stu, *p, *stu_2;
int del_num;
printf("input records!\n");
stu=creat();
p = stu;
print(p);
printf("\n");
printf("please input the num to delete: ");
scanf("%d", &del_num);
while(del_num!=0)
{
p=del(p,del_num);
print(p);
printf("please input the num to delete: ");
scanf("%d", &del_num);
}
printf("\nplease input the record to insert: \n");
stu_2=(struct student * )malloc(LEN);
printf("please input the num : ");
scanf("%d",&stu_2->num);
printf("please input the score : ");
scanf("%f",&stu_2->score);
while(stu_2->num!=0)
{
p=insert(stu,stu_2);
print(p);
printf("\nplease input the record to insert: \n");
stu_2=(struct student * )malloc(LEN);
printf("please input the num : ");
scanf("%d",&stu_2->num);
printf("please input the score : ");
scanf("%f",&stu_2->score);
}
printf("\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
p1=p2=(struct student * )malloc(LEN);
printf("please input the num : ");
scanf("%d",&p1->num);
printf("please input the score : ");
scanf("%f",&p1->score);
printf("\n");
head=NULL;
n=0;
while(p1->num!=0)
{
n=n+1;
if(n==1)
{
head=p2;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student * )malloc(LEN);
printf("please input the num : ");
scanf("%d",&p1->num);
printf("please input the score : ");
scanf("%f",&p1->score);
printf("\n");
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n\n",n);
p=head;
if(head!=NULL)
{
do
{
printf("学号: %d 分数: %f \n", p->num, p->score);
p=p->next;
}while(p!=NULL);
}
}
struct student *del(struct student *head, int num)
{
struct student *p1, *p2;
if(head==NULL)
{
printf("\nList null\n");
goto end;
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("delete: No. %d succeed!\n", num);
n=n-1;
}
else
{
printf("%d not been found! \n", num);
}
end:
return head;
}
struct student *insert(struct student *head, struct student *stu_2) //插入函数
{
struct student *p0,*p1, *p2;
p1=head;
p0=stu_2;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while( (p0->num > p1->num) && (p1->next!=NULL) ) // 两种情况退出 while
{
p2=p1;
p1=p1->next;
}
if(p0->num <= p1->num)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else //p0的num最大,插在尾部
{
p1->next=p0;
p0->next=NULL;
}
n=n+1;
return head;
}
} |
|