-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathlist.c
More file actions
98 lines (91 loc) · 2.22 KB
/
list.c
File metadata and controls
98 lines (91 loc) · 2.22 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
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
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
#include "generic_printf.h"
/**
* @brief Create a new node with data _val_ and set the next node to _net_
* @param val Specifiy the data to assign to the new node
* @param next Pointer to the next node
* @return Pointer to the created new node
*/
static node_t *node_new(val_t val, node_t *next)
{
/* allocate node */
node_t *node = malloc(sizeof(node_t));
node->data = val;
node->next = next;
return node;
}
/**
* @brief Initialize the linked list.
*
* The function will allocate a block of memory for _llist\_t_ and
* initialize its members _head_ to NULL and _size_ to 0.
*
* @return Pointer to the allocated _llist\_t_
*/
llist_t *list_new()
{
/* allocate list */
llist_t *list = malloc(sizeof(llist_t));
list->head = NULL;
list->size = 0;
return list;
}
/**
* @brief Insert a new node with the given value val at the head of the _list_
* @param list The target linked list
* @param val Specify the value
* @return The final size of the linked list
*/
int list_add(llist_t * const list, const val_t val)
{
node_t *e = node_new(val, NULL);
e->next = list->head;
list->head = e;
list->size++;
return list->size;
}
/**
* @brief Get the node specified by index
* If the index is out of range, it will return NULL.
*
* @param list The target linked list
* @param index Specify the index of the node in the _list_
* @return The node at index _index_.
*/
node_t *list_get(llist_t * const list, const uint32_t index)
{
uint32_t idx = index;
if (!(idx < list->size))
return NULL;
node_t *head = list->head;
while (idx--)
head = head->next;
return head;
}
/**
* @brief Display the data of all nodes in the linked list
* @param list The target linked list
*/
void list_print(const llist_t * const list)
{
const node_t *cur = list->head;
while (cur) {
xprintln(cur->data);
cur = cur->next;
}
}
/**
* @brief Release the memory allocated to nodes in the linked list
* @param list The target linked list
*/
void list_free_nodes(llist_t *list)
{
node_t *cur = list->head;
while (cur) {
cur = cur->next;
free(cur);
}
list->head = NULL;
}