Fix error when RS232 not enabled
[contiki-2.x.git] / core / lib / list.c
blobc37041692296928bac9f247bf7d6a3ad64c01f05
1 /**
2 * \addtogroup list
3 * @{
4 */
6 /**
7 * \file
8 * Linked list library implementation.
10 * \author Adam Dunkels <adam@sics.se>
15 * Copyright (c) 2004, Swedish Institute of Computer Science.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the Institute nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
42 * This file is part of the Contiki operating system.
44 * Author: Adam Dunkels <adam@sics.se>
46 * $Id: list.c,v 1.5 2010/06/15 18:54:27 adamdunkels Exp $
48 #include "lib/list.h"
50 #define NULL 0
52 struct list {
53 struct list *next;
56 /*---------------------------------------------------------------------------*/
57 /**
58 * Initialize a list.
60 * This function initalizes a list. The list will be empty after this
61 * function has been called.
63 * \param list The list to be initialized.
65 void
66 list_init(list_t list)
68 *list = NULL;
70 /*---------------------------------------------------------------------------*/
71 /**
72 * Get a pointer to the first element of a list.
74 * This function returns a pointer to the first element of the
75 * list. The element will \b not be removed from the list.
77 * \param list The list.
78 * \return A pointer to the first element on the list.
80 * \sa list_tail()
82 void *
83 list_head(list_t list)
85 return *list;
87 /*---------------------------------------------------------------------------*/
88 /**
89 * Duplicate a list.
91 * This function duplicates a list by copying the list reference, but
92 * not the elements.
94 * \note This function does \b not copy the elements of the list, but
95 * merely duplicates the pointer to the first element of the list.
97 * \param dest The destination list.
98 * \param src The source list.
100 void
101 list_copy(list_t dest, list_t src)
103 *dest = *src;
105 /*---------------------------------------------------------------------------*/
107 * Get the tail of a list.
109 * This function returns a pointer to the elements following the first
110 * element of a list. No elements are removed by this function.
112 * \param list The list
113 * \return A pointer to the element after the first element on the list.
115 * \sa list_head()
117 void *
118 list_tail(list_t list)
120 struct list *l;
122 if(*list == NULL) {
123 return NULL;
126 for(l = *list; l->next != NULL; l = l->next);
128 return l;
130 /*---------------------------------------------------------------------------*/
132 * Add an item at the end of a list.
134 * This function adds an item to the end of the list.
136 * \param list The list.
137 * \param item A pointer to the item to be added.
139 * \sa list_push()
142 void
143 list_add(list_t list, void *item)
145 struct list *l;
147 /* Make sure not to add the same element twice */
148 list_remove(list, item);
150 ((struct list *)item)->next = NULL;
152 l = list_tail(list);
154 if(l == NULL) {
155 *list = item;
156 } else {
157 l->next = item;
160 /*---------------------------------------------------------------------------*/
162 * Add an item to the start of the list.
164 void
165 list_push(list_t list, void *item)
167 /* struct list *l;*/
169 /* Make sure not to add the same element twice */
170 list_remove(list, item);
172 ((struct list *)item)->next = *list;
173 *list = item;
175 /*---------------------------------------------------------------------------*/
177 * Remove the last object on the list.
179 * This function removes the last object on the list and returns it.
181 * \param list The list
182 * \return The removed object
185 void *
186 list_chop(list_t list)
188 struct list *l, *r;
190 if(*list == NULL) {
191 return NULL;
193 if(((struct list *)*list)->next == NULL) {
194 l = *list;
195 *list = NULL;
196 return l;
199 for(l = *list; l->next->next != NULL; l = l->next);
201 r = l->next;
202 l->next = NULL;
204 return r;
206 /*---------------------------------------------------------------------------*/
208 * Remove the first object on a list.
210 * This function removes the first object on the list and returns a
211 * pointer to it.
213 * \param list The list.
214 * \return Pointer to the removed element of list.
216 /*---------------------------------------------------------------------------*/
217 void *
218 list_pop(list_t list)
220 struct list *l;
221 l = *list;
222 if(*list != NULL) {
223 *list = ((struct list *)*list)->next;
226 return l;
228 /*---------------------------------------------------------------------------*/
230 * Remove a specific element from a list.
232 * This function removes a specified element from the list.
234 * \param list The list.
235 * \param item The item that is to be removed from the list.
238 /*---------------------------------------------------------------------------*/
239 void
240 list_remove(list_t list, void *item)
242 struct list *l, *r;
244 if(*list == NULL) {
245 return;
248 r = NULL;
249 for(l = *list; l != NULL; l = l->next) {
250 if(l == item) {
251 if(r == NULL) {
252 /* First on list */
253 *list = l->next;
254 } else {
255 /* Not first on list */
256 r->next = l->next;
258 l->next = NULL;
259 return;
261 r = l;
264 /*---------------------------------------------------------------------------*/
266 * Get the length of a list.
268 * This function counts the number of elements on a specified list.
270 * \param list The list.
271 * \return The length of the list.
273 /*---------------------------------------------------------------------------*/
275 list_length(list_t list)
277 struct list *l;
278 int n = 0;
280 for(l = *list; l != NULL; l = l->next) {
281 ++n;
284 return n;
286 /*---------------------------------------------------------------------------*/
288 * \brief Insert an item after a specified item on the list
289 * \param list The list
290 * \param previtem The item after which the new item should be inserted
291 * \param newitem The new item that is to be inserted
292 * \author Adam Dunkels
294 * This function inserts an item right after a specified
295 * item on the list. This function is useful when using
296 * the list module to ordered lists.
298 * If previtem is NULL, the new item is placed at the
299 * start of the list.
302 void
303 list_insert(list_t list, void *previtem, void *newitem)
305 if(previtem == NULL) {
306 list_push(list, newitem);
307 } else {
309 ((struct list *)newitem)->next = ((struct list *)previtem)->next;
310 ((struct list *)previtem)->next = newitem;
313 /*---------------------------------------------------------------------------*/
315 * \brief Get the next item following this item
316 * \param item A list item
317 * \returns A next item on the list
319 * This function takes a list item and returns the next
320 * item on the list, or NULL if there are no more items on
321 * the list. This function is used when iterating through
322 * lists.
324 void *
325 list_item_next(void *item)
327 return item == NULL? NULL: ((struct list *)item)->next;
329 /*---------------------------------------------------------------------------*/
330 /** @} */