int main() { CList *list = (CList*)malloc(sizeof(CList)); clist_init(list,destroy); Stu *stu = (Stu*)malloc(sizeof(Stu)); stu->age = 11; stu->name ="HeXinyuan"; clist_ins_next(list,NULL,(void *)stu); Stu *stu2 = (Stu*)malloc(sizeof(Stu)); stu2->age = 22; stu2->name ="ZhangJingli"; clist_ins_next(list,clist_head(list),(void *)stu2); Stu *newStu = createStu(33,"HeXiaoxiao"); clist_ins_next(list,clist_head(list),(void *)newStu); printf("clist_size()= %d\n",clist_size(list)); Stu *result = (Stu*)(clist_head(list)->data); CListElmt *head = clist_head(list); while(head!=NULL){ Stu* res = (Stu*)(head->data); printf("res->age = %d,res->name=%s\n",res->age,res->name); head = head->next; if(clist_head(list) == head){ break; } } void *dataStu; clist_rem_next(list,clist_head(list),&dataStu); printf("%d,%s\n",((Stu*)(dataStu))->age,((Stu*)(dataStu))->name); clist_destroy(list); return 0; }
void clist_destroy(CList * list) { void *data; /***************************************************************************** * Remove each element. * *****************************************************************************/ while (clist_size(list) > 0) { if (clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) { /*********************************************************************** * Call a user-defined function to free dynamically allocated data. * ***********************************************************************/ list->destroy(data); } } /***************************************************************************** * No operations are allowed now, but clear the structure as a precaution. * *****************************************************************************/ memset(list, 0, sizeof(CList)); return; }
void clist_destroy(CList *list) { void *data; //remove each elem while (clist_size(list) > 0) { if (clist_rem_next(list, list->head, (void **) &data) == 0 && list->destroy != NULL) { list->destroy(data); } } memset(list, 0, sizeof(CList)); return; };
void clist_destroy(CList *list) { void * data; while (clist_size(list) > 0) { if (clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) { /* Call a user-defined function to free dynamically allocated data. */ list->destroy(data); } } memset(list, 0, sizeof(CList)); return; }
void clist_destroy(CList *list) { void *data; while (clist_size(list) > 0) { if(clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) { list->destroy(data); } } // 现在不允许任何操作了,不过为了确保安全得清理一下 memset(list, 0, sizeof(CList)); return ; }
void clist_destroy(CList *list) { void *data; while (clist_size(list) > 0) { if (clist_rem_next(list, list->head, (void **)&data) == 0 && list->destroy != NULL) { list->destroy(data); } } /* Clear structure as precaution */ memset(list, 0, sizeof(CList)); return; }
int main() { //linked list CList *l; int *data = 0; int *data1 = 1; int *data2 = 2; clist_init(l, (void *) &data); printf("Circular list has been initalized\n"); clist_ins_next(l, NULL, data); clist_ins_next(l, clist_head(l), data1); clist_ins_next(l, clist_next(clist_head(l)), data2); printf("Current linked list size %d\n", clist_size(l)); printf("Head elem %d\n", clist_head(l)->data); printf("Next of head %d\n", clist_head(l)->next->data); printf("Next next of head %d\n", clist_head(l)->next->next->data); clist_rem_next(l, clist_head(l)->next, (void *) &data2); clist_rem_next(l, clist_head(l), (void *) &data1); clist_rem_next(l, clist_head(l), (void *) &data); printf("Linked list has been cleared\n"); if (clist_size(l) == 0) { clist_destroy(l); printf("Circular list has been destroyed\n"); } else { printf("Can't delete list. It's not empty %d\n", clist_size(l)); }; return 0; }
int clist_destroy(CList *list) { void *data = NULL; while (clist_size(list) > EMPTY) { if (OK == clist_rem_next(list, list->head, (void **)&data) && NULL != list->destroy) { list->destroy(data); } } memset(list, 0, sizeof(Clist)); return OK; }
void clist_destroy(CList *list) { void *data; if (list == NULL) { return; } while(clist_rem_next(list, NULL, (void**)&data) == 0) { if (list->destroy != NULL) { list->destroy(data); } } memset(list, 0x00, sizeof(CList)); return; }
void clist_destroy(CList *list) { /* Will point to data payload to be removed for each element */ void *data; /* Remove each element (and its payload) */ while (clist_size(list) > 0) { if (clist_rem_next(list, NULL, (void **)&data) != 0) { fprintf(stderr, "Problem removing element from CList\n"); return; } if (list->destroy != NULL) list->destroy(data); } /* Zero list out */ memset(list, 0, sizeof(CList)); return; }
/* --------------------------------- main -------------------------------- */ int main(int argc, char **argv) { CList list; CListElmt *element; int *data, i; /* Initialize the circular list */ clist_init(&list, free); /* Perform some circular list operations */ element = clist_head(&list); for (i = 0; i < 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i + 1; if (clist_ins_next(&list, element, data) != 0) return 1; if (element == NULL) element = clist_next(clist_head(&list)); else element = clist_next(element); } print_list(&list); element = clist_head(&list); for (i = 0; i < 10; i++) element = clist_next(element); data = clist_data(element); fprintf(stdout, "Circling and removing an element after the one containing " "%03d\n",*data); if (clist_rem_next(&list, element, (void **)&data) != 0) return 1; free(data); print_list(&list); element = clist_head(&list); for (i = 0; i < 15; i++) element = clist_next(element); data = clist_data(element); fprintf(stdout, "Circling and inserting 011 after the element containing " "%03d\n", *data); if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 11; if (clist_ins_next(&list, element, data) != 0) return 1; print_list(&list); /* Destroy the circular list */ fprintf(stdout, "Destroying the list\n"); clist_destroy(&list); return 0; }