AT&T ECOMP Vendor Event Listener library  0.1
double_list.c
Go to the documentation of this file.
1 /**************************************************************************/
40 #include <assert.h>
41 #include <malloc.h>
42 
43 #include "double_list.h"
44 #include "evel.h"
45 
46 /**************************************************************************/
55 void dlist_initialize(DLIST * list)
56 {
57  EVEL_ENTER();
58 
59  /***************************************************************************/
60  /* Check assumptions. */
61  /***************************************************************************/
62  assert(list != NULL);
63 
64  /***************************************************************************/
65  /* Initialize the list as empty. */
66  /***************************************************************************/
67  list->head = NULL;
68  list->tail = NULL;
69 
70  EVEL_EXIT();
71 }
72 
73 void * dlist_pop_last(DLIST * list)
74 {
75  void *item = NULL;
76  DLIST_ITEM *current_tail = NULL;
77  DLIST_ITEM *new_tail = NULL;
78 
79  assert(list != NULL);
80 
81  current_tail = list->tail;
82  if (current_tail != NULL)
83  {
84  item = current_tail->item;
85  new_tail = current_tail->previous;
86  if (new_tail == NULL)
87  {
88  list->head = NULL;
89  list->tail = NULL;
90  }
91  else
92  {
93  new_tail->next = NULL;
94  list->tail = new_tail;
95  }
96  free(current_tail);
97  }
98 
99  return item;
100 }
101 
102 void dlist_push_first(DLIST * list, void * item)
103 {
104  DLIST_ITEM * new_element = NULL;
105  DLIST_ITEM * current_head = NULL;
106 
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);
112 
113  current_head = list->head;
114 
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;
121 
122  if (current_head != NULL)
123  {
124  current_head->previous = new_element;
125  }
126  else
127  {
128  list->tail = new_element;
129  }
130 }
131 
132 void dlist_push_last(DLIST * list, void * item)
133 {
134  DLIST_ITEM * new_element = NULL;
135  DLIST_ITEM * current_tail = NULL;
136 
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);
142 
143  current_tail = list->tail;
144 
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;
151 
152  if (current_tail != NULL)
153  {
154  current_tail->next = new_element;
155  }
156  else
157  {
158  list->head = new_element;
159  }
160 }
161 
163 {
164  return list->head;
165 }
166 
168 {
169  return list->tail;
170 }
171 
173 {
174  return item->next;
175 }
176 
178 {
179  return (list->head == NULL);
180 }
181 
182 int dlist_count(DLIST * list)
183 {
184  int count = 0;
185  DLIST_ITEM * item = list->head;
186 
187  while (item != NULL)
188  {
189  count++;
190  item = item->next;
191  }
192 
193  return count;
194 }
DLIST_ITEM * head
Definition: double_list.h:55
DLIST_ITEM * dlist_get_first(DLIST *list)
Definition: double_list.c:162
void dlist_initialize(DLIST *list)
List initialization.
Definition: double_list.c:55
DLIST_ITEM * tail
Definition: double_list.h:56
struct dlist_item * next
Definition: double_list.h:46
void * item
Definition: double_list.h:47
void dlist_push_first(DLIST *list, void *item)
Definition: double_list.c:102
A simple double-linked list.
int dlist_count(DLIST *list)
Definition: double_list.c:182
#define EVEL_EXIT()
Definition: evel.h:3631
#define EVEL_ENTER()
Definition: evel.h:3626
Header for EVEL library.
struct dlist_item * previous
Definition: double_list.h:45
DLIST_ITEM * dlist_get_last(DLIST *list)
Definition: double_list.c:167
void * dlist_pop_last(DLIST *list)
Definition: double_list.c:73
void dlist_push_last(DLIST *list, void *item)
Definition: double_list.c:132
DLIST_ITEM * dlist_get_next(DLIST_ITEM *item)
Definition: double_list.c:172
Double-linked list structure.
Definition: double_list.h:53
int dlist_is_empty(DLIST *list)
Definition: double_list.c:177