dev*_*ium 8 c unix data-structures
是否存在任何与C一起"来"的Queue数据结构实现,或者我必须自己开发(这是针对学校项目的,因此我必须使用标准gcc安装中存在的东西或者必须自己实现一个! )
那么像链接列表,堆栈等其他常规数据结构呢?
谢谢
Sud*_*shu 23
试试这个.Unix带有几种链表 - 您可以使用其中一种链接列表来创建其他可能基于列表的结构,例如堆栈.
man queue
不,但这是一个非常简单的实现:
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;
}
完整来源在这里
您必须实施自己的。C 在数据结构方面的知识非常少,迫使您采用有争议的技巧来实现抽象数据类型:请参阅标题为 \xe2\x80\x9cIncomplete types as abstractions\xe2\x80\x9d 的文章(如果您能找到它),或者参见这些原理如何应用到PolarSSL 的 bignum.h 文件中。另一方面,C++ 应该允许您完成 C 中可以做的几乎所有事情,并为您提供实现抽象数据结构的方法。
\n