From dfbdc7f433bdaf02babe1291b53633e610c42dfe Mon Sep 17 00:00:00 2001 From: EvanR Date: Sat, 20 Feb 2010 15:14:26 -0600 Subject: [PATCH] Added list.c and list.h These are actually copies of list.c and list.h from stage branch commit 1fb39e Was probably a mistake to add these in the beginning of that branch since I was going to need them in the other ones. I will now merge this into all topic branches, and master branch. --- Makefile | 2 +- list.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ list.h | 12 ++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 list.c create mode 100644 list.h diff --git a/Makefile b/Makefile index 52badaf..72f8a65 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ OBJ = video.o audio.o input.o kernel.o \ loader.o graphics.o sfx.o text.o console.o \ intro.o title.o splash.o game.o \ synth.o seq.o dsp.o \ - rng.o util.o + rng.o util.o list.o CC = gcc LIBS = -lSDL -lGL -lzzip -lm diff --git a/list.c b/list.c new file mode 100644 index 0000000..b20d6b9 --- /dev/null +++ b/list.c @@ -0,0 +1,147 @@ +#include +#include + + +#include + +list* freelist = NULL; + + +list* make_new(list* next, void* item){ + list* ptr; + + if(freelist == NULL){ + ptr = malloc(sizeof(struct list)); + } + else{ + ptr = freelist; + freelist = ptr->next; + } + + ptr->next = next; + ptr->item = item; + return ptr; +} + +void remove_from_list(list* prev, list* node){ + prev->next = node->next; + node->item = NULL; + node->next = freelist; + freelist = node; +} + + +list* empty(){ + return make_new(NULL, NULL); +} + +void push(list* L, void* item){ + list* ptr = make_new(L->next, item); + L->next = ptr; +} + +void* pop(list* L){ + void* item = L->next->item; + remove_from_list(L, L->next); + return item; +} + +void append(list* L, void* item){ + list* ptr = L->next; + while(ptr->next){ + ptr = ptr->next; + } + ptr->next = make_new(NULL, item); +} + +void recycle(list* L){ + list* ptr = L; + while(ptr->next){ + ptr->next->item = NULL; + ptr = ptr->next; + } + ptr->next = freelist; + freelist = L; +} + + + + + +void print_list(list* L, void (*print)(void* item)){ + list* ptr = L->next; + printf("("); + while(ptr){ + print(ptr->item); + if(ptr->next) + printf(", "); + ptr = ptr->next; + } + printf(")"); +} + +void println_list(list* L, void (*print)(void* item)){ + print_list(L, print); + printf("\n"); +} + +void list_print_free(){ + list* ptr = freelist; + printf("("); + while(ptr){ + printf("_"); + if(ptr->next){ + printf(", "); + } + ptr = ptr->next; + } + printf(")\n"); +} + +void print(void* item){ + char* s = item; + printf("%s", s); +} + +void list_sanitytest(){ + list* L = empty(); + char* s; + printf("empty: "); + println_list(L, print); + + printf("push a b c: "); + push(L, "a"); + push(L, "b"); + push(L, "c"); + println_list(L, print); + + printf("pop: "); + s = pop(L); + printf("%s ", s); + println_list(L, print); + + printf("freelist: "); + list_print_free(); + + printf("pop: "); + s = pop(L); + printf("%s ", s); + println_list(L, print); + + printf("freelist: "); + list_print_free(); + + printf("append a b c: "); + append(L, "a"); + append(L, "b"); + append(L, "c"); + println_list(L, print); + + printf("freelist: "); + list_print_free(); + + printf("recycle: "); + recycle(L); + list_print_free(); + +} diff --git a/list.h b/list.h new file mode 100644 index 0000000..02dc977 --- /dev/null +++ b/list.h @@ -0,0 +1,12 @@ +typedef struct list list; +struct list { + void* item; + list* next; +}; + +list* empty(); +void push(list* L, void* item); +void* pop(list* L); +void append(list* L, void* item); +void recycle(list* L); +void print_list(list* L, void (*print)(void* item)); -- 2.11.4.GIT