Skip to content

Instantly share code, notes, and snippets.

@JettMonstersGoBoom
Last active November 28, 2023 02:08
Show Gist options
  • Save JettMonstersGoBoom/28c73fac2fccf95994a731b6dc0483b6 to your computer and use it in GitHub Desktop.
Save JettMonstersGoBoom/28c73fac2fccf95994a731b6dc0483b6 to your computer and use it in GitHub Desktop.
double_linked_list.h
typedef struct _list_t_
{
struct _list_t_ *prev,*next;
} _list_t_;
#define list_add(list, obj) ({ ((_list_t_ *)obj)->next = list; ((_list_t_ *)obj)->prev = NULL; if (list != NULL) list->prev = obj;list = (_list_t_ *)obj;})
#define list_del(list, obj) ({ if (((_list_t_ *)obj)->next) ((_list_t_ *)obj)->next->prev = ((_list_t_ *)obj)->prev; if (((_list_t_ *)obj)->prev) ((_list_t_ *)obj)->prev->next = ((_list_t_ *)obj)->next; else list = ((_list_t_ *)obj)->next;})
#define list_for(Y,X) for (_list_t_ *Y=X;Y!=NULL;Y=Y->next)
// example
/*
typedef struct
{
_list_t_;
Matrix local,world;
void *data;
_list_t_ *children;
} _entity_t_;
Entity_t o[4];
Entity_t c[4];
Entity_t *root = {0};
memset(o,0,sizeof(o));
memset(c,0,sizeof(c));
list_add(root,&o[0]);
list_add(root,&o[1]);
list_add(root,&o[2]);
list_add(root,&o[3]);
list_add(o[1].children,&c[0]);
list_add(o[1].children,&c[1]);
list_for(self,root)
{
printf("%x\n",self);
}
list_del(root,&o[2]);
list_for(self,root)
{
printf("%x\n",self);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment