已知一个递增有序链表L(带头结点,元素为整数),编写程序将一个新整数插入到L中,并保持L的有序性。 其中单链表的类型定义参考如下:
typedef int elementType;
typedef struct lnode
{ elementType data;
struct lnode *next;
}Lnode,* LinkList;
输入格式:
输入分三行
第一行 元素个数
第二行 元素的值,元素间用空格分隔。
第三行 待插入的元素值
输出格式:
在一行中输出有序链表元素值,每个元素前输出一个空格以便与相邻元素分隔。
输入样例:
5
1 3 5 7 9
4
输出样例:
1 3 4 5 7 9
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
int num,x;
cin>>num;
num++;
vector<int> v;
while(num--){
cin>>x;
v.push_back(x);
}
sort(v.begin(),v.end());
for(auto it = v.begin();it!=v.end();it++)
cout<<" "<<*it;
return 0;
}
}