-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllist_custom_node.c
66 lines (55 loc) · 2.55 KB
/
dllist_custom_node.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
#include <stdio.h>
#include <stdlib.h>
#include "../src/text.h"
#include "../src/lists/double/dllist.h"
// If you want to implement a superset of the standard list node, you can use custom structures.
// In this case, the standard `free` function can be used to free this structure.
// However, if you have allocated memory that is only used in this structure and that isn't `*data`,
// you WILL need to implement a custom freeing function that should be given to `dllist_free`.
struct my_list_node {
// First, we need to re-implement the structure members from DoubleLinkedListNode / struct double_linked_list_node.
// See: "src/lists/structs.h"
void *data;
DoubleLinkedListNode *next;
DoubleLinkedListNode *previous;
// Now we can add our custom fields.
// This would mainly be used for hashmaps to store their hash in a more efficient way.
int myNodeCustomValue;
};
// This function is used to allocate the memory for our custom list node ``,
// and to also populate the custom members.
// A pointer to this function gets passed to `dllist_append`.
DoubleLinkedListNode *allocateMyNode() {
struct my_list_node *myNode = calloc(1, sizeof(struct my_list_node));
if(myNode != NULL) {
// The random numbers will always be the same due to the limited randomness of `rand()`.
myNode->myNodeCustomValue = rand() % 100;
}
return (DoubleLinkedListNode *) myNode;
}
int main() {
// Creating a list.
DoubleLinkedList *myList = dllist_create();
// Adding some custom nodes with no standard data.
// The only data we will care about in this case is the node's `myNodeCustomValue` member.
dllist_append(myList, NULL, &allocateMyNode);
dllist_append(myList, NULL, &allocateMyNode);
dllist_append(myList, NULL, &allocateMyNode);
dllist_append(myList, NULL, &allocateMyNode);
dllist_append(myList, NULL, &allocateMyNode);
// Iterating over the list node by node while typecasting to our custom node.
printf("Iteration:\n");
struct my_list_node *loopNode = (struct my_list_node *) dllist_selectFirst(myList);
while(loopNode != NULL) {
printf("> %d\n", loopNode->myNodeCustomValue);
// Preparing the next node.
loopNode = (struct my_list_node *) dllist_selectNext(myList);
}
printf("\n");
// Freeing the list from memory.
// We pass `NULL` for the data freeing part since we haven't allocated anything in this example.
// The second callback can be left as NULL since our custom node can be freed with the standard `free` function
// that automatically gets used since we passed NULL in this case.
printf("Freeing the list...\n");
dllist_free(myList, NULL, NULL);
}