//1. 稳定排序
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100;
int main()
{
int n, arr[MAX];
cin >> n;
for (int i = 0; i < n; ++ i)
cin >> arr[i];
stable_sort(arr,arr+n);
for (int i = 0; i < n; ++ i)
cout << arr[i] << ' ';
cout << endl;
return 0;
}
//2. 插入元素
#include <iostream>
using namespace std;
const int MAX = 100;
int main()
{
int n, arr[MAX], pos, x;
cin >> n;
for (int i = 0; i < n; ++ i)
cin >> arr[i];
cin >> pos >> x;
for (int i = n; i >= pos; -- i)
arr[i] = arr[i-1];
arr[pos-1] = x;
for (int i = 0; i <= n; ++ i)
cout << arr[i] << ' ';
cout << endl;
return 0;
}
//3. 建立链表插入元素
#include <iostream>
using namespace std;
struct Node
{
int val;
Node* next;
};
int main()
{
int n, value, pos, x;
cin >> n;
cin >> value;
Node* head = new Node{value, NULL}; //链表头
Node* cur = head;
for (int i = 1; i < n; ++ i) { //建立链表
cin >> value;
Node* tmp = new Node{value, NULL};
cur->next = tmp;
cur = tmp;
}
cin >> pos >> x;
Node* prehead = new Node{0,head}; //在表头前插入节点
cur = prehead;
while (-- pos) //将cur移动带待插入位置的前一个节点
cur = cur->next;
Node* tmp = new Node{x, NULL}; //待插入节点
tmp->next = cur->next;
cur->next = tmp;
//插入完毕 遍历并输出元素
head = prehead->next;
delete prehead;
while (head) {
cout << head->val << ' ';
cur = head;
head = head->next;
delete cur;
}
cout << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考