#include #include #include #include void randstack(int i){ int j=rand(); if (i>0){ randstack(i-1); } } typedef struct Cell { int value; struct Cell * next, * prev; } s_cell; s_cell * newCell(int value){ s_cell * p = malloc(sizeof(s_cell)); if (p==NULL){ fprintf(stderr,"Malloc problem"); exit(EXIT_FAILURE); } (*p).value = value; return p; } s_cell * append(s_cell * p_list, s_cell * p_newCell){ s_cell * p; if (p_list==NULL){ return p_newCell; } p=p_list; while(p->next != NULL){ p=p->next; } p->next = p_newCell; p_newCell->prev = p; return p_list; } s_cell *removeValue(s_cell * p_list , int value){ s_cell * p_tmp; if (p_list==NULL){ return NULL; } if (p_list->value==value) { p_tmp=p_list->next; free(p_list); return removeValue(p_tmp,value); } p_list->next = removeValue(p_list->next,value); return p_list; } s_cell * reverseList(s_cell * p_list){ s_cell * p_tmp; if (p_list==NULL){ return NULL; } p_tmp = p_list->next; p_list->next = p_list->prev; p_list->prev = p_tmp; if (p_tmp==NULL){ return p_list; }else{ return reverseList(p_tmp); } } void print(s_cell * p_l){ if (p_l==NULL) { printf("\n"); return; } printf("%d ",p_l->value); print(p_l->next); } void freeList(s_cell * p_l){ s_cell * p_tmp; if (p_l==NULL){ return; } p_tmp = p_l->next; free(p_l); freeList(p_tmp); } int main(void){ srand(time(NULL)); s_cell * p_list = NULL; int i; randstack(100); for(i=0;i<100;i++){ p_list = append(p_list,newCell(i)); } print(p_list); for(i=10;i<50;i=i+2){ p_list = removeValue(p_list,i); } print(p_list); p_list = reverseList(p_list); print(p_list); freeList(p_list); return EXIT_SUCCESS; }