Are there standard Queue implementations for C?

You could use a named pipe. It's a FIFO data structure and is part of the posix standard. If all your want is enque to the back and remove from the front it will work. You'll need to keep track of message boundaries by hand though, perhaps by having the first element be the number of bytes in the next message.


Try this. Unix comes with several kinds of linked lists - you can use one of them to create other possibly list based structures such as a stack.

man queue

You must implement your own. C has very little in terms of data structures and forces you to resort to arguable tricks to implement abstract data types: see an article titled “Incomplete types as abstractions” if you can find it, or see how the principles are applied in, say, PolarSSL's bignum.h file. C++ on the other hand is supposed to allow you to do pretty much everything you can do in C and give you ways to implement abstract data structures.


No. But here is a very simple implementation:

typedef struct node {
   int val;
   struct node *next;
} node_t;

void enqueue(node_t **head, int val) {
   node_t *new_node = malloc(sizeof(node_t));
   if (!new_node) return;

   new_node->val = val;
   new_node->next = *head;

   *head = new_node;
}

int dequeue(node_t **head) {
   node_t *current, *prev = NULL;
   int retval = -1;

   if (*head == NULL) return -1;

   current = *head;
   while (current->next != NULL) {
      prev = current;
      current = current->next;
   }

   retval = current->val;
   free(current);

   if (prev)
      prev->next = NULL;
   else
      *head = NULL;

   return retval;
}

Complete source here