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

typedef struct _Node { // canonical name struct _Node
    int value;
    struct _Node* next; // need to use canonical name
// invalid: Node* next;
/*
linked_list.c:8:2: error: unknown type name ‘Node’
  Node* next;
  ^~~~
*/
} Node; // shortcut name: Node

void print_linked_list(Node* head) {
    // loop to iterate the linked list
    // we could write this as a while loop
    // per CQ, its better to write as a for loop
    for (Node* cur = head; cur != NULL; cur = cur->next) {
        printf("[%d]", cur->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(int 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) { .value = 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) {
    // TODO: implement
    free(*a_head);
    *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
    // [5]->NULL
    append(5, &head, &tail);
    print_linked_list(head);
    assert(head != NULL);
    assert(tail != NULL);
    assert(head == tail);
    assert(tail->value == 5);

    // linked list of size 2
    // [5]->[7]->NULL
    append(7, &head, &tail);
    print_linked_list(head);
    assert(head != tail);
    assert(tail->value == 7);

    // linked list of size 3
    // [5]->[7]->[9]->NULL
    append(9, &head, &tail);
    print_linked_list(head);
    assert(tail->value == 9);

    // linked list of size 4
    // [5]->[7]->[9]->[1]->NULL
    append(1, &head, &tail);
    print_linked_list(head);
    assert(tail->value == 1);

    // 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.