#include #include struct list_node { struct list_node *prev, *next; int value; void init(int v) { value = v; } struct list_node *advance() { return next; } }; struct list { struct list_node *first, *last; struct list_node *begin() { return first; } struct list_node *end() { return NULL; } void init() { first = NULL; last = NULL; } void insert(struct list_node *where, int v) { struct list_node *newnode = (struct list_node*)malloc(sizeof(*newnode)); newnode->init(v); newnode->prev = where ? where->prev : last; newnode->next = where; if(newnode->prev) newnode->prev->next = newnode; if(where) where->prev = newnode; if(where == first) first = newnode; if(!where) last = newnode; } void push_front(int v) { insert(begin(), v); } void push_back(int v) { insert(end(), v); } void erase(struct list_node *where) { struct list_node* prev = where->prev; struct list_node* next = where->next; if(prev) prev->next = next; if(next) next->prev = prev; if(where == first) first = next; if(where == last) last = prev; free(where); } void clear() { while(first) erase(first); } void done() { clear(); } }; int main(void) { struct list mylist; struct list_node *i; mylist.init(); mylist.push_back(1); mylist.push_back(5); mylist.push_front(8); mylist.push_back(3); for(i = mylist.begin(); i != mylist.end(); i = i->advance()) printf("%d ", i->value); printf("\n"); mylist.done(); return 0; }