CAEN Utility  2.0.2
Utilities for CAEN projects
CAENLinkedList.c
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * CAEN SpA - Software Division
4 * Via Vetraia, 11 - 55049 - Viareggio ITALY
5 * +39 0594 388 398 - www.caen.it
6 *
7 *******************************************************************************
8 *
9 * Copyright (C) 2019-2022 CAEN SpA
10 *
11 * This file is part of the CAEN Utility.
12 *
13 * The CAEN Utility is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * The CAEN Utility is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with the CAEN Utility; if not, see
25 * https://www.gnu.org/licenses/.
26 *
27 * SPDX-License-Identifier: LGPL-3.0-or-later
28 *
29 ******************************************************************************
30 *
31 * \file CAENLinkedList.c
32 * \brief Utilities to implement linked lists
33 * \author Francesco Pepe
34 *
35 ******************************************************************************/
36 
37 #include <CAENLinkedList.h>
38 
39 #include <CAENMultiplatform.h>
40 
42  c_linkedlist_t res = { NULL, NULL };
43  return res;
44 }
45 
48  if (res == NULL)
49  return NULL;
50  res->data = newElement;
51  res->prev = node;
52  if (node != NULL) {
53  res->next = node->next;
54  node->next = res;
55  if (res->next != NULL)
56  res->next->prev = res;
57  }
58  else {
59  res->next = NULL;
60  }
61  return res;
62 }
63 
66  if (res == NULL)
67  return NULL;
68  res->data = newElement;
69  res->next = node;
70  if (node != NULL) {
71  res->prev = node->prev;
72  node->prev = res;
73  if (res->prev != NULL)
74  res->prev->next = res;
75  }
76  else {
77  res->prev = NULL;
78  }
79  return res;
80 }
81 
83  c_listnode_t* res = NULL;
84 
85  if (list == NULL)
87  if ((list->head == NULL || list->tail == NULL) && list->head != list->tail)
89 
90  res = c_linkedlist_insert_after(list->tail, newElement);
91  if (res == NULL)
93  list->tail = res;
94  if (list->head == NULL)
95  list->head = list->tail;
96 
98 }
99 
101  c_listnode_t* res = NULL;
102 
103  if (list == NULL)
105  if ((list->head == NULL || list->tail == NULL) && list->head != list->tail)
107 
108  res = c_linkedlist_insert_before(list->head, newElement);
109  if (res == NULL)
111  list->head = res;
112  if (list->tail == NULL)
113  list->tail = list->head;
114 
116 }
117 
119  void* data;
120  if (node == NULL)
121  return NULL;
122  if (node->prev != NULL)
123  node->prev->next = node->next;
124  if (node->next != NULL)
125  node->next->prev = node->prev;
126  // Update head and tail references if needed.
127  if (list->head == node)
128  list->head = node->next;
129  if (list->tail == node)
130  list->tail = node->prev;
131  // not really useful
132  node->next = NULL;
133  node->prev = NULL;
134  data = node->data;
135  c_free(node);
136  return data;
137 }
138 
139 static int _simplecompare(const void* d1, const void* d2) {
140  return d1 == d2;
141 }
142 
144  c_listnode_t* node;
145  if (list == NULL)
146  return NULL;
147  node = list->head;
148  while (node != NULL && cmp(node->data, data) == 0)
149  node = node->next;
150  if (node == NULL)
151  return NULL;
152  return c_linkedlist_delete(list, node);
153 }
154 
157 }
158 
160  c_listnode_t *tail = NULL;
161 
162  if (list == NULL)
163  return NULL;
164  if ((list->head == NULL || list->tail == NULL) && list->head != list->tail)
165  return NULL;
166 
167  tail = list->tail;
168  if (tail == NULL)
169  return NULL;
170 
171  return c_linkedlist_delete(list, tail);
172 }
173 
175  c_listnode_t* head = NULL;
176 
177  if (list == NULL)
178  return NULL;
179  if ((list->head == NULL || list->tail == NULL) && list->head != list->tail)
180  return NULL;
181 
182  head = list->head;
183  if (head == NULL)
184  return NULL;
185 
186  return c_linkedlist_delete(list, head);
187 }
c_Utility_ErrorCode_t c_linkedlist_insert_first(c_linkedlist_t *list, void *newElement)
static int _simplecompare(const void *d1, const void *d2)
void c_free(void *ptr)
c_linkedlist_t c_linkedlist_create()
c_listnode_t * c_linkedlist_insert_before(c_listnode_t *node, void *newElement)
struct c_listnode_t * next
void * c_linkedlist_delete_data_compare(c_linkedlist_t *list, void *data, c_listdata_comparator_t cmp)
c_listnode_t * head
void * c_linkedlist_delete(c_linkedlist_t *list, c_listnode_t *node)
void * c_malloc(size_t size)
c_Utility_ErrorCode_t c_linkedlist_insert_last(c_linkedlist_t *list, void *newElement)
Generic wrappers to platform-dependent functions.
c_listnode_t * tail
void * c_linkedlist_delete_first(c_linkedlist_t *list)
struct c_listnode_t * prev
#define c_use_decl_annotations
Definition: CAENUtility.h:278
int(* c_listdata_comparator_t)(const void *, const void *)
c_Utility_ErrorCode_t
Library return codes.
Definition: CAENUtility.h:319
void * c_linkedlist_delete_last(c_linkedlist_t *list)
void * c_linkedlist_delete_data(c_linkedlist_t *list, void *data)
c_listnode_t * c_linkedlist_insert_after(c_listnode_t *node, void *newElement)