2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ****************************************************************************/
19 /**************************************************************************//**
21 * A simple double-linked list.
23 * @note No thread protection so you will need to use appropriate
24 * synchronization if use spans multiple threads.
26 ****************************************************************************/
31 #include "double_list.h"
34 /**************************************************************************//**
35 * List initialization.
37 * Initialize the list supplied to be empty.
39 * @param list Pointer to the list to be initialized.
42 ******************************************************************************/
43 void dlist_initialize(DLIST * list)
47 /***************************************************************************/
48 /* Check assumptions. */
49 /***************************************************************************/
52 /***************************************************************************/
53 /* Initialize the list as empty. */
54 /***************************************************************************/
61 void * dlist_pop_last(DLIST * list)
64 DLIST_ITEM *current_tail = NULL;
65 DLIST_ITEM *new_tail = NULL;
69 current_tail = list->tail;
70 if (current_tail != NULL)
72 item = current_tail->item;
73 new_tail = current_tail->previous;
81 new_tail->next = NULL;
82 list->tail = new_tail;
90 void dlist_push_first(DLIST * list, void * item)
92 DLIST_ITEM * new_element = NULL;
93 DLIST_ITEM * current_head = NULL;
95 /***************************************************************************/
96 /* Check assumptions. Note that we do allow putting NULL pointers into */
97 /* the list - not sure you'd want to, but let it happen. */
98 /***************************************************************************/
101 current_head = list->head;
103 new_element = malloc(sizeof(DLIST_ITEM));
104 assert(new_element != NULL);
105 new_element->next = current_head;
106 new_element->previous = NULL;
107 new_element->item = item;
108 list->head = new_element;
110 if (current_head != NULL)
112 current_head->previous = new_element;
116 list->tail = new_element;
120 void dlist_push_last(DLIST * list, void * item)
122 DLIST_ITEM * new_element = NULL;
123 DLIST_ITEM * current_tail = NULL;
125 /***************************************************************************/
126 /* Check assumptions. Note that we do allow putting NULL pointers into */
127 /* the list - not sure you'd want to, but let it happen. */
128 /***************************************************************************/
129 assert(list != NULL);
131 current_tail = list->tail;
133 new_element = malloc(sizeof(DLIST_ITEM));
134 assert(new_element != NULL);
135 new_element->next = NULL;
136 new_element->previous = current_tail;
137 new_element->item = item;
138 list->tail = new_element;
140 if (current_tail != NULL)
142 current_tail->next = new_element;
146 list->head = new_element;
150 DLIST_ITEM * dlist_get_first(DLIST * list)
155 DLIST_ITEM * dlist_get_last(DLIST * list)
160 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
165 int dlist_is_empty(DLIST * list)
167 return (list->head == NULL);
170 int dlist_count(DLIST * list)
173 DLIST_ITEM * item = list->head;