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
#include <stdio.h>
#include <stdlib.h>
struct Node {  // SINGLY LINKED LIST
    int    value;
    struct Node* prev;
    struct Node* next;
}; // require ≥20 bytes
int main(int argc, char *argv[]) {
    // empty list
    struct Node* head = NULL; // always set head to either NULL or malloc(…)
    struct Node* tail = NULL;
    
    // add a node → list of size 1
    head = malloc(sizeof(*head));  // allocated ≥20 bytes [OOP]
    tail = head;
    head -> value = 2;
    head -> prev  = NULL;
    head -> next  = NULL;  // IMPORTANT:  ATYT (always terminate your tail)

    // add another node → list of size 2
    struct Node* new_node = malloc(sizeof(*new_node));
    new_node -> value = 4;
    new_node -> next  = NULL;
    new_node -> prev  = head;
    tail              = new_node;
    head -> next      = new_node;
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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