Commits

Guillaume Duhamel committed 5de39f9

A linked list structure that should be useful...

Comments (0)

Files changed (2)

+/*  Copyright 2011 Guillaume Duhamel
+
+    This file is part of Gophrier.
+
+    Gophrier is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    Gophrier is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Gophrier; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+*/
+
+#include "list.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct {
+    List * first;
+    List * last;
+} ListMeta;
+
+static ListMeta * list_meta_new()
+{
+    ListMeta * meta;
+
+    meta = (ListMeta *) malloc(sizeof(ListMeta));
+    meta->first = NULL;
+    meta->last = NULL;
+
+    return meta;
+}
+
+static List * list_meta_push(ListMeta * meta, List * list)
+{
+    list->meta = meta;
+
+    if (NULL == meta->first)
+        meta->first = list;
+
+    if (NULL != meta->last)
+        meta->last->next = list;
+    meta->last = list;
+
+    return meta->first;
+}
+
+static List * list_new(void * elem)
+{
+    List * list;
+
+    list = (List *) malloc(sizeof(List));
+    list->current = elem;
+    list->next = NULL;
+    list->meta = NULL;
+
+    return list;
+}
+
+List * list_push(List * list, void * elem)
+{
+    List * newlist;
+    ListMeta * meta;
+
+    meta = (list == NULL) ? list_meta_new() : list->meta;
+    newlist = list_new(elem);
+    return list_meta_push(meta, newlist);
+}
+/*  Copyright 2011 Guillaume Duhamel
+
+    This file is part of Gophrier.
+
+    Gophrier is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    Gophrier is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Gophrier; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+*/
+
+#ifndef GOPHRIER_LIST_H
+#define GOPHRIER_LIST_H
+
+#include "core.h"
+
+struct _List
+{
+    void * meta;
+    void * current;
+    List * next;
+};
+
+List * list_push(List * list, void * elem);
+
+#endif