This repository has been archived by the owner on Jun 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
205 lines (180 loc) · 3.49 KB
/
list.h
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
#ifndef LIST_HEADER_INCLUDED
#define LIST_HEADER_INCLUDED
#include <iostream>
using namespace std;
void createList(List &L)
{
first(L) = Nil;
last(L) = Nil;
}
address allocate(infotype x)
{
address P = new elmList;
info(P) = x;
return P;
}
void deallocate(address &P)
{
delete P;
}
bool isEmpty(List L)
{
if (not (found (first(L)))) return true;
else return false;
}
address findListByID(List L, unsigned long int id)
{
address R = first(L);
while (found (R) && info(R).id != id) R = next(R);
return R;
}
/*address findElm(List L, infotype x)
{
address P = first(L);
while (found (P) && info(P) != x) P = next(P);
return P;
}*/
address getPrecAddr(List L, address Prec)
{
address P = first(L);
while (found (P) && P != Prec) P = next(P);
return P;
}
void insertEmpty(List &L, address P)
{
if (isEmpty(L))
{
first(L) = P;
last(L) = P;
next(P) = Nil;
prev(P) = Nil;
}
}
void insertFirst(List &L, address P)
{
if (isEmpty(L)) insertEmpty(L, P);
else
{
next(P) = first(L);
prev(P) = Nil;
first(L) = P;
}
}
void insertLast(List &L, address P)
{
if (isEmpty(L)) insertEmpty(L, P);
else
{
next(P) = Nil;
prev(P) = last(L);
next(last(L)) = P;
last(L) = P;
}
}
void insertAfter(List &L, address Prec, address P)
{
if (not isEmpty(L))
{
address Q = getPrecAddr(L, Prec);
if (found (Q))
{
next(P) = next(Q);
prev(P) = Q;
prev(next(Q)) = P;
next(Q) = P;
}
}
}
void deleteSingle(List &L, address &P)
{
if (not isEmpty(L) && single (L))
{
P = first(L);
first(L) = Nil;
last(L) = Nil;
deallocate(P);
}
}
void deleteFirst(List &L, address &P)
{
if (not isEmpty(L))
{
if (single (L)) deleteSingle(L, P);
else
{
P = first(L);
first(L) = next(P);
prev(first(L)) = Nil;
next(P) = Nil;
deallocate(P);
}
}
}
void deleteLast(List &L, address &P)
{
if (not isEmpty(L))
{
if (single (L)) deleteSingle(L, P);
else
{
P = last(L);
last(L) = prev(P);
next(last(L)) = Nil;
prev(P) = Nil;
deallocate(P);
}
}
}
void deleteAfter(List &L, address Prec, address &P)
{
if (not isEmpty(L))
{
P = getPrecAddr(L, Prec);
if (found (P) && next(P) != Nil)
{
P = next(P);
prev(next(P)) = prev(P);
next(prev(P)) = next(P);
next(P) = Nil;
prev(P) = Nil;
deallocate(P);
}
}
}
void deleteListByAddress(List &L, address &P)
{
if (single (L)) deleteSingle(L, P);
else if (first(L) == P) deleteFirst(L, P);
else if (not (found(next(P)))) deleteLast(L, P);
else
{
address R = P;
prev(next(P)) = prev(R);
next(prev(P)) = next(R);
next(P) = Nil;
prev(P) = Nil;
deallocate(P);
}
}
void showAllList(List L)
{
address P = first(L);
unsigned long int i, n;
cout << endl;
while (found (P))
{
cout << " Customer ID : " << info(customer(P)).id << endl
<< " Age : " << info(customer(P)).age << endl
<< " Name : " << info(customer(P)).name << endl;
n = account(P).size();
for (i = 0; i < n; i++)
{
cout << " Account ID : " << info(account(P).at(i)).id << endl
<< " Balance : " << info(account(P).at(i)).balance << endl;
}
cout << endl;
P = next(P);
}
cout << endl;
}
#endif