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 | #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)
int main(int argc, char* argv[]) {
Node* head = NULL;
Node* new_node = malloc(sizeof(*new_node));
*new_node = (Node) { .value = 1, .next = NULL };
head = new_node;
new_node = malloc(sizeof(*new_node));
*new_node = (Node) { .value = 2, .next = NULL };
head->next = new_node;
new_node = malloc(sizeof(*new_node));
*new_node = (Node) { .value = 3, .next = NULL };
head->next->next = new_node;
log_int(head->value);
log_int(head->next->value);
log_int(head->next->next->value);
Node* node = head;
log_int(node->value);
node = node->next;
log_int(node->value);
node = node->next;
log_int(node->value);
free(head->next->next);
free(head->next);
free(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.