make the list a type itself

master
orange 11 years ago
parent 7e2f62da99
commit 7f85731e5e

@ -3,17 +3,27 @@
#include <stdlib.h> #include <stdlib.h>
#include "assert.h" #include "assert.h"
struct list_node { struct list_node {
int data; int data;
struct list_node *next; struct list_node *next;
}; };
typedef struct list_node list_node_t; typedef struct list_node list_node_t;
int list_length(list_node_t * list) { struct list {
list_node_t * head;
};
typedef struct list list_t;
void list_init (list_t * list) {
list->head = NULL;
}
int list_length(list_t * list) {
list_node_t *cur; list_node_t *cur;
int count = 0; int count = 0;
cur = list; cur = list->head;
while (cur != NULL) { while (cur != NULL) {
count++; count++;
cur = cur->next; cur = cur->next;
@ -22,14 +32,14 @@ int list_length(list_node_t * list) {
return count; return count;
} }
bool list_empty(list_node_t * list) { bool list_empty(list_t * list) {
return (list == NULL); return (list->head == NULL);
} }
void list_print(list_node_t * list) { void list_print(list_t * list) {
list_node_t *cur; list_node_t *cur;
cur = list; cur = list->head;
while (cur != NULL) { while (cur != NULL) {
printf("%d ", cur->data); printf("%d ", cur->data);
cur = cur->next; cur = cur->next;
@ -37,22 +47,29 @@ void list_print(list_node_t * list) {
printf("\n"); printf("\n");
} }
void list_add(list_node_t * list, list_node_t * item) { void list_add(list_t * list, list_node_t * item) {
list_node_t *cur; list_node_t *cur;
cur = list; if (list_empty(list)) {
assert(cur != NULL); list->head = item;
} else {
cur = list->head;
while (cur->next != NULL) { while (cur->next != NULL) {
cur = cur->next; cur = cur->next;
} }
cur->next = item; cur->next = item;
}
} }
void list_remove(list_node_t * list, list_node_t * item) { void list_remove(list_t * list, list_node_t * item) {
list_node_t *cur; list_node_t *cur;
cur = list; if (list->head->data == item->data) {
list->head = list->head->next;
return;
}
cur = list->head;
assert(cur != NULL); assert(cur != NULL);
while (cur->next != NULL) { while (cur->next != NULL) {
@ -64,28 +81,34 @@ void list_remove(list_node_t * list, list_node_t * item) {
} }
} }
/* FIXME
* list_node_t list_revert(list_node_t *list) {
} */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
list_node_t *emptylist = NULL; list_t *emptylist = malloc(sizeof(list_t));
list_init(emptylist);
assert(list_length(emptylist) == 0); assert(list_length(emptylist) == 0);
assert(list_empty(emptylist) == true); assert(list_empty(emptylist) == true);
list_print(emptylist); list_print(emptylist);
printf("list_length = %d\n", list_length(emptylist)); printf("list_length = %d\n", list_length(emptylist));
list_t *foolist = malloc(sizeof(list_t));
list_node_t foo = { 1, NULL }; list_node_t foo = { 1, NULL };
list_add(foolist, &foo);
assert(list_length(&foo) == 1); assert(list_length(foolist) == 1);
assert(list_empty(&foo) == false); assert(list_empty(foolist) == false);
list_print(&foo); list_print(foolist);
printf("list_length = %d\n", list_length(&foo)); printf("list_length = %d\n", list_length(foolist));
// append something to foo // append something to foo
list_node_t bar = { 2, NULL }; list_node_t bar = { 2, NULL };
list_add(&foo, &bar); list_add(foolist, &bar);
assert(list_length(&foo) == 2); assert(list_length(foolist) == 2);
assert(list_length(&bar) == 1); list_print(foolist);
list_print(&foo); printf("list_length = %d\n", list_length(foolist));
printf("list_length = %d\n", list_length(&foo));
// append more // append more
const int howmany = 100; const int howmany = 100;
@ -93,31 +116,54 @@ int main(int argc, char *argv[]) {
list_node_t *newnode = malloc(sizeof(list_node_t)); list_node_t *newnode = malloc(sizeof(list_node_t));
newnode->data = i; newnode->data = i;
list_add(&foo, newnode); list_add(foolist, newnode);
} }
assert(list_length(&foo) == 2 + howmany); assert(list_length(foolist) == 2 + howmany);
list_print(&foo); list_print(foolist);
printf("list_length = %d\n", list_length(&foo)); printf("list_length = %d\n", list_length(foolist));
// remove something // remove something
list_node_t toremove = { 23, NULL }; list_node_t toremove = { 23, NULL };
list_remove(&foo, &toremove); list_remove(foolist, &toremove);
assert(list_length(&foo) == 2 + howmany - 1); assert(list_length(foolist) == 2 + howmany - 1);
list_print(&foo); list_print(foolist);
printf("list_length = %d\n", list_length(&foo)); printf("list_length = %d\n", list_length(foolist));
// remove last item
toremove.data = howmany - 1;
list_remove(foolist, &toremove);
assert(list_length(foolist) == 2 + howmany - 2);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
// remove something // remove something
toremove.data = 99; toremove.data = 1;
list_remove(&foo, &toremove); list_remove(foolist, &toremove);
assert(list_length(&foo) == 2 + howmany - 2); assert(list_length(foolist) == 2 + howmany - 3);
list_print(&foo); list_print(foolist);
printf("list_length = %d\n", list_length(&foo)); printf("list_length = %d\n", list_length(foolist));
// remove something // remove something
toremove.data = 1; toremove.data = 1;
list_remove(&foo, &toremove); list_remove(foolist, &toremove);
/* FIXME can't delete first 1 */ assert(list_length(foolist) == 2 + howmany - 4);
assert(list_length(&foo) == 2 + howmany - 3); list_print(foolist);
list_print(&foo); printf("list_length = %d\n", list_length(foolist));
printf("list_length = %d\n", list_length(&foo));
/* remove everything */
for (int i = 0; i < howmany; i++) {
toremove.data = i;
list_remove(foolist, &toremove);
}
assert(list_length(foolist) == 1);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
/* remove last remaining item */
toremove.data = 2;
list_remove(foolist, &toremove);
assert(list_length(foolist) == 0);
assert(list_empty(foolist) == true);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
} }

Loading…
Cancel
Save