//allocate space for tree struct and set vars heap* createHeap() { heap *h = (heap*)malloc(sizeof(heap)); h->root = NULL; h->q = createDeque(); return h; }
OBMap * copyMap(const OBMap *to_copy){ OBMap *copy; OBMapPair *mp; OBDequeIterator *it; assert(to_copy); copy = createDefaultMap(); copy->cap_idx = to_copy->cap_idx; copy->collisions = to_copy->collisions; copy->pairs = createDeque(); /* copy deque manually, internal objects need to be copied as well as Deque * itself */ it = getDequeHeadIt(to_copy->pairs); if(it){ do{ mp = copyMapPair((OBMapPair *)objAtDequeIt(to_copy->pairs, it)); addDequeTail(copy->pairs, (obj *)mp); release((obj *)mp); }while(iterateDequeNext(to_copy->pairs, it)); } release((obj *)it); rehashMap(copy); /* cannot grab to_copy->hash_table directly, stored iterators must point to values within copy, not to copy */ return copy; }
void clearMap(OBMap *m){ assert(m); release((obj *)m->hash_table); release((obj *)m->pairs); m->hash_table = createVector(MAP_CAPACITIES[m->cap_idx]); m->pairs = createDeque(); }
//O(n*k) with the radixSort function as the longest operation int main(int argc, char * argv[]){ DEQUE *radixarray[RADIXSIZE], *maindeque; int i, number, max, mainitems; maindeque = createDeque(); for(i = 0; i < RADIXSIZE; i++) radixarray[i] = createDeque(); getNumbers(maindeque, &max); radixSort(maindeque, radixarray, max); mainitems = numItems(maindeque); system("clear"); for(i = 0; i < mainitems; i++){ number = removeLast(maindeque); printf("%d\n", number); } destroyDeque(maindeque); for(i = 0; i < RADIXSIZE; i++){ destroyDeque(radixarray[i]); } return 0; }
OBMap * createMapWithCapacity(uint32_t capacity){ OBMap *m = createDefaultMap(); uint32_t i = 0; while(MAP_CAPACITIES[i] < capacity && i < NUM_CAPACITIES - 1) i++; m->hash_table = createVector(MAP_CAPACITIES[i]); m->pairs = createDeque(); m->cap_idx = i; return m; }
int main(void) { int x, y; WINDOW *win; win = initscr(); curs_set(0); getmaxyx(win, y, x); width = x / 2 - 1; height = y / 2 - 1; createMaze(); do { clear(); refresh(); initMaze(); dp = createDeque(); buildMaze(0, 0); destroyDeque(dp); printMaze(); dp = createDeque(); solveMaze(); destroyDeque(dp); move(height * 2 + 1, 0); printw("Press 'q' to quit or any other key to run again."); refresh(); } while (getchar() != 'q'); clear(); refresh(); endwin(); return EXIT_SUCCESS; }
int main (int argc, const char* argv[]) { /* * argc should be 2 * arvg[1] is the size of the page table/ amount of memory avaiable/ the number of page frames */ if (argc != 2) { fprintf(stderr, "Not enough input arguments\n"); return 1; } int page_frames = atoi(argv[1]); char buffer[BUFFER_SIZE]; int page_faults = 0; //counter of page faults int memory_accesses = 0; //counter of memory accesses (lines in file) //create DEQUE DEQUE* page_table = createDeque(); //loop through the file and grab the numbers in there while (fgets(buffer, BUFFER_SIZE, stdin) != NULL ) { int access; //find the first integer in the line and store in x int n = sscanf(buffer, "%d", &access); //only try and access the page if the value is a number if (n > 0){ memory_accesses++; //if this page is already in the table, do nothing if (findNode(page_table, access) != NULL) { setRef(page_table,access); } //try and add the this page to the queue (oldest in front, newest in back) //if the queue is already at capacity, search for a page that we can remove else if (numItems(page_table) == page_frames) { /*FIND AND REPLACE PAGE*/ while ( getFirstRef(page_table) != 0) { //move the node with the value of first to the back of the deque moveToBack(page_table, getFirst(page_table)); } //remove the first node because it is unreferenced removeFirst(page_table); //add the new node at the end of the queue addLast(page_table, access); page_faults++; printf("PAGE FAULT: %d\n", access); } //if the page is not already in the list and there is room to add it, place it at the end of the list else { addLast(page_table, access); } } } //printf("memory accesses: %d\n",memory_accesses); //printf("page faults: %d\n",page_faults); }
int main(void) { DEQUE **digit; /* index of deques */ digit = (DEQUE **) malloc(sizeof(DEQUE*) * RADIX); int i,j,k; /* generic counter variables */ /* allocate space for the deques; Complexity: O(1) */ for (i = 0; i < RADIX; i++) digit[i] = createDeque(); int iteration; /* number of iterations needed for sorting */ int num; /* placeholder for temporary integer values */ int max = 0; /* stores the maximum of numbers entered */ /* User Integer Insertion -- while Ctrl-D is not pressed... * Integer cannot be negative. It also keeps track of * the maximum value of the integers entered. * * Complexity: O(n); */ while (scanf("%d",&num) != EOF) { assert (num >= 0); addFirst(digit[0],num); if (num > max) max = num; } /* Calculating 'iteration' a.k.a. max # of digits to sort */ iteration = (int) ceil( log(max+1) / log(RADIX) ); /* THE ACTUAL SORTING ALGORITHM * Sorting by each digit, starting with the 'ones' digit (in a * base ten number system), it traverses each digit's deque, removing * the number of elements originally placed in them and reassigning * each number the deque corresponding to the next digit's value. * * [EXAMPLE: 112 is taken out of '2' deque and placed in the '1' deque * when sorting by the second digit(to the left)] * * Complexity: ~ O(n) */ for (i = 0; i < iteration; i++) { /* Divisor precalculated for ease */ int divisor = 1; /* will help determine which digit to eval */ for (j = 0; j < i; j++) divisor *= RADIX; /* Taking values out and placing them back... * Complexity: O(n) */ for (j = 0; j < RADIX; j++) { /* 'nItems' will help ensure only old values*/ int nItems = numItems(digit[j]); while (nItems > 0) { num = removeFirst(digit[j]); /* k is the appropriate digit of 'num' */ k = (num / divisor) % RADIX; addLast(digit[k],num); nItems--; } } } /* Time to print things out; Complexity: O(n) */ for (i = 0; i < RADIX; i++) { int nItems = numItems(digit[i]); for (j = 0; j < nItems; j++) { num = removeFirst(digit[i]); printf("%d ",num); } /* Deallocate memory for each deque */ destroyDeque(digit[i]); } /* Finally, free the index. */ free(digit); return 0; }
int main(void) { int i, input, m, digits, exp; DEQUE *list, *dp[r]; //1 deque for each digit //make a deque to hold a list of numbers m = 0; list = createDeque(); //make an array of deques to sort by digit for(i = 0; i < r; i++) dp[i] = createDeque(); //read in non-negative intergers while(scanf("%d", &input) == 1) { if(input >= 0) { addLast(list, input); //keep track of max if(input > m) m = input; } else { printf("List cannot contain negative numbers.\n"); return -1; } } exp = 1; //number of iterations through loop digits = ceil(log(m + 1)/log(r)); //Sort list into separate deques by digit while(digits > 0) { //place in respective buckets while(numItems(list) > 0) { input = removeFirst(list); addLast(dp[(input/exp) % r], input); } //Take sorted numbers and place back into list for(i = 0; i < r; i++) { while(numItems(dp[i]) > 0) addLast(list, removeFirst(dp[i])); } exp *= r; //increase exp to check next digit digits--; //decrease after check each digit } //Print the list while(numItems(list) > 0) printf("%d\n", removeFirst(list)); return 1; }