void * list_set_value(linked_list_t *list, size_t pos, void *newval) { void *old_value = NULL; MUTEX_LOCK(list->lock); list_entry_t *entry = pick_entry(list, pos); if (entry) { old_value = entry->value; entry->value = newval; } else { list_insert_value(list, newval, pos); } MUTEX_UNLOCK(list->lock); return old_value; }
static void binomial_tree_node_destroy(binomial_tree_node_t *node, int rindex) { int i; binomial_tree_node_t *new_parent = NULL; if (node->parent) { new_parent = node->parent; int node_index = -1; for (i = 0; i < new_parent->num_children; i++) { if (new_parent->children[i] == node) { node_index = i; break; } } if (new_parent->num_children && node_index >= 0) { int to_copy = new_parent->num_children - (node_index + 1); if (to_copy) { memmove(&new_parent->children[node_index], &new_parent->children[node_index+1], sizeof(binomial_tree_node_t *) * to_copy); } else { // TODO - Error messages // (something is badly corrupted if we are here) } new_parent->num_children--; } } else { // the node is a root node, let's first remove it from the list of trees int item_num = 0; if (rindex) { (void)list_fetch_value(node->bh->trees, rindex); item_num = rindex + 1; } else { item_num = list_foreach_value(node->bh->trees, binheap_remove_root_node, node); } if (node->num_children) { int child_index = node->bh->mode == BINHEAP_MODE_MAX ? binomial_tree_node_find_max_child(node) : binomial_tree_node_find_min_child(node); if (child_index >= 0) { new_parent = node->children[child_index]; if (child_index < node->num_children - 1) { memmove(&node->children[child_index], &node->children[child_index + 1], sizeof(binomial_tree_node_t *) * (node->num_children - (child_index + 1))); } node->num_children--; new_parent->parent = NULL; if (item_num > 0) // and finally add one of its child back to the list of trees list_insert_value(node->bh->trees, new_parent, item_num - 1); } } } for (i = 0; i < node->num_children; i++) { if (new_parent) binomial_tree_node_add(new_parent, node->children[i]); else node->children[i]->parent = NULL; } if (node == node->bh->head) UPDATE_HEAD(node->bh); free(node->key); node->bh->count--; free(node); }
int main(int argc, char **args) { //list1 x^10 s_list *list1 = (s_list *) malloc(sizeof(s_list)); list_init(list1); list_insert_value(list1, 0, 10, 1); //list2 2x^6 s_list *list2 = (s_list *) malloc(sizeof(s_list)); list_init(list2); list_insert_value(list2, 0, 6, 2); //list1 (x^10 + 2x^6) list_append(list1, list2); //list3 (x^10 + 2x^6)y^3 s_list *list3 = (s_list *) malloc(sizeof(s_list)); list_init(list3); list_insert_value(list3, 1, 3, (int) list1); //list4 3x^5 s_list *list4 = (s_list *) malloc(sizeof(s_list)); list_init(list4); list_insert_value(list4, 0, 5, 3); //list5 (3x^5)y^2 s_list *list5 = (s_list *) malloc(sizeof(s_list)); list_init(list5); list_insert_value(list5, 1, 2, (int) list4); //list3 ((x^10 + 2x^6)y^3 + (3x^5)y^2) list_append(list3, list5); //list6 ((x^10 + 2x^6)y^3 + (3x^5)y^2)z^2 s_list *list6 = (s_list *) malloc(sizeof(s_list)); list_init(list6); list_insert_value(list6, 1, 2, (int) list3); //list7 x^4 s_list *list7 = (s_list *) malloc(sizeof(s_list)); list_init(list7); list_insert_value(list7, 0, 4, 1); //list8 6x^3 s_list *list8 = (s_list *) malloc(sizeof(s_list)); list_init(list8); list_insert_value(list8, 0, 3, 6); //list8 (x^4 + 6x^3) list_append(list7, list8); //list9 (x^4 + 6x^3)y^4 s_list *list9 = (s_list *) malloc(sizeof(s_list)); list_init(list9); list_insert_value(list9, 1, 4, (int) list7); //list10 2y s_list *list10 = (s_list *) malloc(sizeof(s_list)); list_init(list10); list_insert_value(list10, 0, 1, 2); //list9 ((x^4 + 6x^3)y^4 + 2y) list_append(list9, list10); //list11 ((x^4 + 6x^3)y^4 + 2y)z s_list *list11 = (s_list *) malloc(sizeof(s_list)); list_init(list11); list_insert_value(list11, 1, 1, (int) list9); //list6 ((x^10 + 2x^6)y^3 + (3x^5)y^2)z^2 + ((x^4 + 6x^3)y^4 + 2y)z list_append(list6, list11); //list12 15 s_list *list12 = (s_list *) malloc(sizeof(s_list)); list_init(list12); list_insert_value(list12, 0, 0, 15); //list6 ((x^10 + 2x^6)y^3 + (3x^5)y^2)z^2 + ((x^4 + 6x^3)y^4 + 2y)z + 15 list_append(list6, list12); //显示多项式 list_display(list6, 'z'); printf("\n"); //求广义表list6深度 int depth = 0; list_depth(list6, &depth); printf("depth = %d \n", depth); s_list *list13 = (s_list *) malloc(sizeof(s_list)); list_copy(list13, list6); //显示多项式 list_display(list13, 'z'); printf("\n"); //求广义表list13深度 depth = 0; list_depth(list13, &depth); printf("depth = %d \n", depth); //销毁广义表,释放内存 list_destory(list6); //销毁广义表,释放内存 list_destory(list13); return 0; }