链表的创建、输出、查找、删除、添加和排序

#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);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iKUNqa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值