node* join_trees(node* n1, node* n2) { node* lowest_n; node* highest_n; if (LE(n1->key, n2->key)) { lowest_n = n1; highest_n = n2; } else { lowest_n = n2; highest_n = n1; } if (lowest_n->child == NULL) { lowest_n->child = highest_n; highest_n->parent = lowest_n; } else { concat_list(lowest_n->child, highest_n); node* temp_ref = highest_n; do { temp_ref->parent = lowest_n; temp_ref = temp_ref->right_sibling; } while (temp_ref != highest_n); } lowest_n->rank = lowest_n->rank + 1; highest_n->marked = 0; return lowest_n; }
void decrease_key (int delta, item* i, heap* h) { node* n = i->n; i->key = i->key - delta; n->key = i->key; if (n->parent != NULL) { // first clean up one level up if (n->right_sibling == n) { // only one in the list n->parent->child = NULL; n->parent->rank = 0; } else { n->parent->child = n->right_sibling; //make sure the parents child does not point to us n->parent->rank = n->parent->rank - 1; remove_node_in_list(n); } update_marked(n->parent, h); n->parent = NULL; // move the node up to the root concat_list(h->min_node, n); } if (GT(h->min_node->key, n->key)) { h->min_node = n; } }
int main (void) { node* init=NULL; node* a[16]; init=create_node("",""); a[0]=create_node("aaa","あああ"); a[1]=create_node("bbb","いいい"); concat_list(a[0],init); concat_list(a[1],init); printf("%-16s\t%-8s\t%-8s\t%-16s\n", "アドレス","eng","jpn","next"); print_list(init); close_list(init); return 0; }
void concat_list(node* p, node* list) { if(list->next){ concat_list(p,list->next); }else{ list->next=p; p->next=NULL; } return; }
void update_marked(node* n, heap* h) { if (n->parent == NULL) { return; } else { if (n->marked == 1) { update_marked(n->parent, h); if (n->right_sibling == n) { n->parent->child = NULL; } else { n->parent->child = n->right_sibling; } n->parent = NULL; remove_node_in_list(n); concat_list(h->min_node, n); } else { n->marked = 1; } } }
heap* meld (heap* h1, heap* h2) { if (h1->min_node == NULL) { *h1 = *h2; return h1; } if (h2->min_node == NULL) {return h1; } node* h1_min_node = h1->min_node; node* h2_min_node = h2->min_node; concat_list(h1_min_node, h2_min_node); if (GT(h1_min_node->key, h2_min_node->key)) { h1->min_node = h2->min_node; } h1->rank = h1->rank + h2->rank; free(h2); return h1; }
int main(void) { link *head, *odd = NULL, *even = NULL; head = new_list(); print_list(head); while (head != NULL) { odd = add_next(head->item,odd); head = head ->next; even = add_next(head->item, even); head = head->next; } head =concat_list(odd, even); print_list(head); return 0; }
void remove_item(item* i, heap* h) { node* n = i->n; if (h->min_node == n) { delete_min(h); } else { if (n->parent != NULL) { update_marked(n->parent, h); if (n->right_sibling == n) { n->parent->child = NULL; } else { n->parent->child = n->right_sibling; } n->parent->rank = n->parent->rank - 1; } remove_node_in_list(n); if (n->child != NULL) { // move the node up to the root concat_list(h->min_node, n->child); // should this be a constant operation? node* last_ref = n->child; do { last_ref->parent = NULL; last_ref = last_ref->right_sibling; } while (last_ref != n->child); } } }
/** * Code adapted from component.cpp, dataitems are unpacked in the same * order as the Component::unpack( void* ) method to maintain consistency. */ struct component * unpack_component( void *buffer ) { struct component *ret = NULL; u32 size = 0, netOrder; char *packed = (char*)buffer; char * next; struct list *item; struct dataitem *data; if( !buffer ) return ret; ret = new_component( 0 ); if( !ret ) return ret; memcpy( &netOrder, packed, sizeof( u32 ) ); size = ntohl( netOrder ); next = packed + sizeof( u32 ); ret->id = unpack_dataitem( next ); if( !ret->id ) goto unpackerr; next += calc_packed_length_dataitem( ret->id ); ret->deviceTreeNode = unpack_dataitem( next ); if( !ret->deviceTreeNode ) goto unpackerr; next += calc_packed_length_dataitem( ret->deviceTreeNode ); ret->sysFsNode = unpack_dataitem( next ); if( !ret->sysFsNode ) goto unpackerr; next += calc_packed_length_dataitem( ret->sysFsNode ); ret->sysFsLinkTarget = unpack_dataitem( next ); if( !ret->sysFsLinkTarget ) goto unpackerr; next += calc_packed_length_dataitem( ret->sysFsLinkTarget ); ret->halUDI = unpack_dataitem( next ); if( !ret->halUDI ) goto unpackerr; next += calc_packed_length_dataitem( ret->halUDI ); ret->netAddr = unpack_dataitem( next ); if( !ret->netAddr ) goto unpackerr; next += calc_packed_length_dataitem( ret->netAddr ); ret->devClass = unpack_dataitem( next ); if( !ret->devClass ) goto unpackerr; next += calc_packed_length_dataitem( ret->devClass ); ret->description = unpack_dataitem( next ); if( !ret->description ) goto unpackerr; next += calc_packed_length_dataitem( ret->description ); ret->cdField = unpack_dataitem( next ); if( !ret->cdField ) goto unpackerr; next += calc_packed_length_dataitem( ret->cdField ); ret->serialNumber = unpack_dataitem( next ); if( !ret->serialNumber ) goto unpackerr; next += calc_packed_length_dataitem( ret->serialNumber ); ret->partNumber = unpack_dataitem( next ); if( !ret->partNumber ) goto unpackerr; next += calc_packed_length_dataitem( ret->partNumber ); ret->firmwareLevel = unpack_dataitem( next ); if( !ret->firmwareLevel ) goto unpackerr; next += calc_packed_length_dataitem( ret->firmwareLevel ); ret->firmwareVersion = unpack_dataitem( next ); if( !ret->firmwareVersion ) goto unpackerr; next += calc_packed_length_dataitem( ret->firmwareVersion ); ret->fru = unpack_dataitem( next ); if( !ret->fru ) goto unpackerr; next += calc_packed_length_dataitem( ret->fru ); ret->manufacturer = unpack_dataitem( next ); if( !ret->manufacturer ) goto unpackerr; next += calc_packed_length_dataitem( ret->manufacturer ); ret->model = unpack_dataitem( next ); if( !ret->model ) goto unpackerr; next += calc_packed_length_dataitem( ret->model ); ret->manufacturerID = unpack_dataitem( next ); if( !ret->manufacturerID ) goto unpackerr; next += calc_packed_length_dataitem( ret->manufacturerID ); ret->engChangeLevel = unpack_dataitem( next ); if( !ret->engChangeLevel ) goto unpackerr; next += calc_packed_length_dataitem( ret->engChangeLevel ); ret->parent = unpack_dataitem( next ); if( !ret->parent ) goto unpackerr; next += calc_packed_length_dataitem( ret->parent ); ret->devSubSystem = unpack_dataitem( next ); if( !ret->devSubSystem ) goto unpackerr; next += calc_packed_length_dataitem( ret->devSubSystem ); ret->devDriver = unpack_dataitem( next ); if( !ret->devDriver ) goto unpackerr; next += calc_packed_length_dataitem( ret->devDriver ); ret->devKernel = unpack_dataitem( next ); if( !ret->devKernel ) goto unpackerr; next += calc_packed_length_dataitem( ret->devKernel ); ret->devKernelNumber = unpack_dataitem( next ); if( !ret->devKernelNumber ) goto unpackerr; next += calc_packed_length_dataitem( ret->devKernelNumber ); ret->devSysName = unpack_dataitem( next ); if( !ret->devSysName ) goto unpackerr; next += calc_packed_length_dataitem( ret->devSysName ); ret->devDevTreeName = unpack_dataitem( next ); if( !ret->devDevTreeName ) goto unpackerr; next += calc_packed_length_dataitem( ret->devDevTreeName ); ret->devBus = unpack_dataitem( next ); if( !ret->devBus ) goto unpackerr; next += calc_packed_length_dataitem( ret->devBus ); ret->devBusAddr = unpack_dataitem( next ); if( !ret->devBusAddr ) goto unpackerr; next += calc_packed_length_dataitem( ret->devBusAddr ); ret->recordType = unpack_dataitem( next ); if( !ret->recordType ) goto unpackerr; next += calc_packed_length_dataitem( ret->recordType ); ret->scsiDetail = unpack_dataitem( next ); if( !ret->scsiDetail ) goto unpackerr; next += calc_packed_length_dataitem( ret->scsiDetail ); ret->plantMfg = unpack_dataitem( next ); if( !ret->plantMfg ) goto unpackerr; next += calc_packed_length_dataitem( ret->plantMfg ); ret->featureCode = unpack_dataitem( next ); if( !ret->featureCode ) goto unpackerr; next += calc_packed_length_dataitem( ret->featureCode ); ret->keywordVersion = unpack_dataitem( next ); if( !ret->keywordVersion ) goto unpackerr; next += calc_packed_length_dataitem( ret->keywordVersion ); ret->microCodeImage = unpack_dataitem( next ); if( !ret->microCodeImage ) goto unpackerr; next += calc_packed_length_dataitem( ret->microCodeImage ); ret->secondLocation = unpack_dataitem( next ); if( !ret->secondLocation ) goto unpackerr; next += calc_packed_length_dataitem( ret->secondLocation ); ret->physicalLocation = unpack_dataitem( next ); if( !ret->physicalLocation ) goto unpackerr; next += calc_packed_length_dataitem( ret->physicalLocation ); while( 0 == strncmp( next, CHILD_START, strlen( CHILD_START ) ) ) { next++; if( next > packed + size ) goto unpackerr; } if( strncmp( next, CHILD_START, strlen( CHILD_START ) ) == 0 ) { next += strlen( CHILD_START ) + 1; if( next > packed + size ) goto unpackerr; while( strncmp( next, CHILD_END, strlen( CHILD_END ) ) != 0 ) { item = new_list( ); if( !next ) goto unpackerr; item->data = strdup( next ); if (item->data == NULL) goto unpackerr; next += strlen( (char*)item->data ) + 1; if( !ret->childrenIDs ) ret->childrenIDs = item; else concat_list( ret->childrenIDs, item ); if( next > packed + size ) goto unpackerr; } next += strlen( CHILD_END ) + 1; } if( strncmp( next, DEVICE_START, strlen( DEVICE_START ) ) == 0 ) { next += strlen( DEVICE_START ) + 1; if( next > packed + size ) goto unpackerr; while( strncmp( next, DEVICE_END, strlen( DEVICE_END ) ) != 0 ) { data = unpack_dataitem( next ); if( !data ) goto unpackerr; next += calc_packed_length_dataitem( data ); if( !ret->deviceSpecific ) ret->deviceSpecific = data; else add_dataitem( ret->deviceSpecific, data ); if( next > packed + size ) goto unpackerr; } next += strlen( DEVICE_END ) + 1; } if( strncmp( next, USER_START, strlen( USER_START ) ) == 0 ) { next += strlen( USER_START ) + 1; if( next > packed + size ) goto unpackerr; while( strncmp( next, USER_END, strlen( USER_END ) ) != 0 ) { data = unpack_dataitem( next ); if( !data ) goto unpackerr; next += calc_packed_length_dataitem( data ); if( !ret->userData ) ret->userData = data; else add_dataitem( ret->userData, data ); if( next > packed + size ) goto unpackerr; } next += strlen( USER_END ) + 1; } if( strncmp( next, AX_START, strlen( AX_START ) ) == 0 ) { next += strlen( AX_START ) + 1; if( next > packed + size ) goto unpackerr; while( strncmp( next, AX_END, strlen( AX_END ) ) != 0 ) { data = unpack_dataitem( next ); if( !data ) goto unpackerr; next += calc_packed_length_dataitem( data ); if( !ret->aixNames ) ret->aixNames = data; else add_dataitem( ret->aixNames, data ); if( next > packed + size ) goto unpackerr; } } return ret; unpackerr: free_component( ret ); return NULL; }
/* * Draw the file directory. * * howmany - How many files can be selected * 0 = none (for directory selection only, as in "rz") * 1 = one (for single-file up-/down-loads, as in "rx") * -1 = any number (for multiple files, as in "sz") * * downloading - Is this for download selection? * 0 = no * 1 = yes - when single file selected, see if it exists */ char * filedir(int howmany, int downloading) { time_t click_time = (time_t) 0; size_t i; how_many = howmany; down_loading = downloading; init_filedir(); again: mc_wlocate(main_w, 0, cur + FILE_MWTR - top); if (first) { mc_wredraw(main_w, 1); first = 0; } while (!quit) { GETSDIR_ENTRY *d = getno(cur, global_dirdat); /* if(S_ISDIR(d->mode)) prone(main_w, d, longest, 0); */ switch (c = wxgetch()) { case K_UP: case 'k': /* if(S_ISDIR(d->mode)) prone(main_w, d, longest, 1); */ cur -= cur > 0; break; case K_DN: case 'j': /* if(S_ISDIR(d->mode)) prone(main_w, d, longest, 1); */ cur += cur < nrents - 1; break; case K_LT: case 'h': subm--; if (subm < 0) subm = SUBM_OKAY; break; case K_RT: case 'l': subm = (subm + 1) % 6; break; case K_PGUP: case '\002': /* Control-B */ pgud = 1; quit = 1; break; case K_PGDN: case '\006': /* Control-F */ pgud = 2; quit = 1; break; case ' ': /* Tag if not directory */ if (S_ISDIR(d->mode)) { time_t this_time = time((time_t *)NULL); if (this_time - click_time < 2) { GETSDIR_ENTRY *d2 = getno(cur, global_dirdat); goto_filedir(d2->fname, 0); click_time = (time_t)0; } else click_time = this_time; } else { if (how_many) { if ((d->cflags ^= FL_TAG) & FL_TAG) { if (tag_cnt && how_many == 1) { d->cflags &= ~FL_TAG; file_tell(_("Can select only one!")); break; } ++tag_cnt; } else --tag_cnt; mc_wlocate(main_w, 0, cur + FILE_MWTR - top); prone(main_w, d, longest, d->cflags & FL_TAG); mc_wputc(main_w, '\n'); cur += cur < nrents - 1; } } break; case '\033': case '\r': case '\n': quit = 1; break; default: for (i = 0; i < WHAT_NR_OPTIONS; i++) { if (strchr (_(what[i]), toupper (c)) != NULL) { subm = i; c = '\n'; quit = 1; break; } } break; } if (c != ' ') click_time = (time_t)0; if (cur < top) { top--; prdir(main_w, top, top, global_dirdat, longest); } if (cur - top > main_w->ys - (2 + FILE_MWTR)) { top++; prdir(main_w, top, top, global_dirdat, longest); } /* if(cur != ocur) mc_wlocate(main_w, 0, cur + FILE_MWTR - top); */ ocur = cur; dhili(subm); /* this really needs to go in dhili !!!*/ mc_wlocate(main_w, 0, cur + FILE_MWTR - top); } quit = 0; /* ESC means quit */ if (c == '\033') { mc_wclose(main_w, 1); mc_wclose(dsub, 1); free(global_dirdat); global_dirdat = NULL; return NULL; } /* Page up or down ? */ if (pgud == 1) { /* Page up */ ocur = top; top -= main_w->ys - (1 + FILE_MWTR); if (top < 0) top = 0; cur = top; pgud = 0; if (ocur != top) prdir(main_w, top, cur, global_dirdat, longest); ocur = cur; goto again; } if (pgud == 2) { /* Page down */ ocur = top; if (top < nrents - main_w->ys + (1 + FILE_MWTR)) { top += main_w->ys - (1 + FILE_MWTR); if (top > nrents - main_w->ys + (1 + FILE_MWTR)) { top = nrents - main_w->ys + (1 + FILE_MWTR); } cur = top; } else cur = nrents - 1; pgud = 0; if (ocur != top) prdir(main_w, top, cur, global_dirdat, longest); ocur = cur; goto again; } if (c =='\r' || c == '\n') { switch(subm) { case 0: /* Goto directory */ { char buf[128]; char *s; strncpy(buf, down_loading? P_DOWNDIR : P_UPDIR, sizeof(buf) -1); s = input(_("Goto directory:"), buf); /* if(s == NULL || *s == (char) 0) */ if (s == NULL) break; goto_filedir(buf, 1); } break; case 1: /* Previous directory */ goto_filedir(prev_dir, 1); break; case 2: /* File (wildcard) spec */ { char *s = input(_("Filename pattern:"), wc_mem); if (s == NULL || *s == (char) 0) break; strcpy(wc_str, wc_mem); new_filedir(global_dirdat, 1); wc_str[0] = (char)0; } break; case 3: /* Tag */ if (how_many == 1) file_tell(_("Can select only one!")); else if (how_many == -1) { char tag_buf[128]; char *s; strncpy(tag_buf, wc_mem, 128); s = input(_("Tag pattern:"), tag_buf); if (s != NULL && *s != (char)0) { int newly_tagged; if ((newly_tagged = tag_untag(tag_buf, 1)) == 0) { file_tell(_("No file(s) tagged")); goto tag_end; } tag_cnt += newly_tagged; prdir(main_w, top, top, global_dirdat, longest); } } tag_end: break; case 4: /* Untag */ { char tag_buf[128]; char *s; int untagged; strncpy(tag_buf, wc_mem, 128); s = input(_("Untag pattern:"), tag_buf); if (s == NULL || *s == (char)0) goto untag_end; if ((untagged = tag_untag(tag_buf, 0)) == 0) { file_tell(_("No file(s) untagged")); goto untag_end; } tag_cnt -= untagged; prdir(main_w, top, top, global_dirdat, longest); } untag_end: break; case 5: { /* Done */ char *ret_ptr = NULL; /* failsafe: assume failure */ if (how_many != 0 && !tag_cnt) { while (1) { s = input(_("No file selected - enter filename:"), ret_buf); if (s != NULL && *s != (char) 0) { int f_exist = access(ret_buf, F_OK); if (down_loading) { if (f_exist != -1) { /* ask 'em if they're *sure* */ char buf[BUFSIZ]; snprintf(buf, sizeof(buf), _("File: \"%s\" exists! Overwrite?"), ret_buf); if (ask(buf, d_yesno) == 0) { ret_ptr = ret_buf; break; } } else { ret_ptr = ret_buf; break; } } else { if (f_exist == -1) file_tell(_("no such file!")); else { ret_ptr = ret_buf; break; } } } else { /* maybe want to ask: "abort?", here */ goto again; } } } else { /* put 'em in a buffer for return */ if (how_many == 0) { /* current working directory */ ret_ptr = work_dir; } else { ret_ptr = concat_list(global_dirdat); } } mc_wclose(main_w, 1); mc_wclose(dsub, 1); free(global_dirdat); global_dirdat = NULL; return ret_ptr; } break; default: /* should "beep", I guess (? shouldn't get here) */ file_tell("BEEP!"); break; } /* switch */ } goto again; }
item* delete_min(heap* h) { node* min_node = h->min_node; node* list_to_concat = NULL; if (min_node != NULL) { if (min_node->left_sibling == min_node) { // only one root h->min_node = NULL; list_to_concat = min_node->child; } else { // save an arbitrary reference to the new list list_to_concat = min_node->left_sibling; // remove min root from forest remove_node_in_list(min_node); if (min_node->child != NULL) { // If we have to append childs from the min root concat_list(list_to_concat, min_node->child); } } } h->rank = h->rank - 1; if (list_to_concat != NULL) { int mr = max_rank(h); node* ranks[mr]; //calloc? for (int i = 0; i < mr; i++) { ranks[i] = NULL; } node* last_ref = list_to_concat; //which is actually just a pointer to an item do { node* next_ref = last_ref->right_sibling; last_ref->parent = NULL; // remember to set parent 0 - all is root nodes last_ref->left_sibling = last_ref; last_ref->right_sibling = last_ref; node* this_ref = last_ref; node* existing_tree = ranks[this_ref->rank]; // a while is necessary if we join trees while (existing_tree != NULL) { ranks[this_ref->rank] = NULL; this_ref = join_trees(existing_tree, this_ref); existing_tree = ranks[this_ref->rank]; } ranks[this_ref->rank] = this_ref; last_ref = next_ref; } while (last_ref != list_to_concat); // this way, we go through the circular list, until we reach the starting point node* new_min_node = NULL; // set an arbitrary one for (int i = 0; i < mr; i++) { node* curr_tree = ranks[i]; if (new_min_node == NULL) { new_min_node = curr_tree; } else if (curr_tree != NULL) { concat_list(new_min_node, curr_tree); if (GT(new_min_node->key, curr_tree->key)) { new_min_node = curr_tree; } } } h->min_node = new_min_node; } if (min_node != NULL) { item* val = min_node->item; free(min_node); return val; } else { return NULL; } }