forked from bayulaxana/data_structure_implementation
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlist.cpp
More file actions
235 lines (177 loc) · 6.23 KB
/
list.cpp
File metadata and controls
235 lines (177 loc) · 6.23 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <list>
using namespace std;
bool is_small_num(const int& value) { return value < 50; }
int main()
{
list<int> angka;
list<int> genap = {2, 4, 6, 8};
list<int> ganjil = {1, 3, 5, 7};
list<int>::iterator it, it1, it2;
angka.push_front(4);
angka.push_back(1);
angka.push_front(2);
angka.push_back(3);
cout << "Isi list \"angka\":";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Jumlah elemen pada list \"angka\": " << angka.size() << "\n";
cout << "Jumlah maksimal elemen yang dapat ditampung oleh list \"angka\": " << angka.max_size() << "\n";
if (angka.empty())
cout << "List \"angka\" kosong.\n";
else
cout << "List \"angka\" tidak kosong.\n";
angka.reverse();
cout << "Isi list \"angka\" setelah reverse:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.sort();
cout << "Isi list \"angka\" setelah sort:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.pop_front();
angka.pop_back();
cout << "Isi list \"angka\" setelah pop (front dan back):";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.resize(5, 100);
cout << "Isi list \"angka\" setelah resize:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.assign(8, 10);
cout << "Isi list \"angka\" setelah assign:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
it = angka.begin(); // it menunjuk posisi pertama
++it; // it menunjuk posisi ke-2
// memasukkan nilai 20 di posisi ke-2
angka.insert(it, 20);
// it menunjuk posisi ke-3 (bergeser akibat insert elemen sebanyak 1)
// memasukkan nilai 30 di posisi ke-3 sebanyak 3
angka.insert(it, 3, 30);
cout << "Isi list \"angka\" setelah insert:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
it1 = angka.begin(); // it1 menunjuk posisi pertama
++it1; // it1 menunjuk posisi ke-2
// menghapus elemen ke-2
it1 = angka.erase(it1);
// it tetap menunjuk posisi ke-2
it2 = angka.begin(); // it2 menunjuk posisi pertama
advance(it2, 4); // menggeser it2 sebanyak 4 posisi
// it2 menunjuk posisi ke-5
// menghapus elemen ke-2 hingga elemen ke-5
angka.erase(it1, it2);
cout << "Isi list \"angka\" setelah erase:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.clear();
if (angka.empty())
cout << "List \"angka\" kosong.\n";
else
cout << "List \"angka\" tidak kosong.\n";
cout << "Isi list \"genap\":";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\":";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
genap.swap(ganjil);
// atau
// ganjil.swap(genap);
cout << "Isi list \"genap\" setelah swap:";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\" setelah swap:";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
it = genap.begin(); // it menunjuk posisi pertama
advance(it, 2); // menggeser it sebanyak 2 posisi
// memindahkan semua elemen ke list lain
genap.splice(it, ganjil);
cout << "Isi list \"genap\" setelah splice:";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\" setelah splice:";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
// memindahkan satu elemen ke list lain
ganjil.splice(ganjil.begin(), genap, it);
cout << "Isi list \"genap\" setelah splice:";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\" setelah splice:";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
it = genap.begin(); // it menunjuk posisi pertama
advance(it, 3); // menggeser it sebanyak 3 posisi
// memindahkan beberapa elemen pada range tertentu ke list lain
ganjil.splice(ganjil.begin(), ganjil, it, genap.end());
cout << "Isi list \"genap\" setelah splice:";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\" setelah splice:";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
// syarat sebelum melakukan merge adalah kedua list terurut
genap.sort();
ganjil.sort();
genap.merge(ganjil);
// atau
// ganjil.merge(genap);
cout << "Isi list \"genap\" setelah merge:";
for (auto i = genap.begin(); i != genap.end(); ++i)
cout << ' ' << *i;
cout << '\n';
cout << "Isi list \"ganjil\" setelah merge:";
for (auto i = ganjil.begin(); i != ganjil.end(); ++i)
cout << ' ' << *i;
cout << '\n';
for (int i = 0; i < 10; i++)
{
angka.push_front((i + 1) * 10);
}
for (int i = 0; i < 10; i++)
{
angka.push_back((i + 1) * 10);
}
angka.sort();
cout << "Isi list \"angka\":";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.remove(10);
cout << "Isi list \"angka\" setelah remove:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.remove_if(is_small_num);
cout << "Isi list \"angka\" setelah remove if:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
angka.unique();
cout << "Isi list \"angka\" setelah unique:";
for (auto i = angka.begin(); i != angka.end(); ++i)
cout << ' ' << *i;
cout << '\n';
return 0;
}