#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
struct List{
int num;
List *next;
};
List *Creat(int n)
{
List *head,*p1,*q;
p1=(List*)malloc(sizeof(List));
scanf("%d",&p1->num);
head=p1;
q=p1;
for(int i=0;i<n-1;i++){
p1=(List*)malloc(sizeof(List));
scanf("%d",&p1->num);
q->next=p1;
q=p1;
}
p1->next=NULL;
return head;
}
void Insert(List *Ori,int in){
List *p1,*q,*t;
t=(List*)malloc(sizeof(List));
q=Ori;
p1=q->next;
t->num=in;
if(q->num>t->num )
{
t->next=Ori;
Ori=t;
}
while(p1!=NULL){
if(p1->num>in)
{
break;
}
q=q->next;
p1=q->next;
}
q->next=t;
t->next=p1;
}
int main()
{
int n;
cin>>n;
List* Ori=Creat(n);
int in;
cin>>in;
Insert(Ori,in);
while(Ori!=NULL){
printf("%d ",Ori->num);
Ori=Ori->next;
}
return 0;
}
有序链表的插入
最新推荐文章于 2021-11-18 12:03:59 发布