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(c) <2016>, AT&T Intellectual Property. All other rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement: This product includes
23 * software developed by the AT&T.
24 * 4. Neither the name of AT&T nor the names of its contributors may be used to
25 * endorse or promote products derived from this software without specific
26 * prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *****************************************************************************/
43 #include "double_list.h"
46 /**************************************************************************//**
47 * List initialization.
49 * Initialize the list supplied to be empty.
51 * @param list Pointer to the list to be initialized.
54 ******************************************************************************/
55 void dlist_initialize(DLIST * list)
59 /***************************************************************************/
60 /* Check assumptions. */
61 /***************************************************************************/
64 /***************************************************************************/
65 /* Initialize the list as empty. */
66 /***************************************************************************/
73 void * dlist_pop_last(DLIST * list)
76 DLIST_ITEM *current_tail = NULL;
77 DLIST_ITEM *new_tail = NULL;
81 current_tail = list->tail;
82 if (current_tail != NULL)
84 item = current_tail->item;
85 new_tail = current_tail->previous;
93 new_tail->next = NULL;
94 list->tail = new_tail;
102 void dlist_push_first(DLIST * list, void * item)
104 DLIST_ITEM * new_element = NULL;
105 DLIST_ITEM * current_head = NULL;
107 /***************************************************************************/
108 /* Check assumptions. Note that we do allow putting NULL pointers into */
109 /* the list - not sure you'd want to, but let it happen. */
110 /***************************************************************************/
111 assert(list != NULL);
113 current_head = list->head;
115 new_element = malloc(sizeof(DLIST_ITEM));
116 assert(new_element != NULL);
117 new_element->next = current_head;
118 new_element->previous = NULL;
119 new_element->item = item;
120 list->head = new_element;
122 if (current_head != NULL)
124 current_head->previous = new_element;
128 list->tail = new_element;
132 void dlist_push_last(DLIST * list, void * item)
134 DLIST_ITEM * new_element = NULL;
135 DLIST_ITEM * current_tail = NULL;
137 /***************************************************************************/
138 /* Check assumptions. Note that we do allow putting NULL pointers into */
139 /* the list - not sure you'd want to, but let it happen. */
140 /***************************************************************************/
141 assert(list != NULL);
143 current_tail = list->tail;
145 new_element = malloc(sizeof(DLIST_ITEM));
146 assert(new_element != NULL);
147 new_element->next = NULL;
148 new_element->previous = current_tail;
149 new_element->item = item;
150 list->tail = new_element;
152 if (current_tail != NULL)
154 current_tail->next = new_element;
158 list->head = new_element;
162 DLIST_ITEM * dlist_get_first(DLIST * list)
167 DLIST_ITEM * dlist_get_last(DLIST * list)
172 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
177 int dlist_is_empty(DLIST * list)
179 return (list->head == NULL);
182 int dlist_count(DLIST * list)
185 DLIST_ITEM * item = list->head;