Author: jshaw
Date: Sat Sep 22 12:25:29 2007
New Revision: 3559
URL:
http://source.netsurf-browser.org?rev=3559&view=rev
Log:
Initial revision of linked list implementation
Added:
trunk/dom/test/list.c
trunk/dom/test/list.h
Added: trunk/dom/test/list.c
URL:
http://source.netsurf-browser.org/trunk/dom/test/list.c?rev=3559&view...
==============================================================================
--- trunk/dom/test/list.c (added)
+++ trunk/dom/test/list.c Sat Sep 22 12:25:29 2007
@@ -1,0 +1,64 @@
+/*
+ * This file is part of libdom test suite.
+ * Licensed under the MIT License,
+ *
http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2007 James Shaw <jshaw(a)netsurf-browser.org>
+ */
+
+#include <malloc.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#include "list.h"
+
+struct list* list_new(void) {
+ struct list* list = malloc(sizeof(struct list));
+ list->size = 0;
+ list->head = NULL;
+ list->tail = NULL;
+ return list;
+}
+
+void list_destroy(struct list* list) {
+ struct list_elt* elt = list->head;
+ while (elt != NULL) {
+ struct list_elt* nextElt = elt->next;
+ free(elt);
+ elt = nextElt;
+ }
+ free(list);
+}
+
+void list_add(struct list* list, void* data) {
+ struct list_elt* elt = malloc(sizeof(struct list_elt));
+ elt->data = data;
+ elt->next = NULL;
+ struct list_elt* tail = list->tail;
+
+ // if tail was set, make its 'next' ptr point to elt
+ if (tail != NULL) {
+ tail->next = elt;
+ }
+
+ // make elt the new tail
+ list->tail = elt;
+
+ if (list->head == NULL) {
+ list->head = elt;
+ }
+
+ // inc the size of the list
+ list->size++;
+}
+
+bool list_contains(struct list* list, void* data, int (*comparator)(const void* a, const
void* b)) {
+ struct list_elt* elt = list->head;
+ while (elt != NULL) {
+ if (comparator(elt->data, data) == 0) {
+ return true;
+ }
+ elt = elt->next;
+ }
+ return false;
+}
+
Added: trunk/dom/test/list.h
URL:
http://source.netsurf-browser.org/trunk/dom/test/list.h?rev=3559&view...
==============================================================================
--- trunk/dom/test/list.h (added)
+++ trunk/dom/test/list.h Sat Sep 22 12:25:29 2007
@@ -1,0 +1,35 @@
+/*
+ * This file is part of libdom test suite.
+ * Licensed under the MIT License,
+ *
http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2007 James Shaw <jshaw(a)netsurf-browser.org>
+ */
+
+#ifndef list_h_
+#define list_h_
+
+struct list_elt {
+ void* data;
+ struct list_elt* next;
+};
+
+struct list {
+ unsigned int size;
+ struct list_elt* head;
+ struct list_elt* tail;
+};
+
+struct list* list_new(void);
+void list_destroy(struct list* list);
+
+/**
+ * Add data to the tail of the list.
+ */
+void list_add(struct list* list, void* data);
+
+/**
+ * Tests if data is equal to any element in the list.
+ */
+bool list_contains(struct list* list, void* data, int (*comparator)(const void* a, const
void* b));
+
+#endif