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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "append.h"

static void _print_int(void* a_n) {
    int n = *((int*)a_n);
    printf("%d", n);
}

static void _dummy_free_fn(void* a_n) {
    // free(a_n);  // ... if this were a string or other object on the heap
}

static void _print_string(void* a_value) {
    char* s = a_value;
    printf("%s\n", s);
}

static void _free_string(void* a_value) {
    free(a_value);
}

static void _dummy_free_fn(void* a_n) {
    // free(a_n);  // ... if this were a string or other object on the heap
}

// COMPARE FUNCTION WOULD GO IN YOUR TEST_▒▒▒▒▒.c FILE.
void demo_with_list_of_int() {
    Node* head = NULL;
    Node* tail = head;

    int n1 = 5, n2 = 6, n3 = 7;
    append(&n1, &head, &tail);
    append(&n2, &head, &tail);
    append(&n3, &head, &tail);

    print_list(head, _print_int);

    free_list(&head, &tail, _dummy_free_fn);
    assert(head == NULL && tail == NULL);  // List should be empty now.
}

void demo_with_list_of_strings() {
    // Create EMPTY list
    Node* head = NULL;
    Node* tail = head;

    append(_strdup("abc"), &head, &tail);  // _strdup(…) creates string on the heap.
    append(_strdup("def"), &head, &tail);
    append(_strdup("ghi"), &head, &tail);

    print_list(head, _print_string);
    free_list(&head, _free_string); // ↔ free_list(&head, free);
}

int main(int argc, char* argv[]) {

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2021 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.