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

struct List
{
int          value;
struct List *next;
};

void print(struct List *h);  	// Declaration
struct List *insert (struct List *h, struct List *e);

/* ====================================================
   delete(h): delete the first elem from the list h
              return new list
   ==================================================== */
struct List *delete (struct List *h)
{
   struct List *newHead;

   if ( h != NULL)
   {
      newHead = h->next;  // Save the return value 
                          // Because free(h) MAY corrupt h !!!

      free( h );          // *** De-allocate the list object at head h !!!
                          // *** This call MAY correct h->value and h->next !!

      return (newHead);   // Return new list that starts at h.next
   }
   else
      return NULL;
}

int main(int argc, char *argv[])
{
   struct List *head = NULL;

   // Insert
   for (int i = 1; i <= 10; i++)
   {
       struct List *e = malloc( sizeof(struct List) ) ;
       e->value = i;
       head = insert(head, e);       // Insert i at head
       print(head);
   }

   // Delete
   for (int i = 1; i <= 10; i++)
   {
       head = delete(head);          // Delete at head
       print(head);
   }

}

// Insert at head
struct List *insert (struct List *h, struct List *e)
{
    e->next = h;
    return e;     // Return new list that starts at e
}


// Print the list
void print(struct List *h)
{
   while (h != NULL)
   {
      printf("%d ", h->value);
      h = h->next;
   }
   printf("\n");
}