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
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
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 $
56 /*---------------------------------------------------------------------------*/
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.
66 list_init(list_t list
)
70 /*---------------------------------------------------------------------------*/
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.
83 list_head(list_t list
)
87 /*---------------------------------------------------------------------------*/
91 * This function duplicates a list by copying the list reference, but
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.
101 list_copy(list_t dest
, list_t 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.
118 list_tail(list_t list
)
126 for(l
= *list
; l
->next
!= NULL
; l
= l
->next
);
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.
143 list_add(list_t list
, void *item
)
147 /* Make sure not to add the same element twice */
148 list_remove(list
, item
);
150 ((struct list
*)item
)->next
= NULL
;
160 /*---------------------------------------------------------------------------*/
162 * Add an item to the start of the list.
165 list_push(list_t list
, void *item
)
169 /* Make sure not to add the same element twice */
170 list_remove(list
, item
);
172 ((struct list
*)item
)->next
= *list
;
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
186 list_chop(list_t list
)
193 if(((struct list
*)*list
)->next
== NULL
) {
199 for(l
= *list
; l
->next
->next
!= NULL
; l
= l
->next
);
206 /*---------------------------------------------------------------------------*/
208 * Remove the first object on a list.
210 * This function removes the first object on the list and returns a
213 * \param list The list.
214 * \return Pointer to the removed element of list.
216 /*---------------------------------------------------------------------------*/
218 list_pop(list_t list
)
223 *list
= ((struct list
*)*list
)->next
;
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 /*---------------------------------------------------------------------------*/
240 list_remove(list_t list
, void *item
)
249 for(l
= *list
; l
!= NULL
; l
= l
->next
) {
255 /* Not first on list */
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
)
280 for(l
= *list
; l
!= NULL
; l
= l
->next
) {
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
303 list_insert(list_t list
, void *previtem
, void *newitem
)
305 if(previtem
== NULL
) {
306 list_push(list
, newitem
);
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
325 list_item_next(void *item
)
327 return item
== NULL
? NULL
: ((struct list
*)item
)->next
;
329 /*---------------------------------------------------------------------------*/