链表 插入

/*链表 创建,插入,输出*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
	__int64 num; 
	char sex; 
	char name[10]; 
	char come_from[20]; 
	struct student *next;
};/*建立结构体*/
void out(struct student *head)
{ 
	struct student *p;
	p=head; 
	do
	{ 
		printf("%I64d %c %s %s\n",p->num,p->sex,p->name,p->come_from);
		p=p->next; }while(p!=NULL);
} /*输出*/
struct student *creat()
{
	int m,n; 
	struct student *head=NULL,*p,*q,*t; 
	scanf("%d",&n);
	while(n--) 
	{ 
		p=(struct student*) malloc(sizeof(struct student));
		scanf("%I64d %c %s %s",&p->num,&p->sex,p->name,p->come_from);
		if(head==NULL) 
			head=p; 
		else 
			q->next=p;
		q=p; 
	} 
	q->next=NULL;
	return(head);
}/*建立链表*/
struct student *input(struct student *head,int m)
{ 
	struct student *p,*q,*t; 
	int i; 
	for(i=0;i<m;i++) 
	{
		p=(struct student*) malloc(sizeof(struct student)); 
		scanf("%I64d %c %s %s",&p->num,&p->sex,p->name,p->come_from);
		for(q=head;q!=NULL;q=q->next)
		{ 
			if(p->num<q->num) 
			{
				p->next=q;
				if(q==head) 
					head=p;
				else t->next=p;
				break; 
			} 
			t=q;
		} 
		if(q==NULL) 
		{ 
			t->next=p; 
			p->next=NULL;
		}
	} 
	return (head);
}/*进行有序插入*/
void main()
{ 
	struct student *head;
	int m;
	head=creat();
	scanf("%d",&m);
	head=input(head,m); 
	out(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值