/* CSE 333 Su12 Lecture 6 demo: manual_list_void.c */
/* Gribble/Perkins */

/* Dynamic linked list demo with arbitrary list data */

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

typedef struct Node {          // Linked list node:
  void *element;               //   Ptr to data for this node
  struct Node *next;           //   Ptr to next node or NULL if none
} Node;

// Given a pointer head to a linked list node and an element e,
// create a node refering to e with head as the next node and
// return a pointer to the newly allocated node.
// Exit the program with failure if unable to allocate a new node.
Node *Push(Node *head, void *e) {

  Node *n = (Node *) malloc(sizeof(Node));
  if (n == NULL)
    exit(EXIT_FAILURE);

  n->element = e;
  n->next = head;

  return n;
}

// Create a new linked list holding two strings and print last one
int main(int argc, char **argv) {
  char *hello = "Hi there!";
  char *goodbye = "Bye bye.";
  Node *list = NULL;

  list = Push(list, (void *) hello);
  list = Push(list, (void *) goodbye);
  printf("payload is: '%s'\n", (char *) list->next->element);

  // omitted: production code should free allocated heap data
  return 0;
}