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

typedef struct item {
  int data;
  struct item * next;
} Item;

typedef struct list {
  int size;
  Item * head;
  Item * tail;
} List;

List * push_back(List * list, int data)
{
  Item * tail;

  if (!list) return;

  Item * new = (Item *) malloc(sizeof(Item));   /* construct new item */
  if (!new) {
    printf("ERROR: out of memory!\n");
    return NULL;
  }
  new->data = data;
  new->next = NULL;

  tail = list->tail;                            /* insert new item as last */
  if (!tail) {
    list->head = list->tail = new;
    list->size = 1;
    return list;
  }

  tail->next = new;
  list->tail = new;
  list->size++;
  return list;
}

void print(List * list)
{
  Item * head;
  
  if (!list) return;
  head = list->head;
  if (!head) return;
  printf("%d", head->data);
  for (head = head->next; head; head = head->next)
    printf(" %d", head->data);
  printf("\n");
}

int main()
{
  List list;
  list.size = 0;
  list.head = list.tail = NULL;
  if (!push_back(&list, 1)) return -1;
  if (!push_back(&list, 2)) return -1;
  if (!push_back(&list, 3)) return -1;
  if (!push_back(&list, 4)) return -1;
  if (!push_back(&list, 5)) return -1;
  print(&list);
  return 0;
}
