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

typedef struct s_node s_node;
typedef struct s_list s_list;
struct s_node {
    s_node * next;
  };
struct s_list {
    s_node * tail;
  };

s_node * append_node(s_list * list) {
    s_node * new_node;

    new_node = malloc(sizeof *new_node);
    if (!new_node)
      return NULL;

    /* Check for empty list */
    if (!list->tail) {
        new_node->next = new_node;
        list->tail = new_node;
        return new_node;
      }

    new_node->next = list->tail->next;
    list->tail->next = new_node;
    list->tail = new_node;
    return new_node;
  }

s_node * head_node(s_list * list) {
    /* Check for empty list */
    if (!list->tail)
      return NULL;

    return list->tail->next;
  }

int list_length(s_list * list) {
    s_node * node;
    int len = 0;

    for (node = head_node(list); node; node = node->next) {
        printf(
            "%p->next == %p%s\n",
            (void *) node,
            (void *) node->next,
            node == list->tail ? "" : " &&"
          );
        ++len;
        if (node == list->tail)
          break;
        continue;
      }
    return len;
  }

int main(void) {
    s_list list[1];

    list->tail = NULL;

    if (append_node(list) && append_node(list) && append_node(list))
      printf("Total nodes: %d\n", list_length(list));

    return EXIT_SUCCESS;
  }