# PTA 7-4 insertion of ordered list (20 points)

Given an increasing ordered list L (leading node, element is integer), a program is written to insert a new integer into L, and keep the order of L The type definition of single chain table is as follows:

typedef int elementType;

typedef struct lnode

{ elementType data;

struct lnode *next;

### Input format:

Input in three lines

Number of elements in the first row

The value of the element in the second line, separated by spaces.

Element value to be inserted in the third line

### Output format:

Output ordered list element values in a row, with a space before each element to separate it from the adjacent elements.

```5
1 3 5 7 9
4
```

### Output example:

` 1 3 4 5 7 9`
```#include<stdio.h>
#include<malloc.h>

typedef int elementType;
typedef struct lnode
{
elementType data;
struct lnode *next;

}Lnode;

//Tail insertion method
Lnode* CreateList(int a[],int n)
{
int i;

for(i = 0;i<n;i++)
{
node = (Lnode*)malloc(sizeof(Lnode));
node->data = a[i];
p->next = node;
p = node;
}
p->next = NULL;

}
{
Lnode *insert,*p;
insert = (Lnode*)malloc(sizeof(Lnode));
int i;
insert->data = temp;
if(temp<p->data)
{
insert->next = p;
}
for(i = 0;i<n-1;i++)
{
if((temp>p->data) && (temp<=p->next->data))
{
insert->next = p->next;
p->next = insert;
}
p = p->next;
}
if(temp>p->data)
{
p->next = insert;
insert->next = NULL;
}

}

{
Lnode *p;
while(1)
{
printf(" %d",p->data);
if(p->next == NULL)
{
break;
}
p = p->next;
}
}
int main()
{
int n,i;
int temp;
scanf("%d",&n);

if(n==0)
{
scanf("%d",&temp);
p = (Lnode*)malloc(sizeof(Lnode));
p->data = temp;
p->next= NULL;
return 0;

}
int a[n];
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&temp);
/*for(i = 0;i<n;i++)
printf("%d",a[i]);*/