1 /* Copyright 1999, 1991, 1999 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 /* This file contains general linked list routines. */
18 static const char rcsid[] = "$Id$";
24 #include "linked_list.h"
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.
36 void ll_init(linked_list *list)
40 fprintf(stderr, "Error: calling ll_init with null pointer.\n");
44 list->first = list->last = NULL;
52 * Adds a node to one end of the list (as specified by which_end)
53 * and returns a pointer to the node added. which_end is of type
54 * ll_end and should be either ll_head or ll_tail as specified in
55 * list.h. If there is not enough memory to allocate a node,
56 * the program returns NULL.
58 ll_node *ll_add_node(linked_list *list, ll_end which_end)
62 node = malloc(sizeof(ll_node));
66 if (list->nelements == 0)
71 node->prev = node->next = NULL;
78 list->first->prev = node;
79 node->next = list->first;
84 list->last->next = node;
85 node->prev = list->last;
90 fprintf(stderr, "ll_add_node got a which_end parameter that "
91 "it can't handle.\n");
106 * If node is in list, deletes node and returns LL_SUCCESS.
107 * Otherwise, returns LL_FAILURE. If node contains other data,
108 * it is the responsibility of the caller to free it. Also, since
109 * this routine frees node, after the routine is called, "node"
110 * won't point to valid data.
112 int ll_delete_node(linked_list *list, ll_node *node)
116 for (cur_node = list->first; cur_node; cur_node = cur_node->next)
118 if (cur_node == node)
121 cur_node->prev->next = cur_node->next;
123 list->first = cur_node->next;
126 cur_node->next->prev = cur_node->prev;
128 list->last = cur_node->prev;
139 int ll_string_check(linked_list *list, char *string)
143 /* Scan the list until we find the string in question */
144 for (cur_node = list->first; cur_node; cur_node = cur_node->next)
146 if (strcmp(string, cur_node->data) == 0)
152 /* This routine maintains a list of strings preventing duplication. */
153 int ll_add_string(linked_list *list, char *string)
158 if (!ll_string_check(list, string))
160 node = ll_add_node(list, ll_tail);
163 new_string = strdup(string);
165 ll_add_data(node, new_string);