Q:

glibc - list and other data structures implementations

I fill like my google searching skills are poor right now, couldn't find a list implementation in glibc, found hash and tree implementations but not a list one. 
Is there any glibc implementation for that? I don't want to reformat the linux kernel linked list macros and use them in userspace.

A:

You can use insque(3) and remque(3)

A:

There's /usr/include/sys/queue.h which contains various linked list variants.(more than the man page documents)
Here's an example for a TAIL_QUEUE:
Run it through the preprocessor (gcc -E -c prog.c) to easier see how it
works under the hood)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/queue.h>

struct Block {
    char text[64];
    //linked list entry
    TAILQ_ENTRY(Block) blocks;
};
struct File {
   char name[128];
   //list of blocks in the "file"
   TAILQ_HEAD(,Block) head; 
};

void add_block(struct File *f, const char *txt)
{
   struct Block *b = malloc(sizeof *b);
   strcpy(b->text,txt); 
   TAILQ_INSERT_TAIL(&f->head, b, blocks);
}

void print_file(struct File *f)
{
    struct Block *b;
    printf("File: %s\n", f->name);
    TAILQ_FOREACH(b, &f->head, blocks) {
        printf("Block: %s\n", b->text);
    }
}
void delete_block(struct File *f, const char *txt)
{
    struct Block *b, *next;
    for(b = TAILQ_FIRST(&f->head) ; b != NULL ; b = next) {
        next = TAILQ_NEXT(b, blocks);
        if(strcmp(b->text, txt) == 0) {
            TAILQ_REMOVE(&f->head, b, blocks);
            free(b);
        }
    }

}

void delete_all_blocks(struct File *f)
{
    struct Block *b;
    while((b = TAILQ_FIRST(&f->head))) {
        TAILQ_REMOVE(&f->head, b, blocks);
        free(b);
    }
}

int main(void)
{
    struct File f;
    TAILQ_INIT(&f.head);
    strcpy(f.name,"Test.f");
    add_block(&f,"one");
    add_block(&f,"two");
    add_block(&f,"three");

    print_file(&f);

    puts("\nDeleting three");
    delete_block(&f, "three");
    print_file(&f);

    puts("\nAdding 2 blocks");
    add_block(&f,"three");
    add_block(&f,"three");
    print_file(&f);

    puts("\nDeleting three");
    delete_block(&f, "three");
    print_file(&f);

    delete_all_blocks(&f);

    return 0;
}

