-1

I've being doing Java for some time and wondering if there's an easy way of creating Queues or similar structures in C? I need to create a buffer that will remove the contents when I need it and be able to add contents to the end of it?

Afriza N. Arief
  • 7,696
  • 5
  • 47
  • 74
Donatello
  • 947
  • 1
  • 7
  • 6
  • 3
    Typically a queue can be implemented using a double-linked list. But a single-linked list will work too, if you keep track of both the head and the tail of the list. – Some programmer dude Oct 21 '13 at 10:21

5 Answers5

2

There is no built-in support for Queues, Lists or Maps in C. You need to either find a third party library for it or write one yourself.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
2

I have used this suite of data structures and been happy with the results:

http://troydhanson.github.io/uthash/utlist.html

The approach of using macros in a header file makes it quite lightweight and straightforward to incorporate into your projects

bph
  • 10,728
  • 15
  • 60
  • 135
2

If you're coming from a Java background, you'll find C++ to have more similarities to it than plain C. In the C++ standard template library, there is already a Queue class, which should be about the most efficient Queue you can possibly implement in C or C++.

Chara
  • 338
  • 6
  • 14
1

Basically you have to write it yourself or look for an implementation in the internet. Compared to Java the C standard library is very limited.

On the other hand, implementing such a basic data structure is really simple and there is a lot of documentation available about efficient implementations.

Jan Henke
  • 875
  • 1
  • 15
  • 28
1

If you are using Linux or BSD (including MacOS), you can use #include <sys/queue.h>.
Also see

If you are using windows, you can copy the header and edit it to be independent (if necessary).

Community
  • 1
  • 1
Afriza N. Arief
  • 7,696
  • 5
  • 47
  • 74