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

typedef struct _Node { // canonical name struct _Node
    //int value;   // v1: integers
    //char* value; // v2: strings
    void* a_value; // v3: anything
    struct _Node* next; // need to use canonical name
} Node; // shortcut name: Node

void print_linked_list(Node* head) {
    for (Node* cur = head; cur != NULL; cur = cur->next) {
        // TODO: get better printing logic
        printf("[%p]", cur->a_value);
        printf("%s", cur->next == NULL ? "\n" : "->");
    }
}

// appends a new value to the linked list after the tail
// mallocing a new node and updating the tail
// works with any size of linked list
void append(void* a_value, Node** a_head, Node** a_tail) {
    // step 1: malloc new node
    Node* new_node = malloc(sizeof(*new_node));
    // step 2: initialize new node
    *new_node = (Node) { .a_value = a_value, .next = NULL };

    // step 3: insert new node into list
    // case 1: list is size 0
    // POTENTIAL BUG: a_head should never be NULL
    //               *a_head might be NULL
    if (*a_head == NULL) {
        // new node is both the new head and new tail
        *a_head = new_node;
    }
    // case 2: list is size > 0
    else {
        // append to the end of the old tail
        (*a_tail)->next = new_node;
        // potential bug: -> has higher precedence than *
        //*a_tail->next = new_node;
    }
    // step 4: update the tail
    // always make the new node the new tail
    *a_tail = new_node;
}

void destroy_linked_list(Node** a_head, Node** a_tail) {
    Node* cur = *a_head;
    while (cur != NULL) {
        Node* next = cur->next;
        free(cur);
        cur = next;
    }
    *a_head = NULL;
    *a_tail = NULL;
}

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

    // linked list of size 1
    // [Generic]->NULL
    append("Generic", &head, &tail);
    print_linked_list(head);
    assert(head != NULL);
    assert(tail != NULL);
    assert(head == tail);
    char* tail_value = tail->a_value;
    assert(strcmp(tail_value, "Generic") == 0);

    // linked list of size 2
    // [Generic]->[Hello]->NULL
    append("Hello", &head, &tail);
    print_linked_list(head);
    assert(head != tail);
    // don't do this, you should not use casts
    tail_value = tail->a_value;
    assert(strcmp(tail_value, "Hello") == 0);

    // linked list of size 3
    // [Generic]->[Hello]->[World]->NULL
    append("World", &head, &tail);
    print_linked_list(head);
    tail_value = tail->a_value;
    assert(strcmp(tail_value, "World") == 0);

    // linked list of size 4
    // [5]->[7]->[9]->[1]->NULL
    // [Generic]->[Hello]->[World]->[String]->NULL
    append("String", &head, &tail);
    print_linked_list(head);
    tail_value = (char*) tail->a_value;
    assert(strcmp(tail_value, "String") == 0);

    // a later node in a linked list is a valid linked list
    print_linked_list(head->next);

    destroy_linked_list(&head, &tail);
    assert(head == NULL);
    assert(tail == NULL);

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

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