Skip to content

Commit 7407abb

Browse files
committed
added bab2_singlelist
1 parent 0d6e783 commit 7407abb

6 files changed

Lines changed: 421 additions & 1 deletion

File tree

.idea/codeStyles/Project.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ project(datastructure_cpp_basic)
44
set(CMAKE_CXX_STANDARD 14)
55

66
add_executable(datastructure_cpp_basic
7-
firstimpression/InputOutput.cpp firstimpression/Kondisi.cpp firstimpression/Perulangan.cpp firstimpression/Fungsi.cpp firstimpression/Array.cpp)
7+
firstimpression/InputOutput.cpp firstimpression/Kondisi.cpp firstimpression/Perulangan.cpp firstimpression/Fungsi.cpp firstimpression/Array.cpp
8+
bab2_singlelist/mainSinglelist.cpp bab2_singlelist/singlelist.cpp bab2_singlelist/singlelist.h)

bab2_singlelist/mainSinglelist.cpp

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
#include "singlelist.h"
4+
5+
using namespace std;
6+
7+
int main() {
8+
List L;
9+
address P, Q;
10+
infotype X, Y;
11+
12+
cout<<endl;
13+
cout<<"Hello, Selamat Datang di Single Linked List"<< endl;
14+
cout <<""<<endl;
15+
16+
17+
cout<<"1a. Uji project berikut: Allocation dan Deallocation"<<endl;
18+
X = 10;
19+
P = allocate(X);
20+
cout<<" print X = "<<X<<endl;
21+
cout<<" P = allocate(X)"<<endl;
22+
cout<<" print info(P) = "<<info(P)<<endl;
23+
cout<<" print alamat dari elemen P = "<<P<<endl;
24+
cout<<""<<endl;
25+
cout<<" [Tekan enter untuk lanjut]"<<endl;
26+
cin.get();
27+
cout<<" deallocate(P)"<<endl;
28+
deallocate(P);
29+
/** cout<<" print info(P) = "<<info(P)<<endl; // this will cause error */
30+
cout<<" print alamat dari elemen P = "<<P<<endl;
31+
cout<<" [press enter to continue]"<<endl;
32+
cin.get();system("cls");
33+
cout<<endl;
34+
cout<<"1b. Ujilah project berikut: Create List dan Print Info"<<endl;
35+
createList(L);
36+
cout<<" Ujilah Print Info untuk List L"<<endl;
37+
printInfo(L);
38+
cout<<" anda bisa memodifikasi prosedur printInfo(List L) untuk menampilkan pesan"<<endl
39+
<<" saat list kosong"<<endl;
40+
cout<<" [Tekan enter untuk lanjut]"<<endl;
41+
cin.get();system("cls");
42+
cout<<endl;
43+
44+
45+
cout<<"2. Uji prosedur insertFirst(List &L, address P)"<<endl;
46+
createList(L);
47+
cout<<" Initial list [kosong]: "; printInfo(L);
48+
cout<<endl;
49+
cout<<" Insert First info = 5"<<endl;
50+
insertFirst(L, allocate(5));
51+
cout<<" Isi list seharusnya : 5, "<<endl;
52+
cout<<" List anda : "; printInfo(L);
53+
cout<<endl;
54+
cout<<" Insert First info = 8"<<endl;
55+
insertFirst(L, allocate(8));
56+
cout<<" Isi list seharusnya : 8, 5, "<<endl;
57+
cout<<" List anda : "; printInfo(L);
58+
cout<<endl;
59+
cout<<" Insert First info = 0"<<endl;
60+
insertFirst(L, allocate(0));
61+
cout<<" Isi list seharusnya : 0, 8, 5, "<<endl;
62+
cout<<" List anda : "; printInfo(L);
63+
cout<<endl;
64+
cout<<" Insert First info = 3"<<endl;
65+
insertFirst(L, allocate(3));
66+
cout<<" Isi list seharusnya : 3, 0, 8, 5, "<<endl;
67+
cout<<" List anda : "; printInfo(L);
68+
cout<<" [Tekan enter untuk lanjut]"<<endl;
69+
cin.get();system("cls");
70+
cout<<endl;
71+
72+
73+
cout<<"3. Uji prosedur deleteFirst(List &L, address &P)"<<endl;
74+
cout<<" ** Anda mungkin mendapat error pada bagian ini**"<<endl;
75+
cout<<" ** pikirlah mengapa hal tersebut dapat terjadi, kemudian tanyakan asprak anda **"<<endl<<endl;
76+
cout<<" Initial List: "; printInfo(L);
77+
cout<<endl;
78+
cout<<" Delete First"<<endl; deleteFirst(L, P);
79+
cout<<" Info(P) seharusnya : 3"<<endl;
80+
cout<<" Info(P) anda : "<<info(P)<<endl;
81+
cout<<" List seharusnya : 0, 8, 5, "<<endl;
82+
cout<<" List anda : "; printInfo(L);
83+
deallocate(P);
84+
cout<<endl;
85+
cout<<" Delete First"<<endl; deleteFirst(L, P);
86+
cout<<" Info(P) seharusnya : 0"<<endl;
87+
cout<<" Info(P) anda : "<<info(P)<<endl;
88+
cout<<" List seharusnya : 8, 5, "<<endl;
89+
cout<<" List anda : "; printInfo(L);
90+
deallocate(P);
91+
cout<<endl;
92+
cout<<" [Tekan enter untuk lanjut]"<<endl;
93+
cin.get();system("cls");
94+
cout<<endl;
95+
96+
97+
cout<<"3. Uji prosedur deleteFirst(List &L, address &P) [Cont'd]"<<endl;
98+
cout<<" Initial List: "; printInfo(L);
99+
cout<<endl;
100+
cout<<" Delete First"<<endl; deleteFirst(L, P);
101+
cout<<" Info(P) seharusnya : 8"<<endl;
102+
cout<<" Info(P) anda : "<<info(P)<<endl;
103+
cout<<" List seharusnya : 5, "<<endl;
104+
cout<<" List anda : "; printInfo(L);
105+
deallocate(P);
106+
cout<<endl;
107+
cout<<" Delete First"<<endl; deleteFirst(L, P);
108+
cout<<" Info(P) seharusnya : 5"<<endl;
109+
cout<<" Info(P) anda : "<<info(P)<<endl;
110+
cout<<" List seharusnya : [kosong] "<<endl;
111+
cout<<" List anda : "; printInfo(L);
112+
deallocate(P);
113+
cout<<endl;
114+
cout<<" Delete First"<<endl;
115+
cout<<" prosedur anda harus mengecek apakah list kosong"<<endl;
116+
deleteFirst(L, P);
117+
cout<<" address P seharusnya : 0 (NULL)"<<endl;
118+
cout<<" address P anda : "<<P<<endl;
119+
cout<<" List seharusnya : [kosong] "<<endl;
120+
cout<<" List anda : "; printInfo(L);
121+
cout<<endl;
122+
cout<<" [Tekan enter untuk lanjut]"<<endl;
123+
cin.get();system("cls");
124+
cout<<endl;
125+
126+
127+
cout<<"4. Uji prosedur insertLast(List &L, address P)"<<endl;
128+
cout<<" ** Anda mungkin mendapat error pada bagian ini**"<<endl;
129+
cout<<" ** pikirlah mengapa hal tersebut dapat terjadi, kemudian tanyakan asprak anda **"<<endl<<endl;
130+
createList(L);
131+
cout<<" Initial List [kosong]: "; printInfo(L);
132+
cout<<endl;
133+
cout<<" Insert Last info = 5"<<endl;
134+
cout<<" prosedur anda harus mengecek apakah list kosong"<<endl;
135+
insertLast(L, allocate(5));
136+
cout<<" List seharusnya : 5, "<<endl;
137+
cout<<" List anda : "; printInfo(L);
138+
cout<<endl;
139+
cout<<" Insert Last info = 8"<<endl;
140+
insertLast(L, allocate(8));
141+
cout<<" List seharusnya : 5, 8, "<<endl;
142+
cout<<" List anda : "; printInfo(L);
143+
cout<<endl;
144+
cout<<" Insert Last info = 0"<<endl;
145+
insertLast(L, allocate(0));
146+
cout<<" List seharusnya : 5, 8, 0, "<<endl;
147+
cout<<" List anda : "; printInfo(L);
148+
cout<<endl;
149+
cout<<" Insert Last info = 3"<<endl;
150+
insertLast(L, allocate(3));
151+
cout<<" List seharusnya : 5, 8, 0, 3, "<<endl;
152+
cout<<" List anda : "; printInfo(L);
153+
cout<<" [Tekan enter untuk lanjut]"<<endl;
154+
cin.get();system("cls");
155+
cout<<endl;
156+
157+
158+
cout<<"5. Test the procedure deleteLast(List &L, address &P)"<<endl;
159+
cout<<" ** Anda mungkin mendapat error pada bagian ini**"<<endl;
160+
cout<<" ** pikirlah mengapa hal tersebut dapat terjadi, kemudian tanyakan asprak anda **"<<endl<<endl;
161+
cout<<" Initial List: "; printInfo(L);
162+
cout<<endl;
163+
cout<<" Delete Last"<<endl; deleteLast(L, P);
164+
cout<<" Info(P) seharusnya : 3"<<endl;
165+
cout<<" Info(P) anda : "<<info(P)<<endl;
166+
cout<<" List seharusnya : 5, 8, 0, "<<endl;
167+
cout<<" List anda : "; printInfo(L);
168+
deallocate(P);
169+
cout<<endl;
170+
cout<<" Delete Last"<<endl; deleteLast(L, P);
171+
cout<<" Info(P) seharusnya : 0"<<endl;
172+
cout<<" Info(P) anda : "<<info(P)<<endl;
173+
cout<<" List seharusnya : 5, 8, "<<endl;
174+
cout<<" List anda : "; printInfo(L);
175+
deallocate(P);
176+
cout<<endl;
177+
cout<<" [Tekan enter untuk lanjut]"<<endl;
178+
cin.get();system("cls");
179+
cout<<endl;
180+
181+
182+
cout<<"5. Test the procedure deleteLast(List &L, address &P) [Cont'd]"<<endl;
183+
cout<<" Initial List: "; printInfo(L);
184+
cout<<endl;
185+
cout<<" Delete Last"<<endl; deleteLast(L, P);
186+
cout<<" Info(P) seharusnya : 8"<<endl;
187+
cout<<" Info(P) anda : "<<info(P)<<endl;
188+
cout<<" List seharusnya : 5, "<<endl;
189+
cout<<" List anda : "; printInfo(L);
190+
deallocate(P);
191+
cout<<endl;
192+
cout<<" Delete Last"<<endl; deleteLast(L, P);
193+
cout<<" Info(P) seharusnya : 5"<<endl;
194+
cout<<" Info(P) anda : "<<info(P)<<endl;
195+
cout<<" List seharusnya : [kosong] "<<endl;
196+
cout<<" List anda : "; printInfo(L);
197+
deallocate(P);
198+
cout<<endl;
199+
cout<<" Delete Last"<<endl;
200+
cout<<" prosedur anda harus mengecek apakah list kosong"<<endl;
201+
deleteLast(L, P);
202+
cout<<" address P seharusnya: 0 (NULL)"<<endl;
203+
cout<<" address P anda : "<<P<<endl;
204+
cout<<" List seharusnya : [kosong] "<<endl;
205+
cout<<" List anda : "; printInfo(L);
206+
cout<<endl;
207+
cout<<" [Tekan enter untuk lanjut]"<<endl;
208+
cin.get();system("cls");
209+
cout<<endl;
210+
211+
212+
cout<<"Anda telah menyelesaikan jurnal modul 2"<<endl;
213+
cout<<endl;
214+
cout<<"BONUS: Reverse List"<<endl;
215+
cout<<"Amati prosedur reverseList yang telah tersedia, amati hasil dibawah:"<<endl;
216+
createList(L);
217+
insertFirst(L,allocate(3));
218+
insertFirst(L,allocate(6));
219+
insertFirst(L,allocate(8));
220+
insertFirst(L,allocate(2));
221+
insertFirst(L,allocate(9));
222+
cout<<"sebelum reverse : ";printInfo(L);
223+
reverseList(L);
224+
cout<<"setelah reverse : ";printInfo(L);
225+
226+
cout<<""<<endl;
227+
return 0;
228+
}
229+

bab2_singlelist/singlelist.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "singlelist.h"
2+
3+
/** DIBERIKAN **/
4+
address allocate(infotype x){
5+
/** Fungsi ini akan membuat
6+
elemen baru, mengalokasikan
7+
memory untuk elemen tersebut,
8+
dan mengembalikan pointer p
9+
yang menunjuk elemen baru*/
10+
11+
address p = new elmlist;
12+
info(p) = x;
13+
next(p) = NULL;
14+
15+
return p;
16+
}
17+
void deallocate(address &P){
18+
/** Prosedur ini akan menghapus
19+
elemen yang ditunjuk oleh
20+
pointer P*/
21+
22+
delete P;
23+
P = NULL;
24+
}
25+
void createList(List &L){
26+
/** Prosedur ini akan menginisialisasi
27+
list L*/
28+
29+
first(L) = NULL;
30+
}
31+
void printInfo(List L){
32+
/** prosedur ini akan mengoutputkan
33+
info dari masing-masing elemen
34+
di list L*/
35+
36+
address p = first(L);
37+
while (p != NULL){
38+
cout << info(p) << ", ";
39+
p = next(p);
40+
}
41+
cout << endl;
42+
}
43+
void reverseList(List &L){
44+
/** Prosedur ini akan membalikkan isi
45+
dari list L*/
46+
47+
List new_L;
48+
createList(new_L);
49+
50+
while (first(L) != NULL) {
51+
address p;
52+
deleteFirst(L, p);
53+
insertFirst(new_L, p);
54+
}
55+
56+
L = new_L;
57+
}
58+
59+
/** DIKERJAKAN **/
60+
void insertFirst(List &L, address P){
61+
/** TODO: Masukkan elemen yang ditunjuk
62+
oleh pointer P ke dalam bagian
63+
pertama list L*/
64+
next(P) = first(L);
65+
first(L) = P;
66+
67+
68+
}
69+
void insertLast(List &L, address P){
70+
/** TODO: Masukkan elemen yang ditunjuk
71+
oleh pointer P ke dalam bagian
72+
terakhir list L*/
73+
address Q;
74+
Q = first(L);
75+
if(first(L) == NULL){
76+
first(L) = P;
77+
}
78+
79+
else {
80+
while(next(Q) != NULL){
81+
Q = next(Q);
82+
}
83+
next(Q) = P;
84+
}
85+
86+
}
87+
void deleteFirst(List &L, address &P){
88+
/** TODO: Hapus elemen pertama list L
89+
dan masukkan elemen yang dihapus
90+
ke dalam pointer P*/
91+
92+
P = first(L);
93+
first(L) = next(first(L));
94+
next(P) = NULL;
95+
96+
}
97+
void deleteLast(List &L, address &P){
98+
/** TODO: Hapus elemen terakhir list L
99+
dan masukkan elemen yang dihapus
100+
ke dalam pointer P*/
101+
102+
if(first(L)=NULL) {
103+
cout<<"List Kosong / Tidak Ada" << endl;
104+
}
105+
else {
106+
address(Q) = first(L);
107+
while(next(next(Q)) != NULL){
108+
Q = next(Q);
109+
}
110+
P = next(Q);
111+
next(Q) = NULL;
112+
}
113+
114+
}

0 commit comments

Comments
 (0)