forked from ASD-ADF/ASD_Task_4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
199 lines (184 loc) · 4.61 KB
/
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
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
#include "list.h"
void createList(List &L) {
/**
* FS : first(L) diset Nil
*/
//------------- YOUR CODE HERE -------------
first(L) == NULL;
//----------------------------------------
}
address allocate(infotype x) {
/**
* FS : mengembalikan elemen list baru dengan info = x,
* next dan prev elemen = Nil
*/
address P = NULL;
//------------- YOUR CODE HERE -------------
P = new elmlist;
info(P) = x;
next(P) = NULL;
prev(P) = NULL;
//----------------------------------------
return P;
}
void deallocate(address &P) {
/**
* FS : menghapus elemen yang ditunjuk oleh P (delete)
*/
//------------- YOUR CODE HERE -------------
delete P;
//----------------------------------------
}
void insertFirst(List &L, address P) {
/**
* IS : List L mungkin kosong
* FS : elemen yang ditunjuk P menjadi elemen pertama pada List L
*/
//------------- YOUR CODE HERE -------------
if ( first(L) == NULL )
{
first(L) = P;
next(P) = P;
prev(P) = P;
}
else
{
next(P) = first(L) ;
prev(P) = prev(first(L));
next(prev(first(L))) = P;
prev(first(L)) = P;
first(L) = P;
}
//----------------------------------------
}
void insertLast(List &L, address P) {
/**
* IS : List L mungkin kosong
* FS : elemen yang ditunjuk P menjadi elemen terakhir pada List L
*/
//------------- YOUR CODE HERE -------------
address Prec;
Prec = first(L);
if (first(L) == NULL)
{
insertFirst(L,P);
}
else
{
next(P) = first(L);
prev(P) = prev(first(L));
next(prev(first(L))) = P;
prev(first(L)) = P;
}
//----------------------------------------
}
address findElmByID(List L, infotype x) {
/**
* IS : List L mungkin kosong
* FS : mengembalikan elemen dengan info.ID = x.ID,
mengembalikan Nil jika tidak ditemukan
*/
address P = NULL;
//------------- YOUR CODE HERE -------------
P = first(L);
do
{
P = next(P);
}
while((P != first(L)) && (x.ID != info(P).ID)) ;
if ((P == first(L)) && (x.ID != info(P).ID))
{
return NULL;
}
//----------------------------------------
return P;
}
address findElmByName(List L, infotype x) {
/**
* IS : List L mungkin kosong
* FS : mengembalikan elemen dengan info.name = x.name,
mengembalikan Nil jika tidak ditemukan
*/
address P = NULL;
//------------- YOUR CODE HERE -------------
P = first(L);
do
{
P = next(P);
}
while((P != first(L)) && (x.name != info(P).name));
if (P == first(L) && (x.name != info(P).name))
{
return NULL;
}
//----------------------------------------
return P;
}
void deleteFirst(List &L, address &P) {
/**
* IS : List L mungkin kosong
* FS : elemen pertama di dalam List L dilepas dan disimpan/ditunjuk oleh P
*/
//------------- YOUR CODE HERE -------------
//----------------------------------------
}
void deleteLast(List &L, address &P) {
/**
* IS : List L mungkin kosong
* FS : elemen tarakhir di dalam List L dilepas dan disimpan/ditunjuk oleh P
*/
//------------- YOUR CODE HERE -------------
P = first(L);
if (next(P) == first(L))
{
first(L) = NULL;
next(P) = NULL;
prev(P) = NULL;
}
else
{
first(L) = next(first(L));
next(prev(P)) = first(L);
prev(first(L)) = prev(P);
next(P) = NULL;
prev(P) = NULL;
}
//----------------------------------------
}
void insertAfter(List &L, address &Prec, address P) {
/**
* IS : Prec dan P tidak NULL
* FS : elemen yang ditunjuk P menjadi elemen di belakang elemen yang
* ditunjuk pointer Prec
*/
//------------- YOUR CODE HERE -------------
prev(next(Prec)) = P;
next(P) = next(Prec);
next(Prec) = P;
prev(P) = Prec;
//----------------------------------------
}
void deleteAfter(List &L, address &Prec, address &P) {
/**
* IS : Prec tidak NULL
* FS : elemen yang berada di belakang elemen Prec dilepas
* dan disimpan/ditunjuk oleh P
*/
//------------- YOUR CODE HERE -------------
P = next(Prec);
if (next(next(Prec)) == Prec)
{
next(P) = NULL;
prev(P) = NULL;
next(Prec) = Prec;
prev(Prec) = Prec;
}
else
{
next(Prec) = next(P);
prev(next(P)) = Prec;
next(P) = NULL;
prev(P) = NULL;
}
//----------------------------------------
}