00001
00007 #include "list.h"
00008
00009 #include <stdlib.h>
00010 #include <assert.h>
00011
00012
00013
00014 typedef struct s_element
00015 {
00016 void* data;
00017 struct s_element* next;
00018 } element;
00019
00020 struct s_list
00021 {
00022 element *top, *curr;
00023 };
00024
00025
00026
00027 list* newList()
00028 {
00029 list* res = (list*) malloc(sizeof(list));
00030 res->curr = res->top = (element*) calloc(1, sizeof(element));
00031 return res;
00032 }
00033
00034 void deleteList(list* src)
00035 {
00036 assert(src);
00037
00038 while ((src->curr = src->top->next))
00039 {
00040 free(src->top);
00041 src->top = src->curr;
00042 }
00043
00044 free(src->top);
00045 free(src);
00046 }
00047
00048 void listAdd(list* src, const void* data)
00049 {
00050 element* nE = (element*) malloc(sizeof(element));
00051
00052 assert(src);
00053
00054 nE->data = (void*) data;
00055 nE->next = src->curr->next;
00056 src->curr = src->curr->next = nE;
00057 }
00058
00059 void listRemove(list* src)
00060 {
00061 element* tmp;
00062
00063 assert(src);
00064
00065 tmp = src->curr;
00066
00067 if (src->top == tmp)
00068 tmp = tmp->next;
00069 if (src->top->next == tmp)
00070 src->top->next = tmp->next;
00071
00072 src->curr = tmp->next;
00073 free(tmp);
00074 }
00075
00076 void listSet(list* src, const void* data)
00077 {
00078 assert(src);
00079 assert(src->curr != src->top);
00080
00081 src->curr->data = (void*) data;
00082 }
00083
00084 void* listGet(list* src)
00085 {
00086 assert(src);
00087 return src->curr->data;
00088 }
00089
00090 int listNext(list* src)
00091 {
00092 assert(src);
00093 if (src->curr->next)
00094 {
00095 src->curr = src->curr->next;
00096 return 1;
00097 }
00098 return 0;
00099 }
00100
00101 void listStart(list* src)
00102 {
00103 assert(src);
00104 src->curr = src->top;
00105 }
00106
00107 int listCount(list* src)
00108 {
00109 int res = 0;
00110 element* tmp;
00111
00112 assert(src);
00113
00114 tmp = src->top;
00115
00116 while ((tmp = tmp->next))
00117 ++res;
00118
00119 return res;
00120 }
00121
00122 int listIsEmpty(list* src)
00123 {
00124 assert(src);
00125 if (src->top->next)
00126 return 0;
00127 return 1;
00128 }