Posts Tagged ‘snippets’

C linked list macro

Here’s a simple macro I came up with a few weeks ago for easily defining linked list types in C.

#define LINKED_LIST(type, name)     \
    struct name ## _ {              \
        struct name ## _ * next;    \
        type data;                  \
    };                              \
    typedef struct name ## _ name;

Usage is simple:

LINKED_LIST(int, int_ll);

gives the same type as

struct int_ll_ {
    struct int_ll_ * next;
    int data;
};
typedef int_ll_ int_ll;