static void swap_nodes(struct heap *heap, size_t i, size_t j) { struct heap_node *old_i = heap->array[i]; struct heap_node *old_j = heap->array[j]; put_node(heap, old_j, i); put_node(heap, old_i, j); }
int main(int argc, char ** argv) { int n = 1; real m = 1.0; check_help(); extern char *poptarg; int c; const char *param_string = "m:n:"; while ((c = pgetopt(argc, argv, param_string, "$Revision: 1.9 $", _SRC_)) != -1) switch(c) { case 'm': m = atof(poptarg); break; case 'n': n = atoi(poptarg); break; case '?': params_to_usage(cerr, argv[0], param_string); get_help(); exit(1); } if (m <= 0) err_exit("mknodes: M > 0 required!"); if (n <= 0) err_exit("mknodes: N > 0 required!"); node * root = mknode_mass(n, m); root->log_history(argc, argv); put_node(root); rmtree(root); return 0; }
int main(void) { static char distances_buffer[DISTANCES_BUFFER_SIZE]; char *restrict buffer; unsigned int i_node; init_locations_coordinates_maps(); buffer = &distances_buffer[0]; i_node = 0u; while (1) { put_node(i_node, &buffer); ++i_node; if (i_node == 50u) break; *buffer = '\n'; ++buffer; } write_distances_file(&distances_buffer[0], buffer - &distances_buffer[0]); return 0; }
void pop_front() { node *n = head; head = head->next; if (!head) tail = &head; put_node(n); }
/* Removes 'node' from 'heap', without maintaining the heap invariant. * * After this call, heap_max() will no longer necessarily return the maximum * value in the heap, and HEAP_FOR_EACH will no longer necessarily iterate in * heap level order, until the next call to heap_rebuild(heap). * * This takes time O(1). */ void heap_raw_remove(struct heap *heap, struct heap_node *node) { size_t i = node->idx; if (i < heap->n) { put_node(heap, heap->array[heap->n], i); } heap->n--; }
/* Inserts 'node' into 'heap' with the specified 'priority', without * maintaining the heap invariant. * * After this call, heap_max() will no longer necessarily return the maximum * value in the heap, and HEAP_FOR_EACH will no longer necessarily iterate in * heap level order, until the next call to heap_rebuild(heap). * * This takes time O(1). */ void heap_raw_insert(struct heap *heap, struct heap_node *node, uint64_t priority) { if (heap->n >= heap->allocated) { heap->allocated = heap->n == 0 ? 1 : 2 * heap->n; heap->array = xrealloc(heap->array, (heap->allocated + 1) * sizeof *heap->array); } put_node(heap, node, ++heap->n); node->priority = priority; }
/*----------------------------------------------------------------------------- * main -- driver to create directly a single node *----------------------------------------------------------------------------- */ main(int argc, char ** argv) { bool c_flag = FALSE; bool i_flag = FALSE; real m = 1; // default mass: unity char *comment; check_help(); extern char *poptarg; int c; const char *param_string = "c:im:"; while ((c = pgetopt(argc, argv, param_string, "$Revision: 1.7 $", _SRC_)) != -1) switch(c) { case 'c': c_flag = TRUE; comment = poptarg; break; case 'i': i_flag = TRUE; break; case 'm': m = atof(poptarg); break; case '?': params_to_usage(cerr, argv[0], param_string); get_help(); exit(1); } node * root; root = new node(); root->set_root(root); root->set_mass(m); if (c_flag == TRUE) root->log_comment(comment); root->log_history(argc, argv); if (i_flag) root->set_label(1); put_node(root); rmtree(root); }
void pq_insert(struct priority_queue *p, unsigned int new_elem) { if(new_elem < MAXKEY){ struct node *new_node; if(nodes_array[new_elem] == NULL){ new_node = get_node(); nodes_array[new_elem] = new_node; } else{ struct l_node *temp; new_node = (struct node *) malloc(sizeof(struct node)); put_node(new_node); } new_node->parent = NULL; new_node->child = NULL; new_node->degree = 0; new_node->key = new_elem; new_node->tag = 0; new_node->left = new_node; new_node->right = new_node; if(p->n_nodes == 0){ p->root_list = new_node; p->n_nodes = 1; } else{ new_node->left = p->root_list; new_node->right = p->root_list->right; p->root_list->right = new_node; new_node->right->left = new_node; if((p->root_list->key) > (new_node->key)) p->root_list = new_node; p->n_nodes+=1; } } #ifdef DEBUG else printf("Key %d can not be inserted. Maximum allowable key is %d\n",new_elem,MAXKEY); #endif }
//-------------核心代码--------------// int solve() { int i, j; queue[0].ca=Ja.val; queue[0].cb=Jb.val; queue[0].parent=-1; for (i = 0; i<tail; i++){ if (succeed(i)){ //找到目标状态 show(i); //递归打印出达到目标状态的操作组序列 return 1; } for (j = 0; j<6; j++){ Ja.val=queue[i].ca; Jb.val=queue[i].cb;//将油罐油量设置为节点i的状态 if (action[j]() && !exist(i)) {//执行j判断并检查是否重复 ans[i].parent=i; ans[i].ca=Ja.val; ans[i].cb=Jb.val; ans[i].act=name[j]; put_node(ans[i]); //j操作后的状态加入队列中 } } } return 0; }
unsigned int pq_extract(struct priority_queue *p){ if(p->root_list != NULL){ int temp; struct node *tempptr,*t1,*t2; temp = p->root_list->key; if((p->root_list->child == NULL) && (p->n_nodes == 1)){ put_node(p->root_list); p->n_nodes = 0; p->root_list = NULL; } else if ((p->root_list->child == NULL) && (p->n_nodes > 1)){ p->root_list->left->right = p->root_list->right; p->root_list->right->left = p->root_list->left; tempptr = p->root_list; p->root_list = p->root_list->left; put_node(tempptr); p->n_nodes -= 1; if(p->root_list->key != 3) consolidate(p); } else if(p->root_list->child != NULL){ t1 = p->root_list->child; while(t1 != t1->right ){ t2=t1->right; t1->right = t2->right; t2->right->left = t1; t2->right= p->root_list->right; t2->left = p->root_list; t2->parent=NULL; t2->tag = 0; p->root_list->right->left = t2; p->root_list->right = t2; p->n_nodes += 1; } t1->right= p->root_list->right; t1->left = p->root_list; t1->parent = 0; t1->tag = 0; p->root_list->right->left = t1; p->root_list->right = t1; p->n_nodes += 1; p->root_list->degree = 0; t2 = p->root_list; p->root_list->left->right = p->root_list->right; p->root_list->right->left = p->root_list->left; p->root_list = p->root_list->right; put_node(t2); p->n_nodes -=1; consolidate(p); } return temp; } #ifdef DEBUG else{ printf("priority_queue is Empty.\n"); return -1; } #endif }
main(int argc, char ** argv) { char *comment; bool c_flag = FALSE; // if TRUE, a comment given on command line real t_extract; bool t_flag = FALSE; // if TRUE, a time was specified bool v_flag = FALSE; // if TRUE, print snap times as read int n = 1; bool n_flag = false; // if TRUE, a number was specified check_help(); extern char *poptarg; int c; const char *param_string = "c:n:t:v"; while ((c = pgetopt(argc, argv, param_string, "$Revision: 1.8 $", _SRC_)) != -1) switch(c) { case 'c': c_flag = TRUE; comment = poptarg; break; case 't': t_flag = TRUE; t_extract = atof(poptarg); break; case 'n': n_flag = TRUE; n = atoi(poptarg); break; case 'v': v_flag = TRUE; break; case '?': params_to_usage(cerr, argv[0], param_string); get_help(); exit(1); } if (n < 0) err_exit("n < 0 specified."); if (n > 1 && !t_flag) { warning("n > 1 but no time specified -- 0 assumed."); t_extract = 0; t_flag = true; } node *b = NULL, *bp = NULL; int i = 0; // Need to ensure that the data are not changed by this function. // Currently, get_dyn() ends by calling check_and_correct_node(), // which may change pos and vel to force the system to the center // of mass frame. Can fix (1) by suppressing this call in this // case, (2) setting a tolerance to avoid forcing if the system is // already "close" to the CM frame, or (3) use nodes here, which // will never interpret pos or vel and hence will not change them. // The only number we really need below is sytsem_time. Options // (1) and (2) are awkward, because this function is a member // function and changing its arguments would propagate through the // other classes. However, we could add a static local option to // the dyn instance of the function. Option (3) seems cleanest. // To revert, change node to dyn and use b->get_system_time() to // determine time. // (Steve, 7/04) while (b = get_node()) { real time; if (find_qmatch(b->get_dyn_story(), "real_system_time")) time = getrq(b->get_dyn_story(), "real_system_time"); else time = getrq(b->get_dyn_story(), "system_time"); // real time = b->get_system_time(); i++; if (v_flag) cerr << "Snap time #" << i << " = " << time << endl; if (t_flag && time >= t_extract) { if (n > 0) { if (c_flag == TRUE) b->log_comment(comment); b->log_history(argc, argv); put_node(b); } if (--n <= 0) exit(0); } if (bp) rmtree(bp); bp = b; } if (n > 0 && !t_flag) { bp->log_history(argc, argv); put_node(bp); } }
char *tx_parse(char *data, size_t count, struct tx_node *nodes) { char *marks[3] = { data }; char c; struct tx_node *parent = nodes; put_node(TX_EOF, NULL, NULL, NULL); parse_text: { while(*data && *data != '<') ++data; c = *data; if(data > marks[0]) { put_node(TX_TEXT, parent, "#text", marks[0]); *data = 0; if(!parent->value) parent->value = marks[0]; } if(c == '<') call(parse_element); put_node(TX_EOF, NULL, NULL, NULL); return NULL; } parse_element: { if(*data == '/') call(parse_closing_element); while(isspace(*data)) ++data; if(isalnum(*data)) { marks[0] = data; call(parse_element_name); } return data; } parse_closing_element: { char *start; while(isspace(*data)) ++data; start = data; while(isalnum(*data)) ++data; while(isspace(*data)) ++data; if(*data != '>') return data; *data = 0; if(!parent->name || strcmp(parent->name, start)) return start; parent = parent->parent; marks[0] = data + 1; call(parse_text); } parse_element_name: { char c ; while(isalnum(*data)) ++data; put_node(TX_ELEMENT, parent, marks[0], NULL); parent = nodes - 1; c = *data; *data = 0; if(c == '>') { marks[0] = data + 1; call(parse_text); } else if(c == '/') { if(*++data == '>') { parent = parent->parent; marks[0] = data + 1; call(parse_text); } return data; } else if(isspace(c)) { call(parse_attributes); } return data; } parse_attributes: { while(isspace(*data)) ++data; if(isalnum(*data)) { marks[0] = data; call(parse_attribute_name); } else if(*data == '>') { marks[0] = data + 1; call(parse_text); } else if(*data == '/') { if(*++data == '>') { parent = parent->parent; marks[0] = data + 1; call(parse_text); } return data; } return data; } parse_attribute_name: { while(isalnum(*data)) ++data; marks[1] = data; while(isspace(*data)) ++data; if(*data == '=') call(parse_attribute_value); return data; } parse_attribute_value: { char c; while(isspace(*data)) ++data; marks[2] = data + 1; c = *data; if(c != '"' && c != '\'') return data; ++data; for(; *data; ++data) { if(*data == '\\') { if(*++data == 0) return data; } else if(*data == c) { put_node(TX_ATTRIBUTE, parent, marks[0], marks[2]); *marks[1] = 0; *data = 0; call(parse_attributes); } } return data; } }