Contiki-NG
Loading...
Searching...
No Matches
list.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 */
34
35/**
36 * \file
37 * Linked list library implementation.
38 *
39 * \author Adam Dunkels <adam@sics.se>
40 *
41 */
42
43/**
44 * \addtogroup list
45 * @{
46 */
47#include "contiki.h"
48#include "lib/list.h"
49
50#include <string.h>
51/*---------------------------------------------------------------------------*/
52struct list {
53 struct list *next;
54};
55/*---------------------------------------------------------------------------*/
56void *
58{
59 struct list *l;
60
61 if(*list == NULL) {
62 return NULL;
63 }
64
65 for(l = *list; l->next != NULL; l = l->next);
66
67 return l;
68}
69/*---------------------------------------------------------------------------*/
70void
71list_add(list_t list, void *item)
72{
73 struct list *l;
74
75 /* Make sure not to add the same element twice */
76 list_remove(list, item);
77
78 ((struct list *)item)->next = NULL;
79
80 l = list_tail(list);
81
82 if(l == NULL) {
83 *list = item;
84 } else {
85 l->next = item;
86 }
87}
88/*---------------------------------------------------------------------------*/
89void
90list_push(list_t list, void *item)
91{
92 /* Make sure not to add the same element twice */
93 list_remove(list, item);
94
95 ((struct list *)item)->next = *list;
96 *list = item;
97}
98/*---------------------------------------------------------------------------*/
99void *
101{
102 struct list *l, *r;
103
104 if(*list == NULL) {
105 return NULL;
106 }
107 if(((struct list *)*list)->next == NULL) {
108 l = *list;
109 *list = NULL;
110 return l;
111 }
112
113 for(l = *list; l->next->next != NULL; l = l->next);
114
115 r = l->next;
116 l->next = NULL;
117
118 return r;
119}
120/*---------------------------------------------------------------------------*/
121void *
123{
124 struct list *l;
125 l = *list;
126 if(*list != NULL) {
127 *list = ((struct list *)*list)->next;
128 }
129
130 return l;
131}
132/*---------------------------------------------------------------------------*/
133void
134list_remove(list_t list, const void *item)
135{
136 struct list *l, *r;
137
138 if(*list == NULL) {
139 return;
140 }
141
142 r = NULL;
143 for(l = *list; l != NULL; l = l->next) {
144 if(l == item) {
145 if(r == NULL) {
146 /* First on list */
147 *list = l->next;
148 } else {
149 /* Not first on list */
150 r->next = l->next;
151 }
152 l->next = NULL;
153 return;
154 }
155 r = l;
156 }
157}
158/*---------------------------------------------------------------------------*/
159int
161{
162 struct list *l;
163 int n = 0;
164
165 for(l = *list; l != NULL; l = l->next) {
166 ++n;
167 }
168
169 return n;
170}
171/*---------------------------------------------------------------------------*/
172void
173list_insert(list_t list, void *previtem, void *newitem)
174{
175 if(previtem == NULL) {
176 list_push(list, newitem);
177 } else {
178 list_remove(list, newitem);
179 ((struct list *)newitem)->next = ((struct list *)previtem)->next;
180 ((struct list *)previtem)->next = newitem;
181 }
182}
183/*---------------------------------------------------------------------------*/
184bool
185list_contains(const_list_t list, const void *item)
186{
187 struct list *l;
188 for(l = *list; l != NULL; l = l->next) {
189 if(item == l) {
190 return true;
191 }
192 }
193 return false;
194}
195/*---------------------------------------------------------------------------*/
196/** @} */
void * list_chop(list_t list)
Remove the last object on the list.
Definition list.c:100
int list_length(const_list_t list)
Get the length of a list.
Definition list.c:160
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition list.c:71
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition list.c:134
void * list_pop(list_t list)
Remove the first object on a list.
Definition list.c:122
void ** list_t
The linked list type.
Definition list.h:136
void list_push(list_t list, void *item)
Add an item to the start of the list.
Definition list.c:90
bool list_contains(const_list_t list, const void *item)
Check if the list contains an item.
Definition list.c:185
void *const * const_list_t
The non-modifiable linked list type.
Definition list.h:141
void * list_tail(const_list_t list)
Get the tail of a list.
Definition list.c:57
void list_insert(list_t list, void *previtem, void *newitem)
Insert an item after a specified item on the list.
Definition list.c:173
Linked list manipulation routines.