9

I want to use a stack in C, anybody recommend a library?

For example for a hash table I used UThash.

Thanks!

bstpierre
  • 30,042
  • 15
  • 70
  • 103
code2b
  • 93
  • 1
  • 1
  • 3
  • `sys/queue.h` has them: http://stackoverflow.com/questions/3951020/what-would-be-a-good-open-source-lightweight-c-library-with-basic-utility-functio/3953575#3953575 – Fred Foo Oct 25 '10 at 16:01
  • 8
    `Closed as non constructive`? I don't see how this question will lead to debate or discussion. It just happens that two of the answers are out of topic.Maybe a duplicate, but definitely constructive. – UmNyobe Mar 08 '13 at 10:42

3 Answers3

14

Stack implementation fits in single sheet of paper.

That's simplest stack example

int stack[1000];

int *sp;

#define push(sp, n) (*((sp)++) = (n))
#define pop(sp) (*--(sp))
...
{
    sp = stack; /* initialize */

    push(sp, 10);
    x = pop(sp);
}
Vovanium
  • 3,798
  • 17
  • 23
4

Here is a similar question:

Are there any open source C libraries with common data structures?

And here is CCAN, C's equivalent to CPAN:

http://ccan.ozlabs.org/

Community
  • 1
  • 1
Noah Watkins
  • 5,446
  • 3
  • 34
  • 47
-10

If you can fudge it a bit and use C++, Qt is a really great library with a lot of basic data structures.

kidjan
  • 1,471
  • 14
  • 16
  • 12
    If C++ is acceptable, then Qt is not necessary. Stacks are in the C++ standard library. – Fred Foo Oct 25 '10 at 16:05
  • found one from previous question, thanks! – code2b Oct 25 '10 at 16:09
  • Lars, I'd still use Qt for all of the other primitives it provides. I find the C++ Standard Libraries woefully inadequate, but maybe that's my own personal preference. – kidjan Oct 26 '10 at 16:53
  • 3
    @kidjan If you find the STL inadequate, you are in all likelyhood vastly overthinking your data structures and settling for subpar perfomance guarantees from QT. Use the STL at all opportunities. – Alice Jun 12 '14 at 07:32