14

Is there any Queue data structure implementation that "comes" with C or will I have to develop my own (this is for a school project, thus I must use something that either exists in the standard gcc installation or have to implement one by myself!)

What about other general data structures like Linked Lists, Stacks, etc?

Dharman
  • 30,962
  • 25
  • 85
  • 135
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • http://stackoverflow.com/questions/1819416/standard-data-structure-library-in-c http://stackoverflow.com/questions/14001652/does-standard-c-library-provides-linked-list-etc-data-structures – Bernardo Ramos Apr 28 '17 at 19:01

8 Answers8

27

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
Sudhanshu
  • 2,691
  • 1
  • 18
  • 25
5

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

Bernardo Ramos
  • 4,048
  • 30
  • 28
1

Use BSB lib. sys/queue.h and sys/tree.h have implementations of various lists and trees.

Paul
  • 177
  • 3
1

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.

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
0

Not exacly standard, but many systems have bsd/sys/queue.h and bsd/sys/tree.h that are macro-based libraries.

See the documentation here.

eadmaster
  • 1,347
  • 13
  • 23
0

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.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
0

GLib (not to be confused with glibc) implements many common data structures including double-ended queues. Unlike FreeBSD's macro-based queues, GLib's GQueues are based on functions which may be easier to debug and type check.

https://developer.gnome.org/glib/stable/glib-Double-ended-Queues.html

qwr
  • 9,525
  • 5
  • 58
  • 102
-2

You have to implement your own data structures, but there exist many data structure libraries out there.

swegi
  • 4,046
  • 1
  • 26
  • 45