forked from ASD-ADF/ASD_Task_4
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.cpp
More file actions
135 lines (118 loc) · 2.22 KB
/
list.cpp
File metadata and controls
135 lines (118 loc) · 2.22 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
#include <iostream>
#include "list.h"
using namespace std;
void createList(List &L)
{
First(L) = NULL;
Last(L) = NULL;
}
address alokasi(infotype x)
{
address P = new elemenList;
Info(P) = x;
Next(P) = NULL;
Prev(P) = NULL;
return P;
}
void dealokasi(address &P)
{
delete P;
}
void insertFirst(List &L, address P)
{
if(First(L) == NULL){
First(L) = P;
Last(L) = P;
}else{
Next(P) = First(L);
Prev(First(L)) = P;
Next(Last(L)) = P;
Prev(P) = Last(L);
First(L) = P;
}
}
void insertLast(List &L, address P)
{
if(First(L) == NULL)
{
insertFirst(L,P);
}
else
{
Next(Last(L)) = P;
Prev(P) = Last(L);
Next(P) = First(L);
Prev(First(L)) = P;
}
}
void insertAfter(List &L, address P, address Prec)
{
if(First(L) == NULL)
{
insertFirst(L,P);
}else if(Prec == Last(L)){
insertLast(L, P);
} else {
address Q = Prec;
Q = Next(Q);
Next(P) = Next(Prec);
Next(Prec) = P;
Prev(P) = Prec;
Prev(Q) = P;
}
}
void deleteFirst(List &L, address &P)
{
P = First(L);
First(L) = Next(P);
Next(Last(L)) = First(L);
Prev(First(L)) = Last(L);
Next(P) = NULL;
Prev(P) = NULL;
dealokasi(P);
}
void deleteLast(List &L, address &P)
{
if(Next(First(L)) == NULL)
{
deleteFirst(L,P);
}
else
{
address Q = Last(L);
Last(L) = Prev(Q);
Next(Last(L)) = First(L);
Prev(First(L)) = Last(L);
Next(Q) = NULL;
Prev(Q) = NULL;
dealokasi(P);
}
}
void deleteAfter(List &L, address &P, address &Prec)
{
if(Prec == Last(L)){
deleteFirst(L, P);
}else{
P = Next(Prec);
Next(Prec) = Next(P);
Prev(Next(P)) = Prec;
Next(P) = NULL;
Prev(P) = NULL;
dealokasi(P);
}
}
address findElm(List L, infotype x){
address Q = First(L);
if(Info(Q).ID == Info(First(L)).ID){
}else{
Q = Next(Q);
while(Q != First(L) and Info(Q).ID != x.ID){
Q = Next(Q);
}
}
if(Info(Q).ID == x.ID){
return Q;
}else{
return NULL;
}
}