问题描述
幼儿园小朋友要玩老鹰捉小鸡的游戏,老师把在场的小朋友按身高递增的顺序排好了队,这时有一位小朋友来晚了,老师需要把他按照身高插入到排好的队列中,请你帮助幼儿园老师把这位晚到的小朋友插入到队列中合适的位置。
输入说明
输入的第1行包含原队列长度N(任意int范围内的非负整数)与待插入的小朋友的身高值M(单位为cm);第2行包含N个递增的小朋友的身高值代表原递增数列。
输出说明
根据此递增数列,构造出递增链表,然后将M插入到链表中,输出插入后的链表对应的数列。数字间隔一个空格,但末尾不能有空格。
输入样例
6 100
99 102 104 105 107 110
输出样例
99 100 102 104 105 107 110
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct fri
{
int h;
struct fri *next;
}l;
l *cre(int n)
{
int h,i;
l *head,*r,*s;
head=(l*)malloc(sizeof(l));
r=head;
for(i=0;i<n;i++)
{
scanf("%d",&h);
s=(l *)malloc(sizeof(l));
s->h=h;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
void ins(l *head,int m)
{
l *s,*p=head;
s=(l*)malloc(sizeof(l));
s->h=m;
while(p->next!=NULL&&p->next->h<m) p=p->next;
if(p->next==NULL)
{
p->next=s;
s->next=NULL;
}
else
{
s->next=p->next;
p->next=s;
}
}
void output(l *head)
{
l *r=head->next;
while(r!=NULL)
{
printf("%d",r->h);
if(r->next!=NULL) printf(" ");
r=r->next;
}
}
int main()
{
int n,m;
l *L;
scanf("%d %d",&n,&m);
L=cre(n);
ins(L,m);
output(L);
return 0;
}