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.
17 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
19 ****************************************************************************/
21 /**************************************************************************//**
23 * A simple double-linked list.
25 * @note No thread protection so you will need to use appropriate
26 * synchronization if use spans multiple threads.
28 ****************************************************************************/
33 #include "double_list.h"
36 /**************************************************************************//**
37 * List initialization.
39 * Initialize the list supplied to be empty.
41 * @param list Pointer to the list to be initialized.
44 ******************************************************************************/
45 void dlist_initialize(DLIST * list)
49 /***************************************************************************/
50 /* Check assumptions. */
51 /***************************************************************************/
54 /***************************************************************************/
55 /* Initialize the list as empty. */
56 /***************************************************************************/
63 void * dlist_pop_last(DLIST * list)
66 DLIST_ITEM *current_tail = NULL;
67 DLIST_ITEM *new_tail = NULL;
71 current_tail = list->tail;
72 if (current_tail != NULL)
74 item = current_tail->item;
75 new_tail = current_tail->previous;
83 new_tail->next = NULL;
84 list->tail = new_tail;
92 void dlist_push_first(DLIST * list, void * item)
94 DLIST_ITEM * new_element = NULL;
95 DLIST_ITEM * current_head = NULL;
97 /***************************************************************************/
98 /* Check assumptions. Note that we do allow putting NULL pointers into */
99 /* the list - not sure you'd want to, but let it happen. */
100 /***************************************************************************/
101 assert(list != NULL);
103 current_head = list->head;
105 new_element = malloc(sizeof(DLIST_ITEM));
106 assert(new_element != NULL);
107 new_element->next = current_head;
108 new_element->previous = NULL;
109 new_element->item = item;
110 list->head = new_element;
112 if (current_head != NULL)
114 current_head->previous = new_element;
118 list->tail = new_element;
122 void dlist_push_last(DLIST * list, void * item)
124 DLIST_ITEM * new_element = NULL;
125 DLIST_ITEM * current_tail = NULL;
127 /***************************************************************************/
128 /* Check assumptions. Note that we do allow putting NULL pointers into */
129 /* the list - not sure you'd want to, but let it happen. */
130 /***************************************************************************/
131 assert(list != NULL);
133 current_tail = list->tail;
135 new_element = malloc(sizeof(DLIST_ITEM));
136 assert(new_element != NULL);
137 new_element->next = NULL;
138 new_element->previous = current_tail;
139 new_element->item = item;
140 list->tail = new_element;
142 if (current_tail != NULL)
144 current_tail->next = new_element;
148 list->head = new_element;
152 DLIST_ITEM * dlist_get_first(DLIST * list)
157 DLIST_ITEM * dlist_get_last(DLIST * list)
162 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
167 int dlist_is_empty(DLIST * list)
169 return (list->head == NULL);
172 int dlist_count(DLIST * list)
175 DLIST_ITEM * item = list->head;