问题 K: 高程08-03:编写函数向链表中插入数据

问题 K: 高程08-03:编写函数向链表中插入数据

时间限制: 1 Sec  内存限制: 128 MB
提交: 2315  解决: 1256
[提交][状态][讨论版][命题人:]

题目描述

补充完成下面的 insert 函数,向一个非递减链表中插入一个数,插入后仍按非递减顺序排列

#include<stdio.h>

#include<stdlib.h>
struct node
{
 int x;
 struct node *next;
};

void insert(struct node *H,int p)
{//H带有头结点。头结点后面的结点是第一个数据结点
 
}

void print(struct node *H)
{
 struct node *p;
 p=H->next;
 while(p!=0)
 {
  printf("%d ",p->x);
  p=p->next;
 }
}

void freeheadlist(struct node *h)
{
struct node *t;
h=h->next;
while(h)
{
t=h->next;
free(h);
h=t;
}
}
int main()
{
 struct node H,*t,*tail;
 int x;
 int i;
 int p;
 
 H.next=0;
 tail=&H;
 
 for(i=1;i<=4;i++)
 {
  scanf("%d",&x);
  t=(struct node*)malloc(sizeof(struct node));
  t->x=x;
  t->next=0;
  
  tail->next=t;
  tail=t;  
  
 }
 
 scanf("%d",&p);
 insert(&H,p);
 
 print(&H);
 freeheadlist(&H);
  return 0;
}

输入

输入有两行。第一行有4个数据,按照非递减序排列,用空格隔开。

第二行是一个整数p。

insert的功能是把p插入第一行的4个数的合适位置,使得插入后,5个数是非递减顺序。

输出

输出插入数据后链表的数据

样例输入

2 7 7 11
9

样例输出

2 7 7 9 11

提示

[提交][状态]