|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.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;
void main()
{
struct student *stu,*p,stu_2;
int n;
stu=creat();
p=stu;
print(p);
printf("please enter the num to delete:");
scanf("%d",&n);
print(del(p,n) );
printf("\nplease input the num to insert:\n");
scanf("%d",stu_2.num);
printf("please input the score :");
scanf("%f",&stu_2.score);
p=insert(stu,&stu_2);
print(p);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("输入9999结束\n\n");
printf("please enter the num:");
scanf("%d",&p1->num);
while(999>p1->num||p1->num>10000)
{ printf("学号为4位,请重新输入\n");
system("pause");
//system("cls");
printf("please enter the num:");
scanf("%d",&p1->num);
if(p1->num>999&&p1->num<10000)
break;
system("cls");
}
printf("please enter the score:");
scanf("%f",&p1->score);
head=NULL;
n=0;
while(p1->num!=9999)
{
n++;
if(1==n)
{
head=p1;
}else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("\nplease enter the num :");
scanf("%d",&p1->num);
while(999>p1->num||p1->num>10000)
{ printf("学号为4位,请重新输入\n");
system("pause");
//system("cls");
printf("please enter the num:");
scanf("%d",&p1->num);
if(p1->num>999&&p1->num<10000)
break;
system("cls");
}
printf("please enter the score :");
scanf("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\n there are(is) %d record(s)\n\n",n);
p=head;
if(head!=NULL)
{
do
{
printf("学号 %d 的成绩是: %.2f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
}
struct student *del(struct student *h ,int num)
{
struct student *p1,*p2;
struct student *head=h;
if(NULL==head)
{
printf("\nthis list is NULL\n");
goto END;
}
p1=head;
while(p1->num!=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("\ndelete 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))
{
p2=p1;
p1=p1->next;
}
if(p0->num <= p1->num)
{
if(head == p1)
{
head=p0;
}
else
{
p2->next = p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
} |
|