/* Allocate and initialize dynamic array. param: cap desired capacity for the dyn array pre: none post: none ret: a non-null pointer to a dynArr of cap capacity and 0 elements in it. */ DynArr* newDynArr(int cap) { DynArr *r = (DynArr *)malloc(sizeof( DynArr)); assert(r != 0); initDynArr(r,cap); return r; }
/* Allocate and initialize dynamic array. param: cap desired capacity for the dyn array pre: none post: none ret: a non-null pointer to a dynArr of cap capacity and 0 elements in it. */ DynArr* createDynArr(int cap) { assert(cap > 0); DynArr *r = (DynArr *)malloc(sizeof( DynArr)); assert(r != 0); initDynArr(r,cap); return r; }
/* Allocate and initialize dynamic array. param: cap desired capacity for the dyn array pre: none post: none ret: a non-null pointer to a dynArr of cap capacity and 0 elements in it. */ struct DynArr* createDynArr(int cap) { struct DynArr *r; assert(cap > 0); r = (struct DynArr *)malloc(sizeof(struct DynArr)); assert(r != 0); initDynArr(r,cap); return r; }
/* Copy elements from a dynamic array to another dynamic array param: source pointer to the source dynamic array param: destination pointer to the destination dynamic array pre: s is not null and s is not empty post: destination is initialized post: the elements from source are copied to destination */ void copyDynArr(DynArr *source, DynArr *destination) { int i; assert(source->size > 0); initDynArr(destination, source->capacity); /* copy elements to destination array */ for(i = 0; i < source->size; i++) destination->data[i] = source->data[i]; destination->size = source->size; }
/* Resizes the underlying array to be the size cap param: v pointer to the dynamic array param: cap the new desired capacity pre: v is not null post: v has capacity newCap */ void _dynArrSetCapacity(DynArr *v, int newCap) { /* FIXME: You will write this function */ TYPE* temp = v->data; /*create temp var to store pointer*/ int tempSize = v->size; int i; initDynArr(v, newCap); for (i = 0; i < tempSize; i++) { v->data[i] = temp[i]; /*copy values to new arr*/ } v->size = tempSize; /*copy size to new arr*/ free(temp); /*free old arr*/ }
int main (int argc, const char * argv[]) { TYPE task1, task2, task3, task4, task5, task6, task7, task8, task9, task10; DynArr mainList; int i; initDynArr(&mainList, 10); /* create tasks */ task1 = createTask(9, "task 1"); task2 = createTask(3, "task 2"); task3 = createTask(2, "task 3"); task4 = createTask(4, "task 4"); task5 = createTask(5, "task 5"); task6 = createTask(7, "task 6"); task7 = createTask(8, "task 7"); task8 = createTask(6, "task 8"); task9 = createTask(1, "task 9"); task10 = createTask(0, "task 10"); /* add tasks to the dynamic array */ addDynArr(&mainList, task1); addDynArr(&mainList, task2); addDynArr(&mainList, task3); addDynArr(&mainList, task4); addDynArr(&mainList, task5); addDynArr(&mainList, task6); addDynArr(&mainList, task7); addDynArr(&mainList, task8); addDynArr(&mainList, task9); addDynArr(&mainList, task10); /* sort tasks */ sortHeap(&mainList); /* print sorted tasks from the dynamic array */ for (i = 0; i < mainList.size; i++) { printf("%d\n", mainList.data[i].priority); } printList(&mainList); return 0; }
int main() { struct task task1, task2, task3, task4, task5, task6, task7, task8, task9, task10; struct DynArr taskList; initDynArr(&taskList, 10); /* create tasks */ task1 = createTask(10, "Clean house"); task2 = createTask(6, "Eat dinner"); task3 = createTask(2, "Do laundry"); task4 = createTask(4, "Do dishes"); task5 = createTask(5, "Grocery shopping"); task6 = createTask(7, "Change oil"); task7 = createTask(13, "Do taxes"); task8 = createTask(8, "Sleep"); task9 = createTask(1, "Do other homework"); task10 = createTask(0, "Finish heap homework"); /* add tasks to the dynamic array */ addHeap(&taskList, task1); addHeap(&taskList, task2); addHeap(&taskList, task3); addHeap(&taskList, task4); addHeap(&taskList, task5); addHeap(&taskList, task6); addHeap(&taskList, task7); addHeap(&taskList, task8); addHeap(&taskList, task9); addHeap(&taskList, task10); printf("Beginning testing... \n"); /* Should print: heap size: 10 */ printf("heap size: %d \n", sizeDynArr(&taskList)); /* Should print - Priority: 0 - Description: Finish heap homework */ printTask(getMinHeap(&taskList)); /* Remove top node from head */ removeMinHeap(&taskList); /* Should print - Priority: 1 - Do other homework */ printTask(getMinHeap(&taskList)); int i; printf("printing heap... \n"); /* Should print - 1 2 6 4 5 7 13 10 8 9 */ for(i = 0; i <sizeDynArr(&taskList);i++) { printf("%d ", getDynArr(&taskList, i).priority); } printf("\n"); /* Sort heap */ sortHeap(&taskList); printf("printing sorted heap... \n"); /* Should print (without descriptions) : 1, 2, 4, 5, 6, 7, 8, 10, 13 */ /*Not sure how to both print "Priority: 1 - Do Other homework" like you asked above and in this order ascending, using the same function. Doesn't really make sense.*/ for (i = 0; i < taskList.size; i++) { printTask(getDynArr(&taskList, i)); } printf("\n"); return 0; }
int main (int argc, const char * argv[]) { const long size = atoi(argv[1]); TYPE *task = (TYPE*)malloc(sizeof(TYPE)*size*2); int *flip = (int*)malloc(sizeof(int)*size*2); DynArr mainList; long i; struct skipList skippy; printf("Enter\n"); initDynArr(&mainList, 10000000); /*Avoid effects of resizing*/ initSkipList(&skippy); /* create tasks */ for(i=0;i<size*2; i++) { task[i] = createTask(rand(), "r"); flip[i] = rand()%2; } /* add tasks to the dynamic array */ for(i=0;i<size; i++) { addHeap(&mainList, task[i]); addSkipList(&skippy, task[i]); } long start = clock(); long finish = clock(); long add = 0; long remove = 0; long cnt_add = 0; long cnt_remove = 0; long sadd = 0; long sremove = 0; long scnt_add = 0; long scnt_remove = 0; for(i=0;i<size*2; i++) { if(flip[i]) { start = clock(); addHeap(&mainList, task[i]); finish = clock(); add += (finish-start); cnt_add++; start = clock(); addSkipList(&skippy, task[i]); finish = clock(); sadd += (finish-start); scnt_add++; } else { start = clock(); removeMinHeap(&mainList); finish = clock(); remove += (finish-start); cnt_remove++; start = clock(); removeMinSkipList(&skippy); finish = clock(); sremove += (finish-start); scnt_remove++; } } printf("Below are the average runtimes for a given number of operations.\n"); printf("----HEAP----\n"); printf("Adding %ld tasks took %lf clocks.\n", cnt_add, (double)(add) / (double)(cnt_add)); printf("Removing %ld minimums took %lf clocks.\n", cnt_remove, (double)(remove) / (double)cnt_remove); printf("----SKIP LIST----\n"); printf("Adding %ld tasks took %lf clocks.\n", scnt_add, (double)(sadd) / (double)(scnt_add)); printf("Removing %ld minimums took %lf clocks.\n", scnt_remove, (double)(sremove) / (double)scnt_remove); return 0; }