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>
17 #include "linked_list.h"
31 void ll_init(linked_list *list)
34 * List must point to a linked list structure. It is not acceptable
35 * to pass a null pointer to this routine.
39 * Initializes the list to be one with no elements. If list is
40 * NULL, prints an error message and causes the program to crash.
44 fprintf(stderr, "Error: calling ll_init with null pointer.\n");
48 /* This sets everything to zero, which is what we want. */
49 bzero((char *)list, sizeof(linked_list));
52 ll_node *ll_add_node(linked_list *list, ll_end which_end)
57 * Adds a node to one end of the list (as specified by which_end)
58 * and returns a pointer to the node added. which_end is of type
59 * ll_end and should be either ll_head or ll_tail as specified in
60 * list.h. If there is not enough memory to allocate a node,
61 * the program returns NULL.
66 if ((node = (ll_node *)calloc(1, sizeof(ll_node))) != NULL) {
67 if (list->nelements == 0) {
75 list->first->prev = node;
76 node->next = list->first;
80 list->last->next = node;
81 node->prev = list->last;
85 fprintf(stderr, "%s%s",
86 "ll_add_node got a which_end parameter that ",
87 "it can't handle.\n");
98 int ll_delete_node(linked_list *list, ll_node *node)
103 * If node is in list, deletes node and returns LL_SUCCESS.
104 * Otherwise, returns LL_FAILURE. If node contains other data,
105 * it is the responsibility of the caller to free it. Also, since
106 * this routine frees node, after the routine is called, "node"
107 * won't point to valid data.
110 int status = LL_SUCCESS;
111 ll_node *cur_node = NULL;
114 if (list->nelements == 0)
117 for (cur_node = list->first; (cur_node != NULL) && !found;
118 cur_node = cur_node->next) {
119 if (cur_node == node) {
122 cur_node->prev->next = cur_node->next;
124 list->first = cur_node->next;
127 cur_node->next->prev = cur_node->prev;
129 list->last = cur_node->prev;
145 /* ll_add_data is a macro defined in linked_list.h */
147 /* This routine maintains a list of strings preventing duplication. */
148 int ll_string(linked_list *list, ll_s_action action, char *string)
150 int status = LL_SUCCESS;
155 /* Scan the list until we find the string in question */
156 for (cur_node = list->first; cur_node && (status == FALSE);
157 cur_node = cur_node->next)
158 status = (strcmp(string, cur_node->data) == 0);
161 /* Add a string to the list. */
162 if (!ll_string(list, ll_s_check, string)) {
163 if ((cur_node = ll_add_node(list, ll_tail))) {
165 if ((new_string = (char *)calloc(strlen(string) + 1,
167 strcpy(new_string, string);
168 ll_add_data(cur_node, new_string);
178 /* This should never happen */