-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlist.cpp
More file actions
73 lines (66 loc) · 1.52 KB
/
list.cpp
File metadata and controls
73 lines (66 loc) · 1.52 KB
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
//========================================================================
// List
//========================================================================
// @brief: linked list for reading parameters
#include "list.h"
// make an empty list, return a pointer
list *make_list()
{
list *l = (list *)malloc(sizeof(list));
l->size = 0;
l->front = 0;
l->back = 0;
return l;
}
// insert a new node into list *l with "value":*val
void list_insert(list *l, void *val)
{
node *new_node = (node *)malloc(sizeof(node));
new_node->val = val;
new_node->next = 0;
// add new node to l->back
if(!l->back)
{ // empty list
l->front = new_node;
new_node->prev = 0;
}
else
{
l->back->next = new_node;
new_node->prev = l->back;
}
l->back = new_node;
l->size++; ////
}
// convert a list to 2D array (***array of pointer***)
void **list_to_array(list *l)
{
void **res = (void **)calloc(l->size, sizeof(void *));
int counter = 0;
node *n = l->front; // first node in list l
// convert the list
while(n)
{
res[counter++] = n->val; //
n = n->next;
}
return res;
}
// free memory allocated for the list
void free_list(list *l)
{
free_node(l->front); // first node
free(l);
}
// free node
void free_node(node *n)
{
node *next;
// free all nodes
while(n)
{
next = n->next;
free(n);
n = next;
}
}