C语言 C 是否有标准的队列实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4184954/
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 07:04:51  来源:igfitidea点击:

Are there standard Queue implementations for C?

cunixdata-structures

提问by devoured elysium

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!)

是否有 C 附带的任何队列数据结构实现,或者我是否必须开发自己的(这是一个学校项目,因此我必须使用标准 gcc 安装中存在的东西,或者必须自己实现一个! )

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

链表、堆栈等其他通用数据结构呢?

采纳答案by Pascal Cuoq

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.

您必须实现自己的。C 在数据结构方面几乎没有,并迫使您诉诸有争议的技巧来实现抽象数据类型:如果您能找到它,请参阅题为“不完整的类型作为抽象”的文章,或者看看这些原则是如何应用的,例如,PolarSSL 的 bignum.h 文件。另一方面,C++ 应该允许你做几乎所有你可以用 C 做的事情,并为你提供实现抽象数据结构的方法。

回答by Sudhanshu

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.

尝试这个。Unix 带有多种链表 - 您可以使用其中一种来创建其他可能基于列表的结构,例如堆栈。

man queue

回答by Bernardo Ramos

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

完整来源在这里

回答by Paul

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

使用 BSB 库。sys/queue.h 和 sys/tree.h 有各种列表和树的实现。

回答by eadmaster

Not exacly standard, but many systems have bsd/sys/queue.hand bsd/sys/tree.hthat are macro-based libraries.

不是完全标准的,但许多系统都有bsd/sys/queue.h并且bsd/sys/tree.h是基于宏的库。

See the documentation here.

请参阅此处文档

回答by Paul Rubel

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.

您可以使用命名管道。它是一个 FIFO 数据结构,是 posix 标准的一部分。如果您的所有需求都在后面并从前面取下,它将起作用。不过,您需要手动跟踪消息边界,也许可以通过将第一个元素作为下一条消息中的字节数。

回答by qwr

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

GLib(不要与 glibc 混淆)实现了许多常见的数据结构,包括双端队列。与FreeBSD 的基于宏的队列不同GLib 的 GQueues基于可能更容易调试和类型检查的函数。

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

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

回答by swegi

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

您必须实现自己的数据结构,但存在许多数据结构库。