/* links.c
 * a linked list in C
 * Run this at pythontutor.com (yes, it does C too) to see it.
 *
 *   $ gcc links.c -o links
 *   $ ./links
 *   1 -> 2 -> 3 ->
 *
 * Jim M | Feb 6 2022 | cs.bennington.com | MIT License
 ************************/

#include <stdio.h>
#include <stdlib.h>

int DEBUG=0;   // set to 1 to see debug print

typedef struct _Node *Node;
struct _Node {
    int value;
    Node next;
};

// Allocate space for a new node, initalize its insides, return it.
Node new_node(int value, Node next){
    /* "self" is a Node which is a pointer to a (struct _Node). */
    Node self = malloc(sizeof(struct _Node));
    self->value = value;
    self->next = next;
    if (DEBUG){
        printf("DEBUG new_node self=%p value=%i next=%p \n",
               self, self->value, self->next);
    }
    return self;
}

// Print nodes in a linked list starting at the given one.
void print_nodes(Node node){
    while (node != NULL){
        printf("%i -> ", node->value);
        node = node->next;
    }
    printf("\n");
}

int main(){
    // Create a linked list : 1 -> 2 -> 3 ->
    Node three = new_node(3, NULL);
    Node two = new_node(2, three);
    Node one = new_node(1, two);

    // ... and print all of 'em.
    print_nodes(one);
    
    return 0;
}