2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Unless otherwise specified, all software contained herein is
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ****************************************************************************/
20 /**************************************************************************//**
22 * A simple double-linked list.
24 * @note No thread protection so you will need to use appropriate
25 * synchronization if use spans multiple threads.
27 ****************************************************************************/
32 #include "double_list.h"
35 /**************************************************************************//**
36 * List initialization.
38 * Initialize the list supplied to be empty.
40 * @param list Pointer to the list to be initialized.
43 ******************************************************************************/
44 void dlist_initialize(DLIST * list)
48 /***************************************************************************/
49 /* Check assumptions. */
50 /***************************************************************************/
53 /***************************************************************************/
54 /* Initialize the list as empty. */
55 /***************************************************************************/
62 void * dlist_pop_last(DLIST * list)
65 DLIST_ITEM *current_tail = NULL;
66 DLIST_ITEM *new_tail = NULL;
70 current_tail = list->tail;
71 if (current_tail != NULL)
73 item = current_tail->item;
74 new_tail = current_tail->previous;
82 new_tail->next = NULL;
83 list->tail = new_tail;
91 void dlist_push_first(DLIST * list, void * item)
93 DLIST_ITEM * new_element = NULL;
94 DLIST_ITEM * current_head = NULL;
96 /***************************************************************************/
97 /* Check assumptions. Note that we do allow putting NULL pointers into */
98 /* the list - not sure you'd want to, but let it happen. */
99 /***************************************************************************/
100 assert(list != NULL);
102 current_head = list->head;
104 new_element = malloc(sizeof(DLIST_ITEM));
105 assert(new_element != NULL);
106 new_element->next = current_head;
107 new_element->previous = NULL;
108 new_element->item = item;
109 list->head = new_element;
111 if (current_head != NULL)
113 current_head->previous = new_element;
117 list->tail = new_element;
121 void dlist_push_last(DLIST * list, void * item)
123 DLIST_ITEM * new_element = NULL;
124 DLIST_ITEM * current_tail = NULL;
126 /***************************************************************************/
127 /* Check assumptions. Note that we do allow putting NULL pointers into */
128 /* the list - not sure you'd want to, but let it happen. */
129 /***************************************************************************/
130 assert(list != NULL);
132 current_tail = list->tail;
134 new_element = malloc(sizeof(DLIST_ITEM));
135 assert(new_element != NULL);
136 new_element->next = NULL;
137 new_element->previous = current_tail;
138 new_element->item = item;
139 list->tail = new_element;
141 if (current_tail != NULL)
143 current_tail->next = new_element;
147 list->head = new_element;
151 DLIST_ITEM * dlist_get_first(DLIST * list)
156 DLIST_ITEM * dlist_get_last(DLIST * list)
161 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
166 int dlist_is_empty(DLIST * list)
168 return (list->head == NULL);
171 int dlist_count(DLIST * list)
174 DLIST_ITEM * item = list->head;