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

struct Node {  // SINGLY LINKED LIST
    int    value;
    struct Node* next;
    struct Node* prev;
};

int main(int argc, char *argv[]) {
    // empty list
    struct Node* head = NULL;  // always set head to either NULL or malloc(…)
    
    // add a node → list of size 1
    head = malloc(sizeof(*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;
    
    // connect the two nodes
    head -> next = new_node;
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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