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

// Linked list of ANYTHING

typedef struct _Node {  // canonical type name is 'struct _Node'
    // int   value; // v1 - linked list of ints
    // char* value; // v2 - linked list of strings
    void* a_value;  // v3 - linked list of anything
    struct _Node* next;
} Node;  // shortcut (typedef) name is 'Node'

void destroy_list(Node** a_head, Node** a_tail) {
    while(*a_head != NULL) {  // While list is not empty…
        Node* new_head = (*a_head) -> next;  // 1. Save (address of) new head (or NULL)
        //free((*a_head) -> value);            // 2. Free whatever head node refers to.
        free(*a_head);                       // 3. Free the old head.
        *a_head = new_head;                  // 4. Set the head to the new_head.
    }
    *a_tail = NULL;                          // 5. Set the tail to NULL.
    // Order is important.  If we free before saving the (address of) new_head, we won't 
    // be able to access it.
    
    assert(*a_head == NULL);
}

void print_list(Node* head, void (*print_value_fn)(void*)) {  // void* keeps this generic
    for(Node* curr = head; curr != NULL; curr = curr -> next) {
        printf("[");
        print_value_fn(curr -> a_value);  // PRINT THIS VALUE
        printf("]");
        printf(curr -> next != NULL ? "→" : "\n");
    }
}

// void append(int value, Node** a_head, Node** a_tail) {   // v1 - linked list of ints
// void append(char* value, Node** a_head, Node** a_tail) { // v2 - linked list of strings
void append(void* a_value, Node** a_head, Node** a_tail) {  // v3 - linked list of anything

    // 1. Allocate memory for the new node.
    Node* new_tail = malloc(sizeof(*new_tail));

    // 2. Initialize the fields of the new node.
    *new_tail = (Node) { .a_value = a_value, .next = NULL };

    // 3. Depending on whether or not the list was initially empty…
    if(*a_head == NULL) {  // If list is empty (size 0)…
        // … Set the head (*a_head) to the (address of) the new tail, or …
        *a_head = new_tail; // type of  *a_head is Node*.  type of new_tail is Node*.
    }
    else {
        // … Connect the old tail to the new tail.
        (*a_tail) -> next = new_tail;
    }

    // 4. Set the tail (*a_tail) to the (address of the) new tail.
    *a_tail = new_tail;
    // IMPORTANT:  This must come last.
}

void print_string(void* a_value) {
    char* s = a_value;  // No typecast needed because in C, you can…
                        // ∙ Assign a void* to a variable declared as ▒▒▒▒*, or
                        // ∙ Assign a ▒▒▒▒* to a variable declared as void*, or
    printf("\"%s\"", s);
}

void* get_max_value(Node const* head, int (*compare_fn)(void*,void*)) {
    // compare_fn(a_value_1, a_value_2) returns ≥1 if *a_value_2 is regarded
    // as "greater" than *a_value_1.

    void* a_max_value = NULL;   // NULL indicates no max value (because list is empty)
    for(Node const* curr = head; curr != NULL; curr = curr -> next) {
        if(a_max_value == NULL) {
            a_max_value = curr -> a_value;
        }
        else if(compare_fn(a_max_value, curr -> a_value) >= 1) {
            a_max_value = curr -> a_value;
        }
    }
    return a_max_value;
}

int compare_strings(void* a_value_1, void* a_value_2) {
    char* s1 = a_value_1;
    char* s2 = a_value_2;
    return strcmp(s1, s2);
}

int main(int argc, char* argv[]) {
    Node* head = NULL; // Linked list of size 0 (empty list)
    Node* tail = NULL;

    char* s1 = "AB";
    append(s1, &head, &tail);
    assert(head == tail); // In a list of size 1, head and tail refer to the same Node.

    char* s2 = "CD";
    append(s2, &head, &tail);
    assert(head != tail); // In a list of size 2, head and tail DON'T refer to the same Node
    //assert(tail -> value == 11);
    char* s3 = "EF";
    append(s3, &head, &tail);

    // Call print_list(…) with head and the address of a function that prints a string,
    // given as the address of the first character in that string.
    print_list(head, print_string);

    char* s_max = get_max_value(head, compare_strings);
    printf("MAX: %s\n", s_max);
    destroy_list(&head, &tail);
    assert(head == NULL);
    assert(tail == NULL);

    return EXIT_SUCCESS;
}

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