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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
typedef struct _Node { // canonical name struct _Node
char* 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("[%s]", 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(char* 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) {
Node* cur = *a_head;
while (cur != NULL) {
Node* next = cur->next;
free(cur->value); // free before I free cur
free(cur);
cur = next;
}
*a_head = NULL;
*a_tail = NULL;
}
char* copy_string(char const* original) {
int length = strlen(original);
char* copy = malloc(sizeof(*copy) * (length + 1));
for (int i = 0; original[i] != '\0'; i++) {
copy[i] = original[i];
}
copy[length] = '\0';
return copy;
}
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
// [Hello]->NULL
append(copy_string("Hello"), &head, &tail);
print_linked_list(head);
assert(head != NULL);
assert(tail != NULL);
assert(head == tail);
assert(strcmp(tail->value, "Hello") == 0);
// linked list of size 2
// [Hello]->[World]->NULL
append(copy_string("World"), &head, &tail);
print_linked_list(head);
assert(head != tail);
assert(strcmp(tail->value, "World") == 0);
// linked list of size 3
// [Hello]->[World]->[String]->NULL
append(copy_string("String"), &head, &tail);
print_linked_list(head);
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.