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

typedef struct queue_t {
  struct queue_t *next; 
  char *string;
} queue_t;

/* Assume that string was allocated by caller. */

void AddToQueue(queue_t **head, char *string) {
  queue_t *q;

  if (*head == NULL) {
    *head = malloc(sizeof(queue_t));
    (*head)->string = string;
    (*head)->next = NULL;
    return;
  }

  for (q = *head; q; q = q->next) {
    if (q->next == NULL) {
      q->next = malloc(sizeof(queue_t));
      q->next->string = string;
      q->next->next   = NULL;
      return;
    }
  }
}


char *RemoveFromQueue(queue_t **head) {
  char *s;
  queue_t *q;

  if (*head == NULL) return NULL;

  s = (*head)->string;
  q = *head; 
  *head = (*head)->next;
  free(q);
  return s;
}

int main(int argc, char *argv[])
{
  queue_t *head = NULL;
  char *s;
 
  AddToQueue(&head, "First");
  AddToQueue(&head, "Second");
  AddToQueue(&head, "Third");
  AddToQueue(&head, "Fourth");

  while ((s = RemoveFromQueue(&head))) {
    printf("string %s\n", s); 
  } 
  return 0;
}
