-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_bidirect.c
384 lines (295 loc) · 6.79 KB
/
list_bidirect.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct info {
int nKey;
unsigned int nData;
struct info *pPrev;
struct info *pNext;
} ST_INFO;
ST_INFO *gpstInfoTop;
ST_INFO *gpstInfoBottom;
ST_INFO *gpstInfoCurrent;
void FreeAllList( void );
void InitList( void );
int AddList( int, unsigned int );
void RefList( void );
void RefListR( void );
int DelList( void );
int InsList( int, unsigned int );
int SearchListLRU( int );
/*
* リスト全体の領域開放
*/
void FreeAllList( void )
{
ST_INFO *pstTmp = gpstInfoTop;
ST_INFO *pstTmp2 = NULL;
while ( pstTmp ) {
pstTmp2 = pstTmp;
pstTmp = pstTmp->pNext;
free( pstTmp2 );
}
gpstInfoTop = NULL;
gpstInfoBottom = NULL;
gpstInfoCurrent = NULL;
return;
}
/*
* リスト初期化
*/
void InitList( void )
{
if ( gpstInfoTop ) {
FreeAllList();
}
gpstInfoTop = NULL;
gpstInfoBottom = NULL;
gpstInfoCurrent = NULL;
return;
}
/*
* リスト追加
* Bottomの後ろに1要素を追加する
*/
int AddList( int nKey, unsigned int nData )
{
ST_INFO *pstTmp = NULL;
pstTmp = (ST_INFO*)malloc( sizeof(ST_INFO) );
if ( !pstTmp ) {
perror( "malloc()" );
return -1;
}
/* データをセット */
pstTmp->nKey = nKey;
pstTmp->nData = nData;
if ( !gpstInfoTop ) {
/* リストが空の場合 */
/* 前後はNULLとなる */
pstTmp->pPrev = NULL;
pstTmp->pNext = NULL;
/* Top,Bottom,Currentにセット */
gpstInfoTop = pstTmp;
gpstInfoBottom = pstTmp;
gpstInfoCurrent = pstTmp;
} else {
/* リストが空でない場合 */
/* 前はBottom、後はNULLとなる */
pstTmp->pPrev = gpstInfoBottom;
pstTmp->pNext = NULL;
/* Bottomの後ろに繋ぎ 自分がBottom及びCurrentになる */
gpstInfoBottom->pNext = pstTmp;
gpstInfoBottom = pstTmp;
gpstInfoCurrent = pstTmp;
}
return 0;
}
/*
* リスト参照
*/
void RefList( void )
{
ST_INFO *pstTmp = gpstInfoTop;
while ( pstTmp ) {
printf(
"%snKey=[%d] nData=[0x%08x] Addr:[%p] ( pPrev:[%p] pNext:[%p] )\n",
pstTmp == gpstInfoCurrent ? "*" : " ",
pstTmp->nKey,
pstTmp->nData,
pstTmp,
pstTmp->pPrev,
pstTmp->pNext
);
pstTmp = pstTmp->pNext;
}
return;
}
/*
* リスト参照 逆順
*/
void RefListR( void )
{
ST_INFO *pstTmp = gpstInfoBottom;
while ( pstTmp ) {
printf(
"%snKey=[%d] nData=[0x%08x] Addr:[%p] ( pPrev:[%p] pNext:[%p] )\n",
pstTmp == gpstInfoCurrent ? "*" : " ",
pstTmp->nKey,
pstTmp->nData,
pstTmp,
pstTmp->pPrev,
pstTmp->pNext
);
pstTmp = pstTmp->pPrev;
}
return;
}
/*
* リスト削除 (領域開放と繋ぎ変え)
* Currentが指す1要素を削除する
*/
int DelList( void )
{
ST_INFO *pstTmp = gpstInfoCurrent;
if ( !pstTmp ) {
return -1;
}
if ( gpstInfoTop == gpstInfoBottom ) {
/* リスト要素が1つの場合 */
gpstInfoTop = NULL;
gpstInfoBottom = NULL;
gpstInfoCurrent = NULL;
} else {
/* リスト要素が複数の場合 */
if ( pstTmp == gpstInfoTop ) {
/* Topである場合 */
/* 自分の後ろをTop及びCurrentとする */
gpstInfoTop = pstTmp->pNext;
gpstInfoCurrent = pstTmp->pNext;
pstTmp->pNext->pPrev = NULL;
} else if ( pstTmp == gpstInfoBottom ) {
/* Bottomである場合 */
/* 自分の前をBottom及びCurrentとする */
gpstInfoBottom = pstTmp->pPrev;
gpstInfoCurrent = pstTmp->pPrev;
pstTmp->pPrev->pNext = NULL;
} else {
/*
* TopでもBottomでない場合
* 自分を飛び越してポインタをつなぐ
* Currentは後ろ側とする
*/
pstTmp->pNext->pPrev = pstTmp->pPrev;
pstTmp->pPrev->pNext = pstTmp->pNext;
gpstInfoCurrent = pstTmp->pNext;
}
}
free( pstTmp );
return 0;
}
/*
* リスト挿入
* Currentが指すリスト要素の後ろに追加する
*/
int InsList( int nKey, unsigned int nData )
{
ST_INFO *pstTmp = NULL;
pstTmp = (ST_INFO*)malloc( sizeof(ST_INFO) );
if ( !pstTmp ) {
perror( "malloc()" );
return -1;
}
/* データをセット */
pstTmp->nKey = nKey;
pstTmp->nData = nData;
if ( !gpstInfoTop ) {
/* リストが空の場合 */
/* 前後はNULLとなる */
pstTmp->pPrev = NULL;
pstTmp->pNext = NULL;
/* Top,Bottom,Currentにセット */
gpstInfoTop = pstTmp;
gpstInfoBottom = pstTmp;
gpstInfoCurrent = pstTmp;
} else {
/* リストが空でない場合 */
if ( gpstInfoCurrent == gpstInfoBottom ) {
/* CurrentがBottomの場合 */
/* 前は元々のBottom、後はNULLとなる */
pstTmp->pPrev = gpstInfoBottom;
pstTmp->pNext = NULL;
/* 元々のBottomの後ろに繋ぎ 自分がBottom及びCurrentになる */
gpstInfoBottom->pNext = pstTmp;
gpstInfoBottom = pstTmp;
gpstInfoCurrent = pstTmp;
} else {
/* 元々のCurrentの後に追加する */
pstTmp->pPrev = gpstInfoCurrent;
pstTmp->pNext = gpstInfoCurrent->pNext;
gpstInfoCurrent->pNext = pstTmp;
/* 上で書き換えたので 次の次の前 */
gpstInfoCurrent->pNext->pNext->pPrev = pstTmp;
gpstInfoCurrent = pstTmp;
}
}
return 0;
}
/*
* リストを検索
* 検索された要素をTopに移動する
*/
int SearchListLRU( int nKey )
{
ST_INFO *pstTmp = gpstInfoTop;
if ( !gpstInfoTop ) {
return -1;
}
while ( pstTmp ) {
if ( pstTmp->nKey == nKey ) {
if( gpstInfoTop == gpstInfoBottom ){
/* リスト要素が1つの場合 */
/* 何もしない */
} else {
/* リスト要素が複数の場合 */
if ( pstTmp == gpstInfoTop ) {
/* Topが検索された場合 何もしない */
} else if ( pstTmp == gpstInfoBottom ) {
/* Bottomが検索された場合 */
/* Bottomを自分の一つ前に */
gpstInfoBottom = pstTmp->pPrev;
gpstInfoBottom->pNext = NULL;
/* 自分がTopの前に入る */
pstTmp->pNext = gpstInfoTop;
gpstInfoTop->pPrev = pstTmp;
pstTmp->pPrev = NULL;
/* 自分がTopに */
gpstInfoTop = pstTmp;
} else {
/* TopでもBottomでもない場合 */
/* 自分を飛び越してポインタをつなぐ */
pstTmp->pNext->pPrev = pstTmp->pPrev;
pstTmp->pPrev->pNext = pstTmp->pNext;
/* 自分がTopの前に入る */
pstTmp->pNext = gpstInfoTop;
gpstInfoTop->pPrev = pstTmp;
pstTmp->pPrev = NULL;
/* 自分がTopに */
gpstInfoTop = pstTmp;
}
}
break;
}
pstTmp = pstTmp->pNext;
}
return 0;
}
int main( void )
{
InitList();
if ( AddList( 1, 0x00000001 ) ) {
exit( EXIT_FAILURE );
}
AddList( 2, 0x00000002 );
AddList( 3, 0x00000003 );
AddList( 4, 0x00000004 );
RefList();
puts("");
gpstInfoCurrent = gpstInfoCurrent->pPrev->pPrev;
DelList();
RefList();
puts("");
InsList( 10, 0x11110001 );
InsList( 11, 0x11110002 );
RefList();
puts("");
SearchListLRU(11);
SearchListLRU(1);
SearchListLRU(3);
RefList();
puts("");
FreeAllList();
RefList();
puts("");
exit( EXIT_SUCCESS );
}