00001
00009 #include <stdlib.h>
00010 #include "list.h"
00011
00012
00013 List newList()
00014 {
00015 List list=(List)malloc(sizeof(SList));
00016
00017 if(list)
00018 {
00019 list->size=0;
00020 list->first=NULL;
00021 list->last=NULL;
00022 }
00023 return list;
00024 }
00025
00026
00027
00028 void listDelete(List list)
00029 {
00030 ListNode this,next;
00031
00032 if(!list->size) free(list);
00033 else
00034 {
00035 for(this=list->first,next=this->next;next;this=next,next=next->next)
00036 free(this);
00037
00038 free(this);
00039 free(list);
00040 }
00041 }
00042
00043
00044
00045 int listInsertFst(List list,void* inf)
00046 {
00047 ListNode new;
00048
00049 if(!list->size)
00050 {
00051 new=(ListNode)malloc(sizeof(SListNode));
00052
00053 if(new)
00054 {
00055 new->inf=inf;
00056 new->next=NULL;
00057 new->prev=NULL;
00058
00059 list->size++;
00060 list->first=new;
00061 list->last=new;
00062
00063 return 0;
00064 }
00065 else return 1;
00066 }
00067 else
00068 {
00069 new=(ListNode)malloc(sizeof(SListNode));
00070
00071 if(new)
00072 {
00073 new->inf=inf;
00074 new->next=list->first;
00075 new->prev=NULL;
00076
00077 list->size++;
00078 list->first->prev=new;
00079 list->first=new;
00080
00081 return 0;
00082 }
00083 else return 1;
00084 }
00085 }
00086
00087
00088
00089 int listInsertLst(List list,void* inf)
00090 {
00091 ListNode new;
00092
00093 if(!list->size)
00094 {
00095 new=(ListNode)malloc(sizeof(SListNode));
00096
00097 if(new)
00098 {
00099 new->inf=inf;
00100 new->next=NULL;
00101 new->prev=NULL;
00102
00103 list->size++;
00104 list->first=new;
00105 list->last=new;
00106
00107 return 0;
00108 }
00109 else return 1;
00110 }
00111 else
00112 {
00113 new=(ListNode)malloc(sizeof(SListNode));
00114
00115 if(new)
00116 {
00117 new->inf=inf;
00118 new->next=NULL;
00119 new->prev=list->last;
00120
00121 list->size++;
00122 list->last->next=new;
00123 list->last=new;
00124
00125 return 0;
00126 }
00127 else return 1;
00128 }
00129 }
00130
00131
00132
00133 int listInsertAt(List list,int index,void* inf)
00134 {
00135 int size=listSize(list);
00136 ListNode aux,new;
00137
00138 if(index<0||index>size) return 1;
00139 else if(!index) return(listInsertFst(list,inf));
00140 else if(index==size) return(listInsertLst(list,inf));
00141 else if(index>size/2)
00142 {
00143 for(index=size-index,aux=list->last;index>0;index--,aux=aux->prev);
00144
00145 new=(ListNode)malloc(sizeof(SListNode));
00146
00147 if(new)
00148 {
00149 new->inf=inf;
00150 new->next=aux->next;
00151 new->prev=aux;
00152
00153 list->size++;
00154 aux->next->prev=new;
00155 aux->next=new;
00156
00157 return 0;
00158 }
00159 else return 1;
00160 }
00161 else
00162 {
00163 for(aux=list->first;index>0;index--,aux=aux->next);
00164
00165 new=(ListNode)malloc(sizeof(SListNode));
00166
00167 if(new)
00168 {
00169 new->inf=inf;
00170 new->next=aux;
00171 new->prev=aux->prev;
00172
00173 list->size++;
00174 aux->prev->next=new;
00175 aux->prev=new;
00176
00177 return 0;
00178 }
00179 else return 0;
00180 }
00181 }
00182
00183
00184
00185 int listRemoveFst(List list,void** inf)
00186 {
00187 ListNode aux;
00188
00189 if(!list->size)
00190 {
00191 if(inf) *inf=NULL;
00192 return 1;
00193 }
00194 else if(list->size==1)
00195 {
00196 if(inf) *inf=list->first->inf;
00197 free(list->first);
00198 list->first=NULL;
00199 list->last=NULL;
00200 list->size=0;
00201
00202 return 0;
00203 }
00204 else
00205 {
00206 if(inf) *inf=list->first->inf;
00207 aux=list->first;
00208 list->first->next->prev=NULL;
00209 list->first=list->first->next;
00210 free(aux);
00211 list->size--;
00212
00213 return 0;
00214 }
00215 }
00216
00217
00218
00219 int listRemoveLst(List list,void** inf)
00220 {
00221 ListNode aux;
00222
00223 if(!list->size)
00224 {
00225 if(inf) *inf=NULL;
00226 return 1;
00227 }
00228 else if(list->size==1)
00229 {
00230 if(inf) *inf=list->last->inf;
00231 free(list->last);
00232 list->first=NULL;
00233 list->last=NULL;
00234 list->size=0;
00235
00236 return 0;
00237 }
00238 else
00239 {
00240 if(inf) *inf=list->last->inf;
00241 aux=list->last;
00242 list->last->prev->next=NULL;
00243 list->last=list->last->prev;
00244 free(aux);
00245 list->size--;
00246
00247 return 0;
00248 }
00249 }
00250
00251
00252
00253 int listRemoveAt(List list,int index,void** inf)
00254 {
00255 int size=listSize(list);
00256 ListNode aux;
00257
00258 if(index<0||index>size-1) return 1;
00259 else if(index==0) return listRemoveFst(list,inf);
00260 else if(index==size-1) return listRemoveLst(list,inf);
00261 else if(index>size/2)
00262 {
00263 for(aux=list->last,index=size-index-1;index>0;index--,aux=aux->prev);
00264
00265 if(inf) *inf=aux->inf;
00266 aux->prev->next=aux->next;
00267 aux->next->prev=aux->prev;
00268 free(aux);
00269 list->size--;
00270
00271 return 0;
00272 }
00273 else
00274 {
00275 for(aux=list->first;index>0;index--,aux=aux->next);
00276
00277 if(inf) *inf=aux->inf;
00278 aux->prev->next=aux->next;
00279 aux->next->prev=aux->prev;
00280 free(aux);
00281 list->size--;
00282
00283 return 0;
00284 }
00285 }
00286
00287
00288
00289 int listFst(List list,void** inf)
00290 {
00291 if(!list->size)
00292 {
00293 *inf=NULL;
00294 return 1;
00295 }
00296 else
00297 {
00298 *inf=list->first->inf;
00299 return 0;
00300 }
00301 }
00302
00303
00304
00305 int listLst(List list,void** inf)
00306 {
00307 if(!list->size)
00308 {
00309 *inf=NULL;
00310 return 1;
00311 }
00312 else
00313 {
00314 *inf=list->last->inf;
00315 return 0;
00316 }
00317 }
00318
00319
00320
00321 int listAt(List list,int index,void** inf)
00322 {
00323 int size=listSize(list);
00324 ListNode aux;
00325
00326 if(index<0||index>size-1)
00327 {
00328 *inf=NULL;
00329 return 1;
00330 }
00331 else
00332 {
00333 if(index>size/2) for(index=size-index-1,aux=list->last;index>0;index--,aux=aux->prev);
00334 else for(aux=list->first;index>0;index--,aux=aux->next);
00335
00336 *inf=aux->inf;
00337 return 0;
00338 }
00339 }
00340
00341
00342
00343 int listSize(List list)
00344 {
00345 return(list->size);
00346 }
00347
00348
00349
00350 int listMap(List list,void(*fun)(void*))
00351 {
00352 ListNode aux;
00353
00354 if(!list->size) return 1;
00355 else
00356 {
00357 for(aux=list->first;aux;aux=aux->next)
00358 fun(aux->inf);
00359
00360 return 0;
00361 }
00362 }
00363
00364
00365
00366 Iterator listIterator(List list)
00367 {
00368 int ctrl;
00369 ListNode aux;
00370 Iterator it;
00371
00372 it=newIt(list->size);
00373 for(aux=list->first,ctrl=0;aux&&!ctrl;aux=aux->next)
00374 ctrl=itAdd(it,aux->inf);
00375
00376 if(ctrl)
00377 {
00378 itDelete(it);
00379 return NULL;
00380 }
00381 else return it;
00382 }