blob: 64811abdfc55e5de4f9386eb06f9310ed5f33831 [file] [log] [blame]
adamdunkelsce7f02a2005-02-07 07:52:31 +00001/**
2 * \defgroup list Linked list library
3 * @{
4 *
5 * The linked list library provides a set of functions for
6 * manipulating linked lists.
7 *
8 * A linked list is made up of elements where the first element \b
9 * must be a pointer. This pointer is used by the linked list library
10 * to form lists of the elements.
11 *
12 * Lists are declared with the LIST() macro. The declaration specifies
13 * the name of the list that later is used with all list functions.
14 *
15 * Example:
16 \code
17
18struct packet {
19 struct packet *next;
20 char data[1500];
21 int len;
22};
23
24LIST(packets);
25
26static void
27init_function(void) {
28 list_init(packets);
29}
30
31static void
32another_function(struct packet *p) {
33 list_add(packets, p);
34
35 p = list_head(packets);
36
37 p = list_tail(packets);
38}
39 \endcode
40 *
41 * Lists can be manipulated by inserting or removing elements from
42 * either sides of the list (list_push(), list_add(), list_pop(),
43 * list_chop()). A specified element can also be removed from inside a
44 * list with list_remove(). The head and tail of a list can be
45 * extracted using list_head() and list_tail(), respecitively.
46 */
47
48/**
49 * \file
50 * Linked list library implementation.
51 *
52 * \author Adam Dunkels <adam@sics.se>
53 *
54 */
55
adamdunkelsa2f3c422004-09-12 20:24:53 +000056/*
57 * Copyright (c) 2004, Swedish Institute of Computer Science.
58 * All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 * 3. Neither the name of the Institute nor the names of its contributors
69 * may be used to endorse or promote products derived from this software
70 * without specific prior written permission.
71 *
72 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82 * SUCH DAMAGE.
83 *
84 * This file is part of the Contiki operating system.
85 *
86 * Author: Adam Dunkels <adam@sics.se>
87 *
adamdunkelsce7f02a2005-02-07 07:52:31 +000088 * $Id: list.c,v 1.4 2005/02/07 07:52:31 adamdunkels Exp $
adamdunkelsa2f3c422004-09-12 20:24:53 +000089 */
adamdunkels97435a92004-02-16 20:47:51 +000090#include "list.h"
91
92#define NULL 0
93
94struct list {
95 struct list *next;
96};
97
adamdunkelsce7f02a2005-02-07 07:52:31 +000098/*---------------------------------------------------------------------------*/
99/**
100 * Initialize a list.
101 *
102 * This function initalizes a list. The list will be empty after this
103 * function has been called.
104 *
105 * \param list The list to be initialized.
106 */
adamdunkels97435a92004-02-16 20:47:51 +0000107void
PulkoMandy3e213902014-06-26 11:08:29 +0200108list_init(void** list)
adamdunkels97435a92004-02-16 20:47:51 +0000109{
110 *list = NULL;
111}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000112/*---------------------------------------------------------------------------*/
113/**
114 * Get a pointer to the first element of a list.
115 *
116 * This function returns a pointer to the first element of the
117 * list. The element will \b not be removed from the list.
118 *
119 * \param list The list.
120 * \return A pointer to the first element on the list.
121 *
122 * \sa list_tail()
123 */
adamdunkels97435a92004-02-16 20:47:51 +0000124void *
PulkoMandy3e213902014-06-26 11:08:29 +0200125list_head(void** list)
adamdunkels97435a92004-02-16 20:47:51 +0000126{
127 return *list;
128}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000129/*---------------------------------------------------------------------------*/
130/**
131 * Duplicate a list.
132 *
133 * This function duplicates a list by copying the list reference, but
134 * not the elements.
135 *
136 * \note This function does \b not copy the elements of the list, but
137 * merely duplicates the pointer to the first element of the list.
138 *
139 * \param dest The destination list.
140 * \param src The source list.
141 */
adamdunkels97435a92004-02-16 20:47:51 +0000142void
PulkoMandy3e213902014-06-26 11:08:29 +0200143list_copy(void** dest, void** src)
adamdunkels97435a92004-02-16 20:47:51 +0000144{
145 *dest = *src;
146}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000147/*---------------------------------------------------------------------------*/
148/**
149 * Get the tail of a list.
150 *
151 * This function returns a pointer to the elements following the first
152 * element of a list. No elements are removed by this function.
153 *
154 * \param list The list
155 * \return A pointer to the element after the first element on the list.
156 *
157 * \sa list_head()
158 */
adamdunkels97435a92004-02-16 20:47:51 +0000159void *
PulkoMandy3e213902014-06-26 11:08:29 +0200160list_tail(void** list)
adamdunkels97435a92004-02-16 20:47:51 +0000161{
162 struct list *l;
163
164 if(*list == NULL) {
165 return NULL;
166 }
167
168 for(l = *list; l->next != NULL; l = l->next);
169
170 return l;
171}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000172/*---------------------------------------------------------------------------*/
adamdunkelsd28b1e12004-08-20 21:41:04 +0000173/**
adamdunkelsce7f02a2005-02-07 07:52:31 +0000174 * Add an item at the end of a list.
175 *
176 * This function adds an item to the end of the list.
177 *
178 * \param list The list.
179 * \param item A pointer to the item to be added.
180 *
181 * \sa list_push()
adamdunkelsd28b1e12004-08-20 21:41:04 +0000182 *
183 */
adamdunkels97435a92004-02-16 20:47:51 +0000184void
PulkoMandy3e213902014-06-26 11:08:29 +0200185list_add(void** list, void *item)
adamdunkels97435a92004-02-16 20:47:51 +0000186{
187 struct list *l;
188
189 ((struct list *)item)->next = NULL;
190
191 l = list_tail(list);
192
193 if(l == NULL) {
194 *list = item;
195 } else {
196 l->next = item;
197 }
198}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000199/*---------------------------------------------------------------------------*/
adamdunkels97435a92004-02-16 20:47:51 +0000200/**
adamdunkelsd28b1e12004-08-20 21:41:04 +0000201 * Add an item to the start of the list.
202 */
203void
PulkoMandy3e213902014-06-26 11:08:29 +0200204list_push(void** list, void *item)
adamdunkelsd28b1e12004-08-20 21:41:04 +0000205{
adamdunkelsce7f02a2005-02-07 07:52:31 +0000206 /* struct list *l;*/
adamdunkelsd28b1e12004-08-20 21:41:04 +0000207
208 ((struct list *)item)->next = *list;
209 *list = item;
210}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000211/*---------------------------------------------------------------------------*/
adamdunkelsd28b1e12004-08-20 21:41:04 +0000212/**
213 * Remove the last object on the list.
214 *
adamdunkelsce7f02a2005-02-07 07:52:31 +0000215 * This function removes the last object on the list and returns it.
216 *
217 * \param list The list
218 * \return The removed object
219 *
adamdunkelsd28b1e12004-08-20 21:41:04 +0000220 */
221void *
PulkoMandy3e213902014-06-26 11:08:29 +0200222list_chop(void** list)
adamdunkelsd28b1e12004-08-20 21:41:04 +0000223{
224 struct list *l, *r;
225
226 if(*list == NULL) {
227 return NULL;
228 }
229 if(((struct list *)*list)->next == NULL) {
230 l = *list;
231 *list = NULL;
232 return l;
233 }
234
235 for(l = *list; l->next->next != NULL; l = l->next);
236
237 r = l->next;
238 l->next = NULL;
239
240 return r;
241}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000242/*---------------------------------------------------------------------------*/
adamdunkelsd28b1e12004-08-20 21:41:04 +0000243/**
adamdunkelsce7f02a2005-02-07 07:52:31 +0000244 * Remove the first object on a list.
adamdunkels97435a92004-02-16 20:47:51 +0000245 *
adamdunkelsce7f02a2005-02-07 07:52:31 +0000246 * This function removes the first object on the list and returns it.
247 *
248 * \param list The list.
249 * \return The new head of the list.
adamdunkels97435a92004-02-16 20:47:51 +0000250 */
adamdunkelsce7f02a2005-02-07 07:52:31 +0000251/*---------------------------------------------------------------------------*/
adamdunkels97435a92004-02-16 20:47:51 +0000252void *
PulkoMandy3e213902014-06-26 11:08:29 +0200253list_pop(void** list)
adamdunkels97435a92004-02-16 20:47:51 +0000254{
255 struct list *l;
256
257 if(*list != NULL) {
258 *list = ((struct list *)*list)->next;
259 }
260
261 return *list;
262}
adamdunkelsce7f02a2005-02-07 07:52:31 +0000263/*---------------------------------------------------------------------------*/
264/**
265 * Remove a specific element from a list.
266 *
267 * This function removes a specified element from the list.
268 *
269 * \param list The list.
270 * \param item The item that is to be removed from the list.
271 *
272 */
273/*---------------------------------------------------------------------------*/
274void
PulkoMandy3e213902014-06-26 11:08:29 +0200275list_remove(void** list, void *item)
adamdunkelsce7f02a2005-02-07 07:52:31 +0000276{
277 struct list *l, *r;
278
279 if(*list == NULL) {
280 return;
281 }
282
283 r = NULL;
284 for(l = *list; l != NULL; l = l->next) {
285 if(l == item) {
286 if(r == NULL) {
287 /* First on list */
288 *list = l->next;
289 } else {
290 /* Not first on list */
291 r->next = l->next;
292 }
293 l->next = NULL;
294 return;
295 }
296 r = l;
297 }
298}
299/*---------------------------------------------------------------------------*/
300/**
301 * Get the length of a list.
302 *
303 * This function counts the number of elements on a specified list.
304 *
305 * \param list The list.
306 * \return The length of the list.
307 */
308/*---------------------------------------------------------------------------*/
309int
PulkoMandy3e213902014-06-26 11:08:29 +0200310list_length(void** list)
adamdunkelsce7f02a2005-02-07 07:52:31 +0000311{
312 struct list *l;
313 int n = 0;
314
315 for(l = *list; l != NULL; l = l->next) {
316 ++n;
317 }
318
319 return n;
320}
321/*---------------------------------------------------------------------------*/
322
323/** @} */