static void clear_data(Parser *parser) { if (parser != NULL) { List_destroy(parser->token_list, (Destructor) Token_destroy); if (parser->groups != NULL) { unsigned int i; for (i = 0; i < parser->group_count; ++i) { List_destroy(parser->groups[i], (Destructor) Token_destroy); } free(parser->groups); parser->groups = NULL; } if (parser->ranges != NULL) { unsigned int i; for (i = 0; i < parser->range_count; ++i) { List_destroy(parser->ranges[i], (Destructor) Token_destroy); } free(parser->ranges); parser->ranges = NULL; } parser->token_list = List_create(); parser->group_count = 0; parser->range_count = 0; } }
void Parser_destroy(Parser *parser) { if (parser != NULL) { List_destroy(parser->token_list, (Destructor) Token_destroy); if (parser->groups != NULL) { unsigned int i; for (i = 0; i < parser->group_count; ++i) { List_destroy(parser->groups[i], (Destructor) Token_destroy); } free(parser->groups); parser->groups = NULL; } if (parser->ranges != NULL) { unsigned int i; for (i = 0; i < parser->range_count; ++i) { List_destroy(parser->ranges[i], (Destructor) Token_destroy); } free(parser->ranges); parser->ranges = NULL; } free(parser); parser = NULL; } }
List *List_merge_sort(List *list, List_compare cmp) { // 未初始化的List,视为不能排序 if(list == NULL) return NULL; // 空List和只有一个节点的List视为已经排序 if(List_count(list) < 2) return list; int i = 1; ListNode *cur = list->first; List *left = List_create(); List *right= List_create(); int middle = List_count(list) / 2; // 拆成两个List,分别排序 for(i = 1; i < middle; i++) { List_push(left, cur->value); cur=cur->next; } for(i = 1; i <= List_count(list) - middle; i++) { List_push(right, cur->value); cur=cur->next; } List *sort_left = List_merge_sort(left, cmp); List *sort_right = List_merge_sort(right, cmp); if(sort_left != left) List_destroy(left); if(sort_right != right) List_destroy(right); // merge return List_merge(sort_left, sort_right, cmp); }
List *List_merge_sort(List *list, List_compare cmp){ if(List_count(list)<=1){ return list; } List *left=List_create(); List *right=List_create(); int middle=List_count(list)/2; LIST_FOREACH(list, first, next, cur){ if(middle>0){ List_push(left, cur->value); } else { List_push(right, cur->value); } middle--; } List *sort_left=List_merge_sort(left, cmp); List *sort_right=List_merge_sort(right, cmp); if(sort_left !=left) List_destroy(left); if(sort_right !=right)List_destroy(right); return List_merge(sort_left, sort_right, cmp); }
/** * \brief Close the Var component * \private */ void Var_close(void) { List* keys; int n; if(initialized) { /* Free readonly variable cache */ keys = Dictionary_getKeys(ro_cache); n = List_getSize(keys); for(int i = 0; i < n; i++) { free(Dictionary_get(ro_cache, List_get(keys, i))); } List_destroy(keys); Dictionary_destroy(ro_cache); /* Free subscriptions */ keys = Dictionary_getKeys(subscriptions); n = List_getSize(keys); for(int i = 0; i < n; i++) { free(Dictionary_get(subscriptions, List_get(keys, i))); } List_destroy(keys); Dictionary_destroy(subscriptions); initialized = false; } }
List * List_insertList(List * list, int position, List * insertList) { ListIterator * newIterator; ListIterator * insertIterator; void * data; List * newList; if(list == NULL || insertList == NULL) return NULL; if(position < 0 || position > List_getLength(list)) return NULL; if(position == 0) return List_prependList(list, insertList); if(position == List_getLength(list)) return List_appendList(list, insertList); newList = List_clone(list); newIterator = ListIterator_create(newList); insertIterator = ListIterator_create(insertList); if(ListIterator_seekToPosition(newIterator, position) == NULL) { ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); List_destroy(newList); return NULL; } data = ListIterator_seekToFirst(insertIterator); while(data != NULL) { if(!ListIterator_insertValue(newIterator, data)) { ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); List_destroy(newList); return NULL; } ListIterator_nextItem(newIterator); data = ListIterator_nextItem(insertIterator); } ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); return newList; }
int main(int argc, char *argv[]) { List *list; List *right; list = List_create(sizeof(int)); if (list == NULL) { errExit("List_create"); } List_blaster_test(list); List_pop_test(list); List_blaster_test(list); List_pop_left_test(list); List_blaster_test(list); right = List_split_test(list); List_join_test(list, right); List_copy_test(list); List_multithread_test(list); List_debug(list); List_bubble_sort_test(list); List_debug(list); List_mergesort_test(list); List_debug(list); List_destroy(list); exit(EXIT_SUCCESS); }
void test(){ /* 测试链表函数 测试结果:全部函数运行通过 */ List A; int a[]={1,2,3,4}; List B; int b[]={5,6,7,8,9}; List_Init(&A); List_Init(&B); List_creat(&A,a,4); List_creat(&B,b,5); printf("\nA:"); List_printf(&A); printf("\nB:"); List_printf(&B); List_Insert(&A,1,2); printf("\nA:"); List_printf(&A); printf("\nA length:%d",List_getLength(&A)); List_clear(&B); printf("\nB:"); List_printf(&B); List_destroy(&B); }
void EH_destroy() { if(isInitialised) { List_destroy(&EventHandlers); isInitialised = false; } }
List *List_merge(List *list1, List *list2, List_compare cmp) { List *result = List_create(); // 存放排序好的List // 数据域 void *val = NULL; // 如果list1的数据小,则push之,并unshift list1 // 否则,push list2的数据,并unshift list2 while(List_count(list1) > 0 && List_count(list2) > 0) { if(cmp((char *)List_first(list1) , (char *)List_first(list2)) >= 0) { val = List_unshift(list2); List_push(result, val); } else { val = List_unshift(list1); List_push(result, val); } } // push剩余的数据 if(List_count(list1) > 0) { while(List_count(list1) > 0) { val =List_unshift(list1); List_push(result, val); } } else { while(List_count(list2) > 0) { val =List_unshift(list2); List_push(result, val); } } List_destroy(list1); List_destroy(list2); return result; }
void Mod_fw_end_log_capture(FW_handle_T handle) { struct fw_handle *fwh = handle->fwh; List_destroy(&fwh->entries); pcap_close(fwh->pcap_handle); }
char *test_merge_sort() { List *words = create_words(); // should work on a list that needs sorting List *res = List_merge_sort(words, (List_compare)strcmp); mu_assert(is_sorted(res), "Words are not sorted after merge sort"); List *res2 = List_merge_sort(res, (List_compare)strcmp); mu_assert(is_sorted(res2), "should still be sort after merge sort"); List_destroy(res2); List_destroy(res); List_destroy(words); return NULL; }
List * List_removeRange(List * list, int start, int end) { List * newList; int length, i; void * data; ListIterator * iterator; if(list == NULL) return NULL; if(start < 0 || start >= List_getLength(list)) return NULL; if(end < 0 || end >= List_getLength(list)) return NULL; if(end < start) return NULL; length = end - start; newList = List_create(); iterator = ListIterator_create(list); if(ListIterator_seekToPosition(iterator, start) == NULL) { ListIterator_destroy(iterator); List_destroy(newList); return NULL; } for(i = 0; i <= length; i++) { data = ListIterator_removeCurrent(iterator); if(data == NULL) { ListIterator_destroy(iterator); List_destroy(newList); } List_addBack(newList, data); } ListIterator_destroy(iterator); return newList; }
/** * \brief Free a sources list * * Free a sources list as returned by SVR_getSourcesList * * \param sources_list List to free */ void SVR_freeSourcesList(List* sources_list) { char* source_name; for(int i = 0; (source_name = List_get(sources_list, i)) != NULL; i++) { free(source_name); } List_destroy(sources_list); }
void destroy_properties_list (prop_list box) { for (prop_list_iterator it = prop_list_begin (box); it != NULL; prop_list_iterator_next (&it)) { destroy_property (it->val); } List_destroy (box); }
void destroy_svcs_list (svc_list box) { for (svc_list_iterator it = svc_list_begin (box); it != NULL; svc_list_iterator_next (&it)) { destroy_svc (it->val); } List_destroy (box); }
void List_destroy(List * list) { //Base Case if(list==NULL) return; //Recursive Case List_destroy(list->next); free(list->str); free(list); }
/** * Destroys a linked list. * Arguments: * head A pointer pointing to the first element of the linked list. * * Returns: * void * * Destroys (frees memory for) the whole linked list. * You can either use recursion or a while loop. */ void List_destroy(Node * head) { if (head == NULL) { return; } List_destroy(head->next); free(head); }
void free_fd_state(struct fd_state *state) { event_free(state->read_event); event_free(state->write_event); RingBuffer_destroy(state->buffer); List_destroy(state->outputs); rtmp_destroy(state->rtmp); free(state); }
List *Path(World *world, Point *source, Point *destination) { int tentative_gscore; List *result = NULL; int tries = 0; Hashmap *nodes = Hashmap_create(cmp, hash); Hashmap *closedset = Hashmap_create(cmp, hash); PQueue *openset = PQueue_create(cmp, hash); Node *current; Node *start = Node_create(source, 0, 0, NULL); Hashmap_set(nodes, start, start); start->fscore = start->gscore + heuristic_cost_estimate(start->point, destination); PQueue_push(openset, start, start->fscore); while(!PQueue_empty(openset) && tries < 300) { tries++; current = PQueue_pop(openset); Hashmap_set(closedset, current, current); if(POINT_EQ(current->point, destination)) { result = reconstruct_path(current); break; } else { List *neighbours = neighbours_list(world, current->point, destination, nodes); LIST_FOREACH(neighbours, first, next, cur) { Node *neighbour = cur->value; if(Hashmap_get(closedset, neighbour) == NULL) { tentative_gscore = current->gscore + 1; if(!PQueue_contains(openset, neighbour) || tentative_gscore > neighbour->gscore) { if(!PQueue_contains(openset, neighbour)) { neighbour->came_from = current; neighbour->gscore = tentative_gscore; neighbour->fscore = neighbour->gscore + heuristic_cost_estimate(neighbour->point, destination); PQueue_push(openset, neighbour, neighbour->fscore); } } } } List_destroy(neighbours); } }
/** * Free all memory associated with the linked list, including memory for * contained strings. Must safely handle NULL lists. */ void List_destroy(List * list){ //the list is null if (list == NULL) { return; } //the list is not null List_destroy(list -> next); free(list -> str); //freeing the string free(list); }
/* * _cerebrod_monitor_module_list_destroy * * Destroy a monitor_module_list struct. */ static void _cerebrod_monitor_module_list_destroy(void *data) { struct cerebrod_monitor_module_list *ml; assert(data); ml = (struct cerebrod_monitor_module_list *)data; List_destroy(ml->monitor_list); Free(ml); }
/* * _cerebrod_event_module_list_destroy * * Destroy a event_module_list struct. */ static void _cerebrod_event_module_list_destroy(void *data) { struct cerebrod_event_module_list *el; assert(data); el = (struct cerebrod_event_module_list *)data; List_destroy(el->event_list); Free(el); }
List *List_merge_sort(List *list, List_compare *cmp) { if (list->count == 1) { List *res = List_create(); List_push(res, list->first->value); return res; } int middle = list->count / 2; List *l= List_sublist(list, 0, middle); List *r= List_sublist(list, middle, list->count - middle); List *left = List_merge_sort(l, cmp); List *right = List_merge_sort(r, cmp); List_destroy(l); List_destroy(r); ListNode* leftNode = left->first; ListNode* rightNode = right->first; List *res = List_create(); while (leftNode != NULL || rightNode != NULL) { if (leftNode == NULL) { List_push(res, rightNode->value); rightNode = rightNode->next; } else if (rightNode == NULL) { List_push(res, leftNode->value); leftNode = leftNode->next; } else if (cmp(leftNode->value, rightNode->value) < 0) { List_push(res, leftNode->value); leftNode = leftNode->next; } else { List_push(res, rightNode->value); rightNode = rightNode->next; } } List_destroy(left); List_destroy(right); return res; }
void pt_destroy (process_tracker_t * pt) { for (pid_list_iterator it = pid_list_begin (pt->pids); it != NULL; pid_list_iterator_next (&it)) { pt_disregard_pid (pt, *it->val); free (it->val); } List_destroy (pt->pids); s16mem_free (pt); }
void destroy_svc (svc_t * delSvc) { for (inst_list_iterator it = inst_list_begin (delSvc->instances); it != NULL; inst_list_iterator_next (&it)) { destroy_instance (it->val); } List_destroy (delSvc->instances); destroy_properties_list (delSvc->properties); free (delSvc->name); free (delSvc); }
static List *merge_two_parts( List *first , List * second , List_compare cmp){ /* get frist node in each , cmp copy to another * make new list to copy into * return */ List *dest = List_create(); assert( dest != NULL); ListNode *r1 ,*r2; r1 = first->first; r2 = second->first; while( r1 != NULL && r2 != NULL ){ if( cmp( r1->value , r2->value) <= 0){ List_push(dest , r1->value); r1 = r1->next; } else{ List_push(dest , r2->value); r2 = r2->next; } } while( r1 != NULL){ List_push(dest , r1->value); r1 = r1->next; } while(r2 != NULL){ List_push(dest , r2->value); r2 = r2->next; } List_destroy( first ); List_destroy( second); return dest; }
unit_t * unit_new (svc_t * svc, svc_instance_t * inst) { #define CompareType(typ) \ !strcasecmp (svc_object_get_property_string (svc, "Unit.Strategy"), typ) unit_t * unitnew = s16mem_alloc (sizeof (unit_t)); unitnew->name = inst_object_get_property_string (inst, "S16.FMRI"); unitnew->state = S_OFFLINE; unitnew->svc = svc; unitnew->inst = inst; unitnew->timer_id = 0; unitnew->main_pid = 0; unitnew->rtype = R_YES; unitnew->state = S_OFFLINE; unitnew->pids = List_new (); if (!unitnew->pids) { fprintf (stderr, "unit alloc! (%s pids)\n", inst_object_get_property_string (inst, "S16.FMRI")); return 0; } if (CompareType ("exec")) unitnew->type = T_EXEC; else if (CompareType ("forks")) unitnew->type = T_FORKS; else if (CompareType ("oneshot")) unitnew->type = T_ONESHOT; else { fprintf (stderr, "Unit <%s> lacks a known type\n", unitnew->name); List_destroy (unitnew->pids); s16mem_free (unitnew); return 0; } unitnew->method[M_PRESTART] = svc_object_get_property_string (svc, "Method.Prestart"); unitnew->method[M_START] = svc_object_get_property_string (svc, "Method.Start"); unitnew->method[M_POSTSTART] = svc_object_get_property_string (svc, "Method.Poststart"); unitnew->method[M_STOP] = svc_object_get_property_string (svc, "Method.Stop"); unitnew->timeout_secs = 12; fprintf (stderr, "[%s] new unit formed\n", unitnew->name); return unitnew; }
/* * _metric_list * * Output list of all available metrics */ static void _metric_list(void) { const char *func = __FUNCTION__; cerebro_namelist_t m = NULL; cerebro_namelist_iterator_t mitr = NULL; List l = NULL; ListIterator litr = NULL; char *str; if (!(m = cerebro_get_metric_names(handle))) { char *msg = cerebro_strerror(cerebro_errnum(handle)); _clean_err_exit(cerebro_errnum(handle)); err_exit("%s: cerebro_get_metric_names: %s", func, msg); } if (!(mitr = cerebro_namelist_iterator_create(m))) { char *msg = cerebro_strerror(cerebro_namelist_errnum(m)); err_exit("%s: cerebro_namelist_iterator_create: %s", func, msg); } l = List_create(NULL); while (!cerebro_namelist_iterator_at_end(mitr)) { if (cerebro_namelist_iterator_name(mitr, &str) < 0) { char *msg = cerebro_strerror(cerebro_namelist_iterator_errnum(mitr)); err_exit("%s: cerebro_namelist_iterator_name: %s", func, msg); } List_append(l, str); if (cerebro_namelist_iterator_next(mitr) < 0) { char *msg = cerebro_strerror(cerebro_namelist_iterator_errnum(mitr)); err_exit("%s: cerebro_namelist_iterator_next: %s", func, msg); } } litr = List_iterator_create(l); while ((str = list_next(litr))) fprintf(stdout, "%s\n", str); /* List_destroy() and cerebro_namelist_destory() destroy iterators too */ List_destroy(l); (void)cerebro_namelist_destroy(m); }
void Menu_destroy(Menu * menu) { if (menu) { free(menu->rect); SDL_FreeSurface(menu->surface); Node* actual = menu->buttons->begin; Node * aux = NULL; while (actual != NULL) { aux = actual->next; if (actual->value != NULL) { Button_destroy((Button *) actual->value); } actual = aux; } actual = menu->inputs->begin; aux = NULL; while (actual != NULL) { aux = actual->next; if (actual->value != NULL) { Input_destroy((Input *) actual->value); } actual = aux; } Menu_destroy_labels(menu); List_destroy(menu->buttons); List_destroy(menu->inputs); List_destroy(menu->labels); menu = NULL; } }