int main(){ int n=50; int d=10000000; int r; //tests the sorting by putting a bunch of random numbers into the list //and looking too see whether they're sorted //dividing by d is just so that you can easily read the numbers sortedlist* list=sortedlist_init(sizeof(int),&intcmp); for(int i=0;i<n;++i){ r=rand(); //r=n-i; //printf("%d ",r); sortedlist_add(list,&r); } printf("front is %d\n",*(int*)sortedlist_get_min(list)); free(sortedlist_rm_max(list)); free(sortedlist_rm_min(list)); iterator* iter=sortedlist_iterator(list); while(sortedlist_iteratorhasnext(iter)){ printf("%d ",*(int*)sortedlist_iteratornext(iter)); } printf("\n"); sortedlist_freeiter(iter); sortedlist_free(list); }
/* * Constructs the huffmantree from the file. Rewind after using the function. * Takes in a pointer to a FILE. * Returns a pointer to a huffmantree. */ huffmantree* frequency_tree(FILE * stream){ int size=(int)pow(2,CHAR_BIT)+1; int array[size]; int i; unsigned int c; sortedlist* list=sortedlist_init(sizeof(huffmantree*), cmp); huffmantree* min1; huffmantree* min2; huffmantree* temp; huffmantree* max; for (i=0;i<size;++i){ array[i]=0; } while((c=fgetc(stream))!=EOF){ array[c]=array[c]+1; } array[size-1]=1; //EOF for (i=0;i<size;i++){ if (array[i]){ huffmantree* tree=huffmantree_init(); tree->c=i; tree->count=array[i]; sortedlist_add(list,&tree); } } while (sortedlist_size(list)>1){ huffmantree** m1=(huffmantree**)sortedlist_rm_min(list); huffmantree** m2=(huffmantree**)sortedlist_rm_min(list); min1=*m1; min2=*m2; temp=huffmantree_init(); temp->left=min1; temp->right=min2; temp->count=min1->count+min2->count; temp->size=min1->size+min2->size+1; sortedlist_add(list, &temp); free(m1); free(m2); } huffmantree** m=(huffmantree**)sortedlist_rm_max(list); max=*m; sortedlist_free(list); free(m); return max; }
nukelog_t * _nukelog_add_release(sortedlist_t *t, glnukelog_t *g) { nukelog_t *tmp = malloc(sizeof(nukelog_t)); sortedlist_init(&tmp->nukees); tmp->nuketime = g->nuketime; tmp->mult = g->mult; tmp->dirname = strdup(g->dirname); tmp->status = g->status; tmp->nuker = (g->status == 0)?strdup(g->nuker):strdup(g->unnuker); tmp->reason = malloc(65); strncpy(tmp->reason, g->reason, 60); tmp->bytes = 0; sortedlist_add(t, tmp); return tmp; }
void print_sorted(nuke_top_t *n, int max, char type) { sortedlist_t sorter; nuke_top_t *tmp; int i; sortedlist_init(&sorter); for (tmp = n; tmp; tmp = tmp->next) sortedlist_add(&sorter, tmp); switch (type) { case 'n': sortedlist_sort(&sorter, nuke_top_sort_num); break; case 'm': sortedlist_sort(&sorter, nuke_top_sort_mul); break; case 'c': sortedlist_sort(&sorter, nuke_top_sort_cred); break; default: sortedlist_sort(&sorter, nuke_top_sort_bytes); break; } sortedlist_reset(&sorter); i = 0; while (sortedlist_hasnext(&sorter) && (i < max)) { nuke_top_t *tmp = (nuke_top_t*)sortedlist_next(&sorter); printf(" %3d. | %-12.12s | %5d | %9.1fM | x%4.2f | %9.1fM\n", i+1, tmp->user, tmp->nuke_num, tmp->nuke_bytes_sum, (float)tmp->nuke_mul_sum/tmp->nuke_num, tmp->nuke_bytes_cred); i++; } }