|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stu)
#define NULL 0
struct stu
{
int num; //学号
char name[20];//名字
char sex; //性别
float score; //成绩
float height;//身高
float weight;//体重
struct stu *next;
};
int n;
struct stu *charu(struct stu *head,struct stu *pn);//插入链表
struct stu *creat();//创建一个链表
void print(struct stu *head);//输出函数
struct stu *shanchu(struct stu *head,int del);//删除链表节点函数
void main()
{
struct stu *head,*p1;
int del;
printf("请输入学生的:学号,名字,性别,成绩,身高,体重\n");
head=creat();//创建一个链表
print(head);//
printf("请输入你要删除数据:\n");
scanf("%d",&del);
while(del!=0)
{
head=shanchu(head ,del);
print(head);
printf("请输入你要删除数据:\n");
scanf("%d",&del);
}
printf("请输入你要插入数据\n");
p1=(struct stu *)malloc(LEN);
scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);
while(p1->num!=0&&p1->height!=0&&p1->weight)
{
head=charu(head,p1);
print(head);
printf("请输入你要插入节点\n");
p1=(struct stu *)malloc(LEN);
scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);;
}
}
struct stu *creat()//创建一个链表
{
struct stu *p1,*p2,*head;
p1=p2=(struct stu *)malloc(LEN);
n=0;
scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);
head=NULL;
while(p1->num!=0&&p1->height!=0&&p1->weight!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stu *)malloc(LEN);
scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);
}
p2->next=NULL;
return head;
}
void print(struct stu *head)//打印函数的输出
{
struct stu *p1;
p1=head;
printf("\nNow ,These %d records are :\n",n);
while(p1!=NULL)
{
printf("%d,%5s,%5c,%5f,%5f,%5f\n",p1->num,p1->name,p1->sex,p1->score,p1->height,p1->weight);
p1=p1->next;
}
}
struct stu *shanchu(struct stu *head,int del)//删除链表节点函数
{
struct stu *p1,*p2;
p1=head;
if(head==NULL)
{
printf("找不到该数\n");
goto end;
}
while(p1->next!=NULL&&del!=p1->num)
{
p2=p1;
p1=p1->next;
}
if(del==p1->num)
{
if(head==p1)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
printf("delete:%d,%d",del,p1->num);
}
else
printf("找不到你要删除的数据\n");
end:
return head;
}
struct stu *charu(struct stu *head,struct stu *pn)
{
struct stu *p1,*p2;
p1=head;
if(head==NULL)
{
head=pn;
pn->next=NULL;
}
if(pn->num<=head->num)
{
pn->next=head;
head=pn;
}
while(p1->next!=0&&pn->num<=p1->num)
{
p2=p1;
p1=p1->next;
}
if(pn->num<=p1->num)
{
p1->next=pn;
pn->next=NULL;
}
else
{
pn->next=p1;
p2->next=pn;
}
return head;
} |
|