-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
256 lines (224 loc) · 5.77 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list.h"
/* list.c
* Max Malmer, c17mmr@cs.umu.se
* Laboration 1 5DV088 HT19
* Version 1.0, 2019-09-12
* A implementation of the datatype list as double linked list.
* Use list_free() to clear the memory after use. Give the input 1
* to the list to take control of the memory cleaning. Give it 0
* otherwise.
*/
// ===========INTERNAL DATA TYPES============
struct node {
void *value;
struct node *next;
struct node *previous;
};
struct list {
unsigned int is_struct;
unsigned int num_elements;
struct node *first;
struct node *last;
};
// ===========INTERNAL FUNCTION IMPLEMENTATIONS============
/**
* list_empty() - Creates a new empty list.
* @boolean: The value given for taking control of memory.
*
* Returns: A pointer to a new list.
*/
list *list_empty(int boolean) {
list *l = malloc(sizeof(list));
l->first = malloc(sizeof(struct node));
l->last = malloc(sizeof(struct node));
memory_control(l->first);
memory_control(l->last);
l->is_struct = boolean;
l->first->next = l->last;
l->last->previous = l->first;
l->num_elements = 0;
return l;
}
/**
* list_is_empty() - Checks if the list is empty.
* @l: The specified list.
*
* Returns: A boolean if it's empty or not.
*/
bool list_is_empty(list *l) {
if (l->first->next == l->last) {
return true;
} else {
return false;
}
}
/**
* list_last() - Returns the last element of a given list.
* @l: The specified list.
*
* Returns: The pointer to the endpointer.
*/
pos_node list_last(list *l) {
return l->last;
}
/**
* list_first() - Returns a position the first element of the list.
* @l: The specified list.
*
* Returns: The pointer to the first element.
*/
pos_node list_first(list *l) {
return l->first->next;
}
/**
* list_next() - Gives the next node position of the node given.
* @l: The specified list.
* @n: The current node.
*
* Returns: The pointer to the next element.
*/
pos_node list_next(list *l, pos_node n) {
if (list_is_empty(l)) {
fprintf(stderr, "We're trying to iterate an empty list.\n");
}
return n->next;
}
/**
* list_previous() - Gives the next node position of the node given.
* @l: The specified list.
* @n: The current node.
*
* Returns: The pointer to the next element.
*/
pos_node list_previous(list *l, pos_node n) {
if (list_is_empty(l)) {
fprintf(stderr, "We're trying to iterate an empty list.\n");
}
return n->previous;
}
/**
* list_inspect() - Inspects the the value of the node given.
* @l: The specified list.
* @n: The current node.
*
* Returns: The pointer to the value of the node.
*/
void *list_inspect(list *l, pos_node n) {
if (n == l->first) {
fprintf(stderr, "We're trying to use the startpointer of the list.\n");
}
return n->value;
}
/**
* list_number_of_elements() - Gives the sixe of the list in ints.
* @l: The specified list.
*
* Returns: A pointer to an int which specifies the size of the list.
*/
int list_number_of_elements(list *l) {
return l->num_elements;
}
/**
* list_remove_node() - Removes the node given for a list from it.
* @l: The specified list.
* @n: The current node.
*
* Returns: A pointer to the value after the one deleted.
*/
pos_node list_remove_node(list *l, pos_node n) {
if (n == list_last(l)) {
fprintf(stderr, "We're trying to delete the endpointer.\n");
return n;
}
if (n == l->first) {
fprintf(stderr, "We're trying to delete the startpointer.\n");
return n;
}
pos_node next_node = list_next(l, n);
n->previous->next = n->next;
n->next->previous = n->previous;
if (!l->is_struct) {
free(n->value);
}
free(n);
l->num_elements--;
return next_node;
}
/**
* list_insert_node() - Inserts a node before the node specified.
* @l: The specified list.
* @n: The current node.
* @v: The value to set for the new node.
*
* Returns: A pointer to the new node.
*/
pos_node list_insert_node(list *l, pos_node n, void *v) {
pos_node new_node = malloc(sizeof(struct node));
memory_control(new_node);
new_node->value = v;
new_node->next = n;
new_node->previous = n->previous;
n->previous = new_node;
new_node->previous->next = new_node;
l->num_elements++;
return new_node;
}
/**
* list_replace() - Replaces a given nodes value in the list.
* @n: The node which value is to be replaced.
* @v: The value to replace it.
*
* Returns: Nothing.
*/
void list_replace(pos_node n, void *v) {
n->value = v;
}
/**
* list_free() - Cleans the memory of a given list, it can't be used again.
* @l: The list to be murdered.
*
* Returns: Nothing.
*/
void list_free(list *l) {
if (!l->is_struct) {
pos_node current_node = list_first(l);
while (current_node != list_last(l)) {
current_node = list_remove_node(l, current_node);
current_node = list_next(l, current_node);
}
}
free(l->first);
free(l->last);
free(l);
}
/**
* list_print() - Prints the given list.
* @l: The list to be printed.
*
*/
void list_print(list *l) {
pos_node current_node = list_first(l);
while (current_node != list_last(l)) {
printf("[%p], ", list_inspect(l, current_node));
current_node = list_next(l, current_node);
}
printf("\n");
}
// ===========HELP FUNCTIONS============
/**
* memory_control() - Controls that memory is allocated.
* @memory: Memory to be checked.
*
* Returns: Nothing if everything is okey. Otherwise exit is used and perror
* is used to specify the error.
*/
void memory_control(void *memory) {
if (memory == NULL) {
perror("Memory related error");
exit(errno);
}
}