File: list.c

package info (click to toggle)
inn2 2.4.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,912 kB
  • ctags: 7,860
  • sloc: ansic: 85,104; perl: 11,427; sh: 9,863; makefile: 2,498; yacc: 1,563; python: 298; lex: 252; tcl: 7
file content (125 lines) | stat: -rw-r--r-- 2,133 bytes parent folder | download | duplicates (7)
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
/*  $Id: list.c 6168 2003-01-21 06:27:32Z alexk $
**
*/

#include "config.h"
#include "clibrary.h"

#include "inn/list.h"

void
list_new(struct list *list)
{
    list->head = (struct node *)&list->tail;
    list->tailpred = (struct node *)&list->head;
    list->tail = NULL;
}

struct node *
list_addhead(struct list *list, struct node *node)
{
    node->succ = list->head;
    node->pred = (struct node *)&list->head;
    list->head->pred = node;
    list->head = node;
    return node;
}

struct node *
list_addtail(struct list *list, struct node *node)
{
    node->succ = (struct node *)&list->tail;
    node->pred = list->tailpred;
    list->tailpred->succ = node;
    list->tailpred = node;
    return node;
}

struct node *
list_remhead(struct list *list)
{
    struct node *node;

    node = list->head->succ;
    if (node) {
	node->pred = (struct node *)&list->head;
        node = list->head;
        list->head = node->succ;
    }
    return node;
}

struct node *
list_head(struct list *list)
{
    if (list->head->succ)
	return list->head;
    return NULL;
}

struct node *
list_tail(struct list *list)
{
    if (list->tailpred->pred)
	return list->tailpred;
    return NULL;
}

struct node *
list_succ(struct node *node)
{
    if (node->succ->succ)
	return node->succ;
    return NULL;
}

struct node *
list_pred(struct node *node)
{
    if (node->pred->pred)
	return node->pred;
    return NULL;
}

struct node *
list_remove(struct node *node)
{
    node->pred->succ = node->succ;
    node->succ->pred = node->pred;
    return node;
}

struct node *
list_remtail(struct list *list)
{
    struct node *node;

    node = list_tail(list);
    if (node)
	list_remove(node);
    return node;
}

bool
list_isempty(struct list *list)
{
    return list->tailpred == (struct node *)list;
}

struct node *
list_insert(struct list *list, struct node *node, struct node *pred)
{
    if (pred) {
	if (pred->succ) {
	    node->succ = pred->succ;
	    node->pred = pred;
	    pred->succ->pred = node;
	    pred->succ = node;
	} else {
	    list_addtail(list, node);
	}
    } else {
	list_addhead(list, node);
    }
    return node;
}