NOJ DS G1:SeqList

G1.1

noj_ds1

Simple question, using SeqList and its Insertion.

The code is down here:

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
29
30
31
32
33
34
35
36
37
38
39
40
//Language up to you; Personally use cpp more.
#include <iostream>

using namespace std;
#define MAXSIZE 1000

typedef struct SeqList {
int elenum;
int list[MAXSIZE];
}SeqList, *pSeqList;

void Insert(int x, pSeqList pl) {
int i, j;
for (i = 0; i < pl->elenum; i++) {
if (x < pl->list[i]) {
for (j = pl->elenum; j > i; j--) {
pl->list[j] = pl->list[j - 1];
}
pl->list[i] = x;
pl->elenum++;
return;
}
}
}

int main() {
SeqList L;
pSeqList pl = &L;
int x, i;
cin >> L.elenum ;
for (i = 0; i < L.elenum; i++) {
cin >> L.list[i];
}
cin >> x;
Insert(x, pl);
for (i = 0; i < L.elenum; i++) {
cout << L.list[i] << " ";
}
return 0;
}