Skip to content

Instantly share code, notes, and snippets.

@dontlaugh
Created June 27, 2019 01:46
Show Gist options
  • Save dontlaugh/f378c2eeb0f4df2e9d567066d9697d38 to your computer and use it in GitHub Desktop.
Save dontlaugh/f378c2eeb0f4df2e9d567066d9697d38 to your computer and use it in GitHub Desktop.
linked list interface
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
// linked list elements
typedef struct ListElm_ {
void *data;
struct ListElm_ *next;
} ListElm;
// Linked list data structure
typedef struct List_ {
int size;
int (*match) (const void *key1, const void *key2);
void (*destroy)(void *data);
ListElm *head;
ListElm *tail;
} List;
void list_init(List *list, void (*destroy)(void *data));
/**
Runtime: O(n) for a linked list of n elements. Calls list_rem_next for
each element.
*/
void list_destroy(List *list);
/**
Passing NULL for element means we are inserting at the head.
*/
int list_ins_next(List *list, ListElm *element, const void *data);
int list_rem_next(List *list, ListElm *element, void **data);
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define list_is_tail(list, element) ((element) == (list)->tail ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next)
#endif // LIST_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment