简体   繁体   中英

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

In Java I used LinkedList<T> .

Is there any standard, or maybe some other libraries, to create LinkedLists in C?

No, C doesn't have anything like that. If you can use C++, there's std::list .

You'll have to write the structure yourself in 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.

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.

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.

  • 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. Again, searching should reveal implementations of such lists.

C does not (as yet) provide any standard container structures. 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.

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.

There are no Lists in the standard C library. A very good C library you can use is PBL - The Program Base Library here . Its interface is Java like. You can download it from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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