-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListMap.java
164 lines (151 loc) · 3.92 KB
/
ListMap.java
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
// ListMap.java
class ListMap<K> implements Map<K> {
public ListElement first;
/**
* Kelas ListElement yang diimplementasikan sebagai InnerClass
* Anda diperkenankan menambahkan method apabila dirasa perlu
*/
public class ListElement {
private K key;
private int value;
private ListElement next;
/**
* Constructor ListElement
* Set next = null
*/
public ListElement(K key, int value) {
// Your code goes here :)
this.key = key;
this.value = value;
this.next = null;
}
/**
* HINTED METHOD (Boleh dihilangkan jika tidak digunakan)
* Menghitung panjang rantai ListElement yang ada (terhitung dari elemen *ini*)
* Mungkin diperlukan untuk menghitung size()
*/
/**
* HINTED METHOD (Boleh dihilangkan jika tidak digunakan)
* Mengembalikan ListElement yang terletak pada ujung rantai ListElement
* Mungkin diperlukan ketika menambahkan elemen yang belum terdapat pada map. :)
*/
}
/**
* Constructor ListMap
* Set first = null
*/
public ListMap() {
// Your code goes here :)
this.first = null;
}
/**
* Menambahkan (key, value) ke dalam ListMap jika key belum terdapat pada map.
* Melakukan overwrite jika sudah terdapat elemen dengan key yang sama.
*/
public void add(K key, int value) {
// Your code goes here :)
if (this.first == null) {
this.first = new ListElement(key, value);
} else {
ListElement window = first;
while(!(window.key.equals(key)) && window.next != null) {
window = window.next;
}
if (window.key.equals(key)) {
window.value = value;
} else {
window.next = new ListElement(key, value);
}
}
}
/**
* Mengembalikan value yang tersimpan untuk key tertentu pada map
* Mengembalikan null apabila map tidak mengandung key masukan.
*/
public int get(K key) {
// Your code goes here :)
ListElement window = first;
if (window != null) {
while (!(window.key.equals(key)) && window.next != null) {
window = window.next;
}
if (window.key.equals(key)) {
return window.value;
} else {
return 0;
}
} else {
return 0;
}
}
/**
* Menghitung jumlah elemen yang ada pada map
*/
public int size() {
// Your code goes here :)
int i = 0;
ListElement currPointer = first;
while (currPointer != null) {
i++;
currPointer = currPointer.next;
}
return i;
}
public void printList(int sunfPoint) {
ListElement currPointer = first;
int i = 1;
while (currPointer != null) {
if (currPointer.value <= sunfPoint) {
System.out.println(i + ". " + currPointer.key + " " + currPointer.value);
i++;
}
currPointer = currPointer.next;
}
System.out.println("0. SKIP");
}
public K namaTumbuhanTerpilih(int x, int sunfPoint) {
ListElement currPointer = first;
int i = 1;
K namanya = null;
while (currPointer != null) {
if (currPointer.value <= sunfPoint) {
if (x == i) {
namanya = currPointer.key;
break;
} else {
i++;
currPointer = currPointer.next;
}
}
}
return namanya;
}
public int hargaTumbuhanTerpilih(int x, int sunfPoint) {
ListElement currPointer = first;
int i = 1;
int harganya = 0;
while (currPointer != null) {
if (currPointer.value <= sunfPoint) {
if (x == i) {
harganya = currPointer.value;
break;
} else {
i++;
currPointer = currPointer.next;
}
}
}
return harganya;
}
public int banyakPilihan(int sunfPoint) {
ListElement currPointer = first;
int banyakpilihan = 0;
while (currPointer != null) {
if (currPointer.value <= sunfPoint) {
banyakpilihan++;
}
currPointer = currPointer.next;
}
return banyakpilihan;
}
}