1 /**************************************************************************//**
3 * A simple double-linked list.
5 * @note No thread protection so you will need to use appropriate
6 * synchronization if use spans multiple threads.
11 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 ****************************************************************************/
29 #include "double_list.h"
32 /**************************************************************************//**
33 * List initialization.
35 * Initialize the list supplied to be empty.
37 * @param list Pointer to the list to be initialized.
40 ******************************************************************************/
41 void dlist_initialize(DLIST * list)
45 /***************************************************************************/
46 /* Check assumptions. */
47 /***************************************************************************/
50 /***************************************************************************/
51 /* Initialize the list as empty. */
52 /***************************************************************************/
59 void * dlist_pop_last(DLIST * list)
62 DLIST_ITEM *current_tail = NULL;
63 DLIST_ITEM *new_tail = NULL;
67 current_tail = list->tail;
68 if (current_tail != NULL)
70 item = current_tail->item;
71 new_tail = current_tail->previous;
79 new_tail->next = NULL;
80 list->tail = new_tail;
88 void dlist_push_first(DLIST * list, void * item)
90 DLIST_ITEM * new_element = NULL;
91 DLIST_ITEM * current_head = NULL;
93 /***************************************************************************/
94 /* Check assumptions. Note that we do allow putting NULL pointers into */
95 /* the list - not sure you'd want to, but let it happen. */
96 /***************************************************************************/
99 current_head = list->head;
101 new_element = malloc(sizeof(DLIST_ITEM));
102 assert(new_element != NULL);
103 new_element->next = current_head;
104 new_element->previous = NULL;
105 new_element->item = item;
106 list->head = new_element;
108 if (current_head != NULL)
110 current_head->previous = new_element;
114 list->tail = new_element;
118 void dlist_push_last(DLIST * list, void * item)
120 DLIST_ITEM * new_element = NULL;
121 DLIST_ITEM * current_tail = NULL;
123 /***************************************************************************/
124 /* Check assumptions. Note that we do allow putting NULL pointers into */
125 /* the list - not sure you'd want to, but let it happen. */
126 /***************************************************************************/
127 assert(list != NULL);
129 current_tail = list->tail;
131 new_element = malloc(sizeof(DLIST_ITEM));
132 assert(new_element != NULL);
133 new_element->next = NULL;
134 new_element->previous = current_tail;
135 new_element->item = item;
136 list->tail = new_element;
138 if (current_tail != NULL)
140 current_tail->next = new_element;
144 list->head = new_element;
148 DLIST_ITEM * dlist_get_first(DLIST * list)
153 DLIST_ITEM * dlist_get_last(DLIST * list)
158 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
163 int dlist_is_empty(DLIST * list)
165 return (list->head == NULL);
168 int dlist_count(DLIST * list)
171 DLIST_ITEM * item = list->head;