From da1e8d8099a54a0f49791af5e2baa234ed88278c Mon Sep 17 00:00:00 2001 From: Michael Cardell Widerkrantz Date: Mon, 1 Nov 2010 19:50:18 +0100 Subject: Forgot to remember the new head of the list in addtohead()! On the other hand I would probably have found it if I had tried to use it before. Added some debug code for the stand-alone list. --- list.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'list.c') diff --git a/list.c b/list.c index 1f7c0e5..3f5324e 100644 --- a/list.c +++ b/list.c @@ -33,6 +33,9 @@ void movetohead(struct item **mainlist, struct item *item) /* Now at head. */ item->prev = NULL; item->next = *mainlist; + + /* Remember the new head. */ + *mainlist = item; } /* @@ -107,57 +110,92 @@ void listitems(struct item *mainlist) } #if 0 + +void listall(struct item *mainlist) +{ + struct item *item; + int i; + + for (item = mainlist, i = 1; item != NULL; item = item->next, i ++) + { + printf("%d at %p: %s.\n", i, (void *)item, (char *)item->data); + } +} + int main(void) { struct item *mainlist = NULL; struct item *item1; struct item *item2; struct item *item3; + struct item *item4; char *foo1 = "1"; char *foo2 = "2"; char *foo3 = "3"; - - item1 = addhead(&mainlist); + char *foo4 = "4"; + + item1 = additem(&mainlist); if (NULL == item1) { printf("Couldn't allocate.\n"); exit(1); } item1->data = foo1; - listitems(mainlist); + printf("Current elements:\n"); + listall(mainlist); - item2 = addhead(&mainlist); + item2 = additem(&mainlist); if (NULL == item2) { printf("Couldn't allocate.\n"); exit(1); } item2->data = foo2; - listitems(mainlist); + printf("Current elements:\n"); + listall(mainlist); - item3 = addhead(&mainlist); + item3 = additem(&mainlist); if (NULL == item3) { printf("Couldn't allocate.\n"); exit(1); } item3->data = foo3; - listitems(mainlist); + printf("Current elements:\n"); + listall(mainlist); - printf("DELETING.\n"); - - delitem(&mainlist, item2); - listitems(mainlist); + item4 = additem(&mainlist); + if (NULL == item4) + { + printf("Couldn't allocate.\n"); + exit(1); + } + item4->data = foo4; + printf("Current elements:\n"); + listall(mainlist); + + movetohead(&mainlist, item2); + printf("Current elements:\n"); + listall(mainlist); + + printf("----------------------------------------------------------------------\n"); + + printf("Deleting item stored at %p\n", item3); + delitem(&mainlist, item3); + printf("Current elements:\n"); + listall(mainlist); puts(""); - delitem(&mainlist, item1); - listitems(mainlist); + delitem(&mainlist, item2); + printf("Current elements:\n"); + listall(mainlist); puts(""); - delitem(&mainlist, item3); - listitems(mainlist); + delitem(&mainlist, item1); + printf("Current elements:\n"); + listall(mainlist); puts(""); -- cgit v1.2.3