Graph Convolutional Branch and Bound v1.0.0
A TSP solver that combines a graph convolutional network with a 1-Tree branch-and-bound.
Loading...
Searching...
No Matches
list_functions.c
Go to the documentation of this file.
1
15#include "list_functions.h"
16
17
18List *new_list(void) {
19 List *l = calloc(1, sizeof(List));
20
21 l->size = 0;
22 l->head = l->tail = NULL;
23
24 return l;
25}
26
27
28void del_list(List *list) {
29 if (!list) {
30 return;
31 }
32
33 DllElem *current = list->head;
34 DllElem *next;
35
36 while (current) {
37 next = current->next;
38 free(current);
39 current = next;
40 }
41
42 free(list);
43}
44
45
46DllElem *build_dll_elem(void *value, DllElem *next, DllElem *prev) {
47 DllElem *e = malloc(sizeof(DllElem));
48 e->value = value;
49 e->next = next;
50 e->prev = prev;
51
52 return e;
53}
54
55
56bool is_list_empty(List *list) {
57 return (list == NULL || !(list->head));
58}
59
60
61size_t get_list_size(List *list) {
62 return (list != NULL) ? list->size : 0;
63}
64
65
66void add_elem_list_bottom(List *list, void *element) {
67 if (list == NULL) {
68 return;
69 }
70
71 DllElem *e = build_dll_elem(element, NULL, list->tail);
72
73 if (is_list_empty(list))
74 list->head = e;
75 else
76 list->tail->next = e;
77 list->tail = e;
78 list->size++;
79}
80
81
82/*
83 * This method deletes the element at the indicated index.
84 * If the index is greater than the size of the List, no element is removed.
85 */
86void add_elem_list_index(List *list, void *element, size_t index) {
88 if (!list || index > get_list_size(list)) {
89 return;
90 }
91
92// support element is a temporary pointer which avoids losing data
93 DllElem *e;
94 DllElem *supp = list->head;
95
96 for (size_t i = 0; i < index; ++i)
97 supp = supp->next;
98
99 if (supp == list->head) {
100 e = build_dll_elem(element, supp, NULL);
101
102 if (supp == NULL) {
103 list->head = list->tail = e;
104 } else {
105// e->next->prev = e;
106 list->head->prev = e;
107 list->head = e;
108 }
109 } else {
110 if (supp == NULL) {
111 e = build_dll_elem(element, NULL, list->tail);
112 list->tail->next = e;
113 } else {
114 e = build_dll_elem(element, supp, supp->prev);
115 e->next->prev = e;
116 e->prev->next = e;
117 }
118 }
119
120 list->size++;
121}
122
123
124/*
125 * This method deletes the element at the bottom of the List.
126 */
128
129 if (list == NULL || is_list_empty(list)) {
130 return;
131 }
132
133 DllElem *oldTail = list->tail;
134
135 list->tail = oldTail->prev;
136 list->tail->next = NULL;
137
138 free(oldTail);
139 list->size--;
140}
141
142
143/*
144 * This method iteratively finds and deletes the element at the specified index, but only if it doesn't exceed
145 * the size of the List. In this case, instead, no reference gets deleted.
146 */
147void delete_list_elem_index(List *list, size_t index) {
149 if (list == NULL || is_list_empty(list) || index >= get_list_size(list)) {
150 return;
151 }
152
153 DllElem *oldElem;
154 oldElem = list->head;
155
156 for (size_t i = 0; i < index; ++i)
157 oldElem = oldElem->next;
158
159 // Found index to remove!!
160 if (oldElem != list->head) {
161 oldElem->prev->next = oldElem->next;
162 if (oldElem->next != NULL) {
163 oldElem->next->prev = oldElem->prev;
164 } else {
165 list->tail = oldElem->prev;
166 }
167 } else {
168 if (list->head == list->tail) {
169 list->head = list->tail = NULL;
170 } else {
171 list->head = list->head->next;
172 list->head->prev = NULL;
173 }
174 }
175
176 free(oldElem);
177 list->size--;
178}
179
180
181/*
182 * This method iteratively runs through the dllist elements and returns the one at the requested index.
183 * If the index exceeds the size of the List, we instead return no element.
184 */
185void *get_list_elem_index(List *list, size_t index) {
186 if (list == NULL || index >= get_list_size(list)) {
187 return NULL;
188 }
189
190 DllElem *supp; // iteration support element
191 supp = list->head;
192
193 for (size_t i = 0; i < index; ++i)
194 supp = supp->next;
195 return supp->value;
196}
void delete_list_elem_index(List *list, size_t index)
Deletes the DllElem at the indicated index of the List.
void delete_list_elem_bottom(List *list)
Deletes the DllElem at the bottom of the List.
size_t get_list_size(List *list)
Gets the size of the List.
bool is_list_empty(List *list)
Check if the List is empty.
void add_elem_list_bottom(List *list, void *element)
Adds an DllElem to the bottom of the List.
void add_elem_list_index(List *list, void *element, size_t index)
Adds an DllElem at the index indicated of the List.
void del_list(List *list)
Delete an instance of a List.
DllElem * build_dll_elem(void *value, DllElem *next, DllElem *prev)
List * new_list(void)
Create a new instance of a List.
void * get_list_elem_index(List *list, size_t index)
Retrieves a pointer to an DllElem from the List.
The declaration of the functions to manipulate the List.
The double linked List element.
Definition: linked_list.h:27
void * value
The value of the element, void pointer to be able to store any type of data.
Definition: linked_list.h:28
struct DllElem * prev
The previous element in the List.
Definition: linked_list.h:30
struct DllElem * next
The next element in the List.
Definition: linked_list.h:29
The double linked list.
Definition: linked_list.h:35
size_t size
The current size of the List.
Definition: linked_list.h:38
DllElem * head
The head of the list as a DllElem.
Definition: linked_list.h:36
DllElem * tail
The tail of the list as a DllElem.
Definition: linked_list.h:37