简体   繁体   English

我应该在ANSI C中使用什么来创建链接列表?

[英]What should I use in ANSI C to create linked lists?

In Java I used LinkedList<T> . 在Java中,我使用LinkedList<T>

Is there any standard, or maybe some other libraries, to create LinkedLists in C? 是否有任何标准或其他库来在C中创建LinkedList?

No, C doesn't have anything like that. 不,C没有这样的东西。 If you can use C++, there's std::list . 如果可以使用C ++,则有std::list

You'll have to write the structure yourself in C. 您必须自己用C编写结构。

As others have said, there is no standard library support for linked lists in C. That means you either get to roll your own (like most people have done at one or more times in their C coding career), or you look at other libraries for support. 就像其他人所说的那样,C中不存在对链接列表的标准库支持。这意味着您可以自己滚动(就像大多数人在C编码生涯中做过一次或多次)一样,或者您可以查看其他库为了支持。

For most uses, you will find that an 'intrusive' implementation is appropriate. 对于大多数用途,您会发现“侵入式”实施是合适的。 You modify your existing structure to include a pointer to the next item in the list (and, if you're doing a doubly linked list, the previous item). 您可以修改现有结构,使其包含指向列表中下一项的指针(如果要进行双向链接,则指向上一项)。

typedef struct TheStruct TheStruct;

struct TheStruct
{
    ...the real data...
    TheStruct *next;
  //TheStruct *prev;
};

Your list now consists of a simple pointer to the head item: 现在,您的列表包含一个指向标题的简单指针:

TheStruct *head = 0;

TheStruct *allocated_node = new_TheStruct(value1, value2, ...);

assert(allocated_node->next == 0);

// Insert new allocated node at head of list
allocated_node->next;
head = allocated_node;

You have to worry about threading issues if you've got threads access the same list, and so on and so forth. 如果让线程访问同一列表,则必须担心线程问题,依此类推。

Because this code is so simple, this is how it is most frequently handled. 因为此代码非常简单,所以这是最常处理的方式。 Deleting a node from a list is trickier, but you can write a function to do it. 从列表中删除节点比较麻烦,但是您可以编写一个函数来实现。

People have dressed up this basic scheme with macros of varying degrees of complexity so that the code manipulating different types of lists can use the same macros. 人们已经用复杂程度不同的宏修饰了这种基本方案,以便处理不同类型列表的代码可以使用相同的宏。 The Linux kernel uses a macro-based set of linked lists for managing its linked list data structures. Linux内核使用基于宏的链接列表集来管理其链接列表数据结构。

The alternative to the 'intrusive' implementation (which is so-called because it requires a change to the structure to include the list pointers) is to have some generic list mechanism: “侵入式”实现的替代方案(之所以这样称呼是因为它需要更改结构以包括列表指针),它具有一些通用的列表机制:

typedef struct GenericList GenericList;
struct GenericList
{
    void        *data;
    GenericList *next;
  //GenericList *prev;
};

Now you have the ability to put (pointers to) unmodified structures into the list. 现在,您可以将(未指向)未修改的结构放入列表中。 You lose some type safety; 您会失去一些类型安全性; there is nothing to stop you adding random different structure types to a list that should be homogeneous. 没有什么可以阻止您将随机不同的结构类型添加到应为同类的列表中。 You also have to allocate memory for the GenericList structures separately from the pointed at data. 您还必须与指向的数据分开为GenericList结构分配内存。

  • One advantage of this is that a single data item can be on multiple separate lists simultaneously. 这样的一个优点是单个数据项可以同时位于多个单独的列表中。 * One disadvantage of this is that a single data item can accidentally be on multiple lists, or multiple times in a single list, simultaneously. *这样的一个缺点是单个数据项可能意外地同时出现在多个列表中,或者一次出现在单个列表中多次。

You also have to make sure you understand the ownership properties for the data, so that the data is released properly (not leaked, not multiply-freed, not accessed after being freed). 您还必须确保您了解数据的所有权属性,以便正确释放数据(不泄漏,不多重释放,释放后不访问)。 Managing the memory for the list itself is child's play by comparison. 比较而言,管理列表本身的内存是孩子的事。

With the GenericList mechanism, you will likely have a library of functions that manage the list operations for you. 使用GenericList机制,您可能会拥有一个函数库来为您管理列表操作。 Again, searching should reveal implementations of such lists. 再次,搜索应显示此类列表的实现。

C does not (as yet) provide any standard container structures. C尚未提供标准容器结构。 You'll have to roll your own. 您必须自己动手。

It doesn't exist by default, but it can be made quite easily. 默认情况下不存在,但是可以很容易地使它。

Basically a linked list is a header pointer that points to 1st element, and an ending file pointing to null. 基本上,链接列表是指向1st元素的标头指针和指向null的结束文件。

The elements are actually nodes, containing the object and a pointer to the next node. 元素实际上是节点,包含对象和指向下一个节点的指针。

So making a basic one is quite easy, though in C it might be painful as no classes, only structs. 因此,尽管在C语言中没有类,而只有结构,但是制作一个基本的类很容易,尽管这样可能很痛苦。

There are no Lists in the standard C library. 标准C库中没有列表。 A very good C library you can use is PBL - The Program Base Library here . 您可以使用的一个很好的C库是PBL- 这里的程序库。 Its interface is Java like. 它的接口类似于Java。 You can download it from here 您可以从这里下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM