[go: up one dir, main page]

Menu

[r456]: / tags / 0.6.4 / src / llist.c  Maximize  Restore  History

Download this file

184 lines (147 with data), 3.8 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
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
/* $Id$
* -------------------------------------------------------
* Copyright (C) 2002-2005 Tommi Saviranta <wnd@iki.fi>
* (C) 2002 Lee Hardy <lee@leeh.co.uk>
* * -------------------------------------------------------
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* ifdef HAVE_CONFIG_H */
#include "llist.h"
#include "common.h"
/*
* Create a new node with no links to other nodes.
*
* Return pointer to this node.
*/
llist_node *
llist_create(void *data)
{
llist_node *node;
node = (llist_node *) xmalloc(sizeof(llist_node));
node->data = data;
/*
* Basically we wouldn't have to do this...
node->next = NULL;
node->prev = NULL;
*/
return node;
} /* llist_node *llist_create(void *data) */
/*
* Add node to the beginning of the list.
*/
void
llist_add(llist_node *node, llist_list *list)
{
/* New node has no previous node. */
node->next = list->head;
node->prev = NULL;
if (list->head != NULL) {
list->head->prev = node;
} else if (list->tail == NULL) {
list->tail = node;
}
list->head = node;
} /* void llist_add(llist_node *node, llist_list *list) */
/*
* Add node to the end of list.
*/
void
llist_add_tail(llist_node *node, llist_list *list)
{
node->next = NULL;
node->prev = list->tail;
if (list->head == NULL) {
list->head = node;
} else if (list->tail != NULL) {
list->tail->next = node;
}
list->tail = node;
} /* void llist_add_tail(llist_node *node, llist_list *list) */
/*
* Delete links to node and free memory allocated by node.
*
* Note that user _must_ free memory that was possibly allocated in
* node->data.
*/
void
llist_delete(llist_node *node, llist_list *list)
{
/* Item is at head. */
if (node->prev == NULL) {
list->head = node->next;
} else {
node->prev->next = node->next;
}
/* Item is at tail. */
if (node->next == NULL) {
list->tail = node->prev;
} else {
node->next->prev = node->prev;
}
/*
* Free some resources. Use _must_ free m->data _before_ calling
* llist_delete.
*/
xfree(node);
} /* void llist_delete(llist_node *node, llist_list *list) */
/*
* Find node that points to data.
*
* Return pointer to this node.
*/
llist_node *
llist_find(void *data, llist_list *list)
{
llist_node *ptr;
for (ptr = list->head; ptr; ptr = ptr->next) {
if (ptr->data == data) {
return ptr;
}
}
return NULL;
} /* llist_node *llist_find(void *data, llist_list *list) */
#ifdef OBSOLETE /* Ignore this. */
/*
* We don't want to use this - it just takes more space that doind it by
* hand time after time.
*/
void
llist_empty(llist_list *list, void (* action) (void *))
{
llist_node *node = list->head;
llist_node *nextnode;
while (node != NULL) {
nextnode = node->next;
action(node->data);
llist_delete(node, list);
node = nextnode;
}
} /* void llist_empty(llist_list *list, void (* action) (void *) */
llist_node **
llist_get_indexed(const llist_list *list)
{
llist_node **ret = xmalloc(sizeof(llist_node *));
llist_node *ptr = list->head;
int size = 0;
do {
if (ptr != NULL) {
ret = xrealloc(ret, sizeof(llist_node *) * (size + 2));
ret[size] = ptr;
size++;
ptr = ptr->next;
}
ret[size] = NULL;
} while (ptr != NULL);
return ret;
} /* llist_node **llist_get_indexed(const llist_list *list) */
#endif /* OBSOLETE */