-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence_list.cpp
77 lines (67 loc) · 1.59 KB
/
sequence_list.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/***************************
* @author yehao
* @date 11 July 2018
* @theme sequence list
**************************/
#include <iostream>
#define MAXSIZE 9999
typedef struct {
int data[MAXSIZE];
int length;
} SequenceList;
int a[MAXSIZE];
void create(SequenceList &L) {
L.length = 0;// initial length
int value;
int i = 0;
while (std::cin >> value) {
L.data[i] = value;
L.length++;
i++;
}
}
void display(SequenceList &L) {
for (int i = 0; i < L.length; ++i) {
std::cout << L.data[i] << " ";
}
std::cout << std::endl;
// test
std::cout << L.length << std::endl;
}
void insertion(SequenceList &L, int position, int value) {
if (L.length == 0) {
std::cout << "can not insert a empty sequence_list, please use create() function first." << std::endl;
} else if (position <= L.length) {
for (int i = L.length; i >= position; --i) {
L.data[i] = L.data[i - 1];
}
L.data[position - 1] = value;
L.length++;
} else {
std::cout << "invalid position" << std::endl;
}
}
void deletion(SequenceList &L, int position) {
if (L.length == 0) {
std::cout << "can not delete a empty sequence_list, please use create() function first." << std::endl;
} else if (position <= L.length) {
for (int i = position -1; i < L.length - 1; ++i) {
L.data[i] = L.data[i + 1];
}
L.length--;
} else {
std::cout << "invalid position" << std::endl;
}
}
int main() {
SequenceList a;
display(a);
create(a);
insertion(a, 1, 4);
display(a);
insertion(a, 3, 100);
display(a);
deletion(a, 5);
display(a);
return 0;
}