Exemplo n.º 1
0
//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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
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;
}