liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
/*
 *   Copyright (c) International Business Machines Corp., 2001-2004
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <stdlib.h>
#include <assert.h>
 
#include "cirlist.h"
#include "util.h"
 
void init_cirlist(struct cirlist *cl)
{
   cl->count = 0;
   cl->head = NULL;
}
 
int cl_empty(struct cirlist *cl)
{
   return !(cl->count);
}
 
void cl_insert_tail(struct cirlist *cl, cldatatype object)
{
   struct cnode *new = ffsb_malloc(sizeof(struct cnode));
   new->obj = object;
   if (cl->count == 0) {
       assert(cl->head == NULL);
       cl->head = new;
       cl->head->next = cl->head;
       cl->head->prev = cl->head;
       cl->count = 1;
   } else {
       if (cl->count == 1) {
           assert(cl->head->next == cl->head);
           assert(cl->head->prev == cl->head);
           cl->head->next = new;
           cl->head->prev = new;
           new->next = cl->head;
           new->prev = cl->head;
       } else {
           assert(cl->head->next != cl->head);
           assert(cl->head->prev != cl->head);
 
           new->next = cl->head;
           new->prev = (cl->head)->prev;
           cl->head->prev->next = new;
           cl->head->prev = new;
       }
       cl->count++;
   }
}
 
cldatatype cl_remove_head(struct cirlist *cl)
{
   struct cnode *oldhead = NULL;
   struct cnode *newhead = NULL;
   cldatatype ret = NULL;
 
   if (cl->count == 0) {
       assert(cl->head == NULL);
       return NULL;
   }
   if (cl->count == 1) {
       assert(cl->head->next == cl->head);
       assert(cl->head->prev == cl->head);
       oldhead = cl->head;
       cl->head = NULL;
       cl->count = 0;
   } else if (cl->count == 2) {
       oldhead = cl->head;
       newhead = oldhead->next;
       newhead->next = newhead;
       newhead->prev = newhead;
       cl->head = newhead;
       cl->count = 1;
   } else {
       assert(cl->head->next != cl->head);
       assert(cl->head->prev != cl->head);
       oldhead = cl->head;
       newhead = oldhead->next;
       newhead->prev = oldhead->prev;
       newhead->prev->next = newhead;
       cl->head = newhead;
       cl->count--;
   }
   ret = oldhead->obj;
   oldhead->obj = (void *)(-1);
   oldhead->next = (void *)(-1);
   oldhead->prev = (void *)(-1);
   free(oldhead);
 
   return ret;
}