linked_list.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include "linked_list.h"
14 
15 // #define LL_VALIDATE_INPUT // Uncomment to check that inputs are valid, not NULL.
16 
18 {
19 #ifdef LL_VALIDATE_INPUT
20  // Validate Input
21  if( ll==0 )
22  return;
23 #endif
24 
25  ll->head = 0;
26  ll->tail = 0;
27 }
28 
29 
31 {
32 #ifdef LL_VALIDATE_INPUT
33  // Validate Input
34  if( ll==0 || newNode==0 )
35  return;
36 #endif
37 
38  if( ll->head )
39  { // Non-empty linked list.
40  ll->head->prev = newNode;
41  newNode->nextCt = ll->head;
42  newNode->prev = 0;
43  ll->head = newNode;
44  }
45  else
46  { // Empty linked list. Add to head.
47  ll->head = newNode;
48  ll->tail = newNode;
49  }
50 }
51 
52 
54 {
55  linked_list_node_t *prev;
56 
57 #ifdef LL_VALIDATE_INPUT
58  // Validate Input
59  if( ll==0 || node==0 || newNode==0 )
60  return;
61 #endif
62 
63  // Reference previous node
64  prev = (linked_list_node_t*)(node->prev);
65 
66  // Prev <-> newNode
67  if( prev )
68  { // Node is NOT head of linked list (prev exists).
69  prev->nextCt = newNode;
70  newNode->prev = prev;
71  }
72  else
73  { // Node is first. Insert at head.
74  ll->head = newNode;
75  newNode->prev = 0;
76  }
77 
78  // newNode <-> next node
79  node->prev = newNode;
80  newNode->nextCt = node;
81 }
82 
83 
85 {
86  linked_list_node_t* prev;
87  linked_list_node_t* next;
88 
89 #ifdef LL_VALIDATE_INPUT
90  // Validate Input
91  if( ll==0 || node==0 )
92  return;
93 #endif
94 
95  // Reference adjacent nodes
96  prev = (linked_list_node_t*)(node->prev);
97  next = (linked_list_node_t*)(node->nextCt);
98 
99  // Update Head and Tail pointers if needed
100  if( ll->head == node )
101  ll->head = next;
102 
103  if( ll->tail == node )
104  ll->tail = prev;
105 
106  // Remove item from linked list, connect adjacent nodes
107  if(prev)
108  prev->nextCt = next;
109 
110  if(next)
111  next->prev = prev;
112 }
113 
114 
void linkedListInsertBefore(linked_list_t *ll, linked_list_node_t *node, linked_list_node_t *newNode)
Definition: linked_list.c:53
linked_list_node_t * head
Definition: linked_list.h:31
void linkedListRemove(linked_list_t *ll, linked_list_node_t *node)
Definition: linked_list.c:84
void linkedListClear(linked_list_t *ll)
Definition: linked_list.c:17
linked_list_node_t * tail
Definition: linked_list.h:32
void linkedListInsertAtHead(linked_list_t *ll, linked_list_node_t *newNode)
Definition: linked_list.c:30


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57