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

typedef struct _Node { // 12+ bytes
    int value;          // 4 bytes
    struct _Node* next; // 8 bytes
} Node;

#define log_int(n) printf("%s == %d\n", #n, n)

void print_list(const Node* head) {
    // good code quality: using a for loop
    for (const Node* node = head; node != NULL; node = node->next) {
        printf("[%d]->", node->value);
    }
    printf("NULL\n");
}

void destroy_list(Node* head) {
    Node* node = head;
    while (node != NULL) {
        Node* next = node->next;
        free(node);
        node = next;
    }
}

// append and remove_front effectively makes this a queue
void append(Node** a_head, Node** a_tail, int value) {
    Node* new_node = malloc(sizeof(*new_node));
    *new_node = (Node) { .next = NULL, .value = value };

    if (*a_head == NULL) {
        // from size 0 to size 1
        *a_head = new_node;
    } else {
        // from size N to size N+1
        (*a_tail)->next = new_node;
    }
    *a_tail = new_node;
}

Node* remove_front(Node** a_head, Node** a_tail) {
    Node* node = *a_head;
    if (node == NULL) {
        return NULL;
    }
    *a_head = node->next;
    if (*a_head == NULL) {
        *a_tail = NULL;
    }
    node->next = NULL;
    return node;
}

int get_size_of_list(const Node* head) {
    int size = 0;
    for (const Node* node = head; node != NULL; node = node->next) {
        size += 1;
    }
    return size;
}

int main(int argc, char* argv[]) {
    Node* head = NULL;
    Node* tail = NULL;
    print_list(head);

    append(&head, &tail, 1);
    print_list(head);

    append(&head, &tail, 2);
    print_list(head);

    append(&head, &tail, 3);
    print_list(head);
    print_list(head->next);

    append(&head, &tail, 343);
    append(&head, &tail, 34);
    append(&head, &tail, 6654);
    append(&head, &tail, 545);
    append(&head, &tail, 934);
    append(&head, &tail, 49);
    print_list(head);
    log_int(get_size_of_list(head));
    log_int(get_size_of_list(head->next));

    Node* node = remove_front(&head, &tail);
    print_list(head);
    print_list(node);
    destroy_list(node);

    node = remove_front(&head, &tail);
    print_list(head);
    print_list(node);
    destroy_list(node);

    append(&head, &tail, 1234);
    print_list(head);

    node = remove_front(&head, &tail);
    print_list(head);
    print_list(node);
    destroy_list(node);

    destroy_list(head);

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

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.