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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:49:15  来源:igfitidea点击:

Does standard c library provides linked list etc. data structures?

cgccdata-structuresglibc

提问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

The C standard doesn't, glibc however provides lists, tail queues, and circular queues in <sys/queue.h>according to the queueman page those come from BSD and not POSIX.

C 标准没有,但是 glibc<sys/queue.h>根据来自 BSD 而不是 POSIX的队列手册页提供了列表、尾队列和循环队列。

回答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

来自手册页的更多信息:hsearchtsearchbsearch

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_rhsearch_rhdestroy_r允许多个哈希表。另请参阅 Stack Overflow 的如何使用hcreate_r.

回答by Rahul Tripathi

As such C does not provide data structures but you can use the glibprovided by Gnome

因此,C 不提供数据结构,但您可以使用Gnome 提供的glib

Queue.had Tree.halso provides you some Data structures

Queue.hTree.h还为您提供了一些数据结构

回答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 可能是避免重新实现基本数据结构的最简单方法。

回答by stdcall

As other answers already stated, there isn't a linked list library in the standard library.

正如其他答案已经指出的那样,标准库中没有链接列表库。

I've written one for my own use a while ago. You can freely use it or use the code as reference.

不久前我写了一个供我自己使用。您可以自由使用它或使用代码作为参考。

You can find it here: libllist

你可以在这里找到它:libllist