4 * This file contains general linked list routines.
6 * Copyright 1990,1991 by the Massachusetts Institute of Technology
7 * For distribution and copying rights, see the file "mit-copyright.h"
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 #include "linked_list.h"
25 void ll_init(linked_list
*list
)
28 * List must point to a linked list structure. It is not acceptable
29 * to pass a null pointer to this routine.
33 * Initializes the list to be one with no elements. If list is
34 * NULL, prints an error message and causes the program to crash.
38 fprintf(stderr
, "Error: calling ll_init with null pointer.\n");
42 /* This sets everything to zero, which is what we want. */
43 bzero((char *)list
, sizeof(linked_list
));
46 ll_node
*ll_add_node(linked_list
*list
, ll_end which_end
)
51 * Adds a node to one end of the list (as specified by which_end)
52 * and returns a pointer to the node added. which_end is of type
53 * ll_end and should be either ll_head or ll_tail as specified in
54 * list.h. If there is not enough memory to allocate a node,
55 * the program returns NULL.
60 if ((node
= calloc(1, sizeof(ll_node
))) != NULL
) {
61 if (list
->nelements
== 0) {
69 list
->first
->prev
= node
;
70 node
->next
= list
->first
;
74 list
->last
->next
= node
;
75 node
->prev
= list
->last
;
79 fprintf(stderr
, "%s%s",
80 "ll_add_node got a which_end parameter that ",
81 "it can't handle.\n");
92 int ll_delete_node(linked_list
*list
, ll_node
*node
)
97 * If node is in list, deletes node and returns LL_SUCCESS.
98 * Otherwise, returns LL_FAILURE. If node contains other data,
99 * it is the responsibility of the caller to free it. Also, since
100 * this routine frees node, after the routine is called, "node"
101 * won't point to valid data.
104 ll_node
*cur_node
= NULL
;
106 if (list
->nelements
== 0)
109 for (cur_node
= list
->first
; cur_node
!= NULL
; cur_node
= cur_node
->next
) {
110 if (cur_node
== node
) {
113 cur_node
->prev
->next
= cur_node
->next
;
115 list
->first
= cur_node
->next
;
118 cur_node
->next
->prev
= cur_node
->prev
;
120 list
->last
= cur_node
->prev
;
132 /* ll_add_data is a macro defined in linked_list.h */
134 /* This routine maintains a list of strings preventing duplication. */
135 int ll_string(linked_list
*list
, ll_s_action action
, char *string
)
137 int status
= LL_SUCCESS
;
142 /* Scan the list until we find the string in question */
143 for (cur_node
= list
->first
; cur_node
&& (status
== FALSE
);
144 cur_node
= cur_node
->next
)
145 status
= (strcmp(string
, cur_node
->data
) == 0);
148 /* Add a string to the list. */
149 if (!ll_string(list
, ll_s_check
, string
)) {
150 if ((cur_node
= ll_add_node(list
, ll_tail
))) {
152 if ((new_string
= calloc(strlen(string
) + 1, sizeof(char)))) {
153 strcpy(new_string
, string
);
154 ll_add_data(cur_node
, new_string
);
164 /* This should never happen */