#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[10];
int num;
struct birthday
{
int year;
int month;
int day;
};
float score[3];
struct birthday date;
struct node *next;
};
struct node *creat(struct node *head,int n)
{
struct node *p,*q;int i;
for(i=1;i<=n;i++)
{
q=(struct node *)malloc(sizeof(struct node));
printf("输入第%d位学生姓名、学号、出生日期、三门课成绩:\n",i);
scanf("%s",q->name);
scanf("%d",&q->num);
scanf("%d%d%d",&q->date.year,&q->date.month,&q->date.day);
scanf("%f%f%f",&q->score[0],&q->score[1],&q->score[2]);
q->next=NULL;
if(head==NULL)
head=q;
else
p->next=q;
p=q;
}
return head;
}
void print(struct node *head)
{
struct node *p=head;
printf("姓名、学号、出生日期、三门课成绩:\n");
while(p!=NULL)
{
printf("%s %d %d %d %d %f %f %f\n",p->name,p->num,p->date.year,p->date.month,p->date.day,p->score[0],p->score[1],p->score[2]);
p=p->next;
}
}
struct node *dele(struct node *head)
{
int x;
struct node *p,*q;
p=head;
printf("请输入要删除的学号:\n");
scanf("%d",&x);
while(p!=NULL&&p->num!=x)
{
q=p;
p=p->next;
}
if(p==NULL)
printf("%d is not found!\n");
else if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
return(head);
}
struct node *insert(struct node *head)
{
struct node *q,*p,*p1;
q=(struct node *)malloc(sizeof(struct node));
printf("输入要插入的学生姓名、学号、出生日期、三门课成绩:\n");
scanf("%s",q->name);
scanf("%d",&q->num);
scanf("%d%d%d",&q->date.year,&q->date.month,&q->date.day);
scanf("%f%f%f",&q->score[0],&q->score[1],&q->score[2]);
if(head==NULL)
{
q->next=NULL;
head=q;
return(head);
}
if(head->num>q->num)
{
q->next=head;
head=q;
return head;
}
p=head;
p1=head->next;
while(p1!=NULL&&p1->num<q->num)
{
p=p1;
p1=p1->next;
}
q->next=p1;
p->next=q;
return(head);
}
void main()
{
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node *dele(struct node *head);
struct node *insert(struct node *head);
struct node *head=NULL;
head=creat(head,3);
print(head);
head=dele(head);
print(head);
head=insert(head);
print(head);
}
链表的创建、输出、查找、删除、添加和排序
最新推荐文章于 2023-06-13 16:56:01 发布