1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
void insert(int pos, int val, int list[], int size) {
if (list[pos] != 0)
for (int i = size - 1; i > pos; i--)
list[i] = list[i - 1];
list[pos] = val;
}
void print(const int list[], int size) {
cout << "\n[ ";
for (int i = 0; i < size; i++)
cout << list[i] << ' ';
cout << "]\n";
}
int main() {
const int Size = 10;
int list[Size]{};
print(list, Size);
insert(0, 10, list, Size);
insert(1, 20, list, Size);
insert(2, 25, list, Size);
insert(0, 4, list, Size);
insert(1, 40, list, Size);
print(list, Size);
}
| |