Coby Lu / list

Dependents:   snake

Files at this revision

API Documentation at this revision

Comitter:
lucoby
Date:
Thu Oct 11 18:55:02 2012 +0000
Commit message:
initial

Changed in this revision

list.cpp Show annotated file Show diff for this revision Revisions of this file
list.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/list.cpp	Thu Oct 11 18:55:02 2012 +0000
@@ -0,0 +1,46 @@
+#include "mbed.h"
+#include "list.h"
+
+
+void addToHead(List* list, int row, int col)
+{
+
+    Lnode* head = (Lnode*) malloc(sizeof(Lnode));
+    head->row = row;
+    head->col = col;
+    head->next = list->head;
+    head->prev = NULL;
+    if(list->head) {
+        list->head->prev = head;
+        list->head = head;
+    } else {
+        list->head = head;
+        list->tail = head;
+    }
+}
+
+void removeFromTail(List* list)
+{
+    if(list->tail) {
+        list->tail = list->tail->prev;
+        free(list->tail->next);
+        if(list->tail) {
+            list->tail->next = NULL;
+        } else {
+            list->head = NULL;
+        }
+    }
+}
+
+void deleteList(List* list)
+{
+    Lnode* phere = list->head;
+    Lnode* temp;
+    while(phere != NULL) {
+        temp = phere->next;
+        free(phere);
+        phere = temp;
+    }
+    list->head = NULL;
+    list->tail = NULL;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/list.h	Thu Oct 11 18:55:02 2012 +0000
@@ -0,0 +1,22 @@
+#ifndef LIST_H
+#define LIST_H
+#include "mbed.h"
+
+ typedef struct _lnode{
+    int row;
+    int col;
+    struct _lnode* next;
+    struct _lnode* prev;
+} Lnode;
+
+
+typedef struct _list{
+    Lnode* head;
+    Lnode* tail;
+} List;
+
+void addToHead(List* list, int row, int col);
+void removeFromTail(List* list);
+void deleteList(List* list);
+
+#endif