C语言 标准 C 库是否提供链表等数据结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14001652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does standard c library provides linked list etc. data structures?
提问by rsjethani
Do standard C library implementations, especially glibc(the GNU C Library) provide linked lists, stack et al. data structures, or do we have to roll our own?
标准 C 库实现,尤其是glibc(GNU C 库)是否提供链表、堆栈等。数据结构,还是我们必须自己推出?
Thanks.
谢谢。
采纳答案by Alok Save
The C Standard does not provide data structures like linked list and stack.Some compiler implementations might provide their own versions but their usage will be non portable across different compilers.
C 标准不提供链表和堆栈等数据结构。一些编译器实现可能提供它们自己的版本,但它们的使用在不同的编译器之间是不可移植的。
So Yes, You have to write your own.
所以是的,你必须自己写。
回答by iabdalkader
回答by SKi
There are hash tables, binary trees and binary search stuff in glibc. Those are part of C89, C99 and/or POSIX.1 standards. Some reason linked list is not there.
glibc 中有哈希表、二叉树和二叉搜索的东西。这些是 C89、C99 和/或 POSIX.1 标准的一部分。某些原因链表不存在。
More info from man pages: hsearch, tsearchand bsearch
来自手册页的更多信息:hsearch、tsearch和bsearch
Note:Some of those have bad design. For example: hsearchallows only one hash table per process. The GNU compiler, gcc/glibc, provides reentrant versions hcreate_r, hsearch_r, and hdestroy_rthat allow multiple hash tables. See also Stack Overflow's How to use hcreate_r.
注意:其中一些设计不好。例如:hsearch每个进程只允许一个哈希表。在GNU编译器gcc / glibc的,提供可重入版本hcreate_r,hsearch_r和hdestroy_r允许多个哈希表。另请参阅 Stack Overflow 的如何使用hcreate_r.
回答by Rahul Tripathi
回答by djcb
There's a hash table implementation in POSIX (and GLibc); see the manpages for hcreate/hdestroy/hsearch.
在 POSIX(和 GLibc)中有一个哈希表实现;请参阅 hcreate/hdestroy/hsearch 的联机帮助页。
But, as mentioned, using glib is probably the easiest way to save yourself from reimplementing the basic data structure.
但是,如前所述,使用 glib 可能是避免重新实现基本数据结构的最简单方法。

