/* 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); freeDynArr(destination); _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 */ int i; TYPE *newData = (TYPE*)malloc(newCap*sizeof(TYPE)); assert(newData != 0); for (i = 0; i < v->size; i++) { newData[i] = v->data[i]; } freeDynArr(v); v->data = newData; v->capacity = newCap; v->size = i; }
/* 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) { assert(v != 0); assert(newCap > v->capacity); DynArr* temp = newDynArr(newCap); //create a new array int size = sizeDynArr(v); //get the size of the array //copy elements from old array into new array for (int i = 0; i < size; i++) { addDynArr(temp, v->data[i]); } freeDynArr(v); //deallocate memory tied to old array *v = *temp; //replace old array with new array by overwriting the original structure free(temp); //deallocate memory tied to the temporary struct }
/* Print the list param: heap pointer to the list pre: the list is not empty post: The tasks from the list are printed out in priority order. The tasks are not removed from the list. */ void printList(DynArr *heap) { /* FIXME: Write this */ // create a new copy of the heap to prevent removal DynArr *tmpHeap = createDynArr(sizeDynArr(heap)); copyDynArr(heap, tmpHeap); while (sizeDynArr(tmpHeap) > 0) { // get first value of min priority TYPE min = getMinHeap(tmpHeap); print_type(min); removeMinHeap(tmpHeap); // remove min, to get next } freeDynArr(tmpHeap); }
/* 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) { int i; /* Create a new underlying array */ TYPE *newData = (TYPE*)malloc(sizeof(TYPE)*newCap); assert(newData != 0); /* copy elements to new data array */ for(i = 0; i < v->size; i++) newData[i] = v->data[i]; /* Delete the old underlying array */ freeDynArr(v); /* update capacity and size and data */ v->data = newData; v->capacity = newCap; v->size = i; }
/* 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 */ assert(v!= 0); struct DynArr *newArr = createDynArr(newCap); int newSize = sizeDynArr(v); //same as v so all the data can transfer int i; for (i = 0; i<newSize; i++) { newArr->data[i] = v->data[i]; //transfer data from v to newArr } freeDynArr(v); v->capacity = newCap; //new capacity set for v v->size = newSize; v->data = malloc(sizeof(DynArr)*newCap); //v created with a new "size" aka capacity for (i = 0; i<newSize; i++) { v->data[i] = newArr->data[i]; //transfer data back into v with new capacity } deleteDynArr(newArr); //no longer needed }
/* Deallocate data array and the dynamic array ure. param: v pointer to the dynamic array pre: v is not null post: the memory used by v->data is freed post: the memory used by d is freed */ void deleteDynArr(DynArr *v) { assert (v!= 0); freeDynArr(v); free(v); }
/* Deallocate data array and the dynamic array ure. param: v pointer to the dynamic array pre: none post: the memory used by v->data is freed post: the memory used by d is freed */ void deleteDynArr(DynArr *v) { freeDynArr(v); free(v); }
/* Deallocate data array and the dynamic array ure. param: v pointer to the dynamic array pre: none post: the memory used by v->data is freed post: the memory used by d is freed */ void deleteDynArr(struct DynArr *v) { freeDynArr(v); free(v); }