diff --git a/tp3/TP3/alloc.o b/tp3/TP3/alloc.o index 1eda92c..fc4440c 100644 Binary files a/tp3/TP3/alloc.o and b/tp3/TP3/alloc.o differ diff --git a/tp3/TP3/main.c b/tp3/TP3/main.c index 37b7d83..63fa6a6 100644 --- a/tp3/TP3/main.c +++ b/tp3/TP3/main.c @@ -8,10 +8,10 @@ #include // Basculez à 1 les valeurs ci-dessous pour activer les tests correspondants. -#define TEST_STACK_BASIC 1 +#define TEST_STACK_BASIC 0 #define TEST_SORT_STACK 0 -#define TEST_COPY_STACK 1 -#define TEST_HANOI 1 +#define TEST_COPY_STACK 0 +#define TEST_HANOI 0 #define TEST_QUEUE_BASIC 0 #define TEST_COPY_QUEUE 0 diff --git a/tp3/TP3/main.o b/tp3/TP3/main.o index ff22118..59d1259 100644 Binary files a/tp3/TP3/main.o and b/tp3/TP3/main.o differ diff --git a/tp3/TP3/queue.c b/tp3/TP3/queue.c index 5f33a07..eb953f1 100644 --- a/tp3/TP3/queue.c +++ b/tp3/TP3/queue.c @@ -1,5 +1,6 @@ #include "queue.h" #include "alloc.h" +#include "testprint.h" #include #include #include @@ -14,14 +15,40 @@ // Cette fonction sera utilisée lorsque le tableau est plein et qu'on veut y // ajouter une valeur. static void grow_queue(queue *p) { - return; + while (p -> left != 0) { + int temp = p -> array[p -> left]; + for (int j = 0; j < p -> size_array; j++) { + p -> array[(p -> left + j) % p -> size_array] = p -> array[(p -> left + j + 1) % p -> size_array]; + } + p -> array[(p -> left - 1) % p -> size_array] = temp; + p -> left--; + p -> right = (p -> right - 1) % p -> size_array; + } + p -> right = p -> size_array; + p -> left = 0; + p -> size_array *= 2; + p -> array = realloc(p -> array, p -> size_array * sizeof(int)); + return; } // Divise par deux la taille du tableau utilisé dans la représentation // Cette fonction sera utilisée lorsque le tableau est rempli à moins de 25% de // sa capacité. static void shrink_queue(queue *p) { - return; + if (p -> size_array >= 2) { + while (p -> left != 0) { + int temp = p -> array[p -> left]; + for (int j = 0; j < p -> size_array; j++) { + p -> array[(p -> left + j) % p -> size_array] = p -> array[(p -> left + j + 1) % p -> size_array]; + } + p -> array[(p -> left - 1) % p -> size_array] = temp; + p -> left--; + p -> right = (p -> right - 1) % p -> size_array; + } + p -> size_array /= 2; + p -> array = realloc(p -> array, p -> size_array * sizeof(int)); + } + return; } //////////////////////////////////////////////////////////////// @@ -29,25 +56,72 @@ static void shrink_queue(queue *p) { //////////////////////////////////////////////////////////////// queue *create_queue(void) { - return NULL; + queue *p = malloc(sizeof(queue)); + p -> size_array = 1; + p -> left = 0; + p -> right = 0; + p -> array = malloc(sizeof(int)); + p -> empty = true; + return p; } void delete_queue(queue *p) { - return; + if (p == NULL) return; + p -> size_array = 0; + p -> left = 0; + p -> right = 0; + p -> empty = true; + free(p -> array); + free(p); + return; } bool isempty_queue(const queue *p) { - return false; + return p -> empty; } int getsize_queue(const queue *p) { - return 0; + if (isempty_queue(p)) return 0; + else if (p -> right > p -> left) return p -> right - p -> left; + else if (p -> right == p -> left) return p -> size_array; + else return p -> size_array - (p -> left - p -> right); } void enqueue(int val, queue *p) { - return; + if (p -> left == p -> right && !isempty_queue(p)) { + grow_queue(p); + } + p -> left--; + if (p -> left < 0) { + p -> left += p -> size_array; + } + p -> array[p -> left] = val; + p -> empty = false; + return; } int dequeue(queue *p) { - return 0; + // print_queue(p); + if (isempty_queue(p)) { + FATAL("Pop on empty queue"); + } + p -> right--; + if (p -> right < 0) { + p -> right += p -> size_array; + } + int val = p -> array[p -> right]; + if (getsize_queue(p) <= p -> size_array / 4 && p -> size_array > 1) { + shrink_queue(p); + } else if (getsize_queue(p) <= p -> size_array / 4) { + p -> empty = true; + val = p -> array[p -> left]; + p -> left = 0; + p -> right = 0; + return val; + } + // printf("left = %d, right = %d size = %d\n", p -> left, p -> right, getsize_queue(p)); + if (p -> left == p -> right + 1) { + p -> empty = true; + } + return val; } diff --git a/tp3/TP3/queue.d b/tp3/TP3/queue.d index 93c5866..edd26a2 100644 --- a/tp3/TP3/queue.d +++ b/tp3/TP3/queue.d @@ -1 +1 @@ -queue.o: queue.c queue.h alloc.h error.h +queue.o: queue.c queue.h alloc.h error.h testprint.h stack.h diff --git a/tp3/TP3/queue.o b/tp3/TP3/queue.o index 4a85dac..66a26b7 100644 Binary files a/tp3/TP3/queue.o and b/tp3/TP3/queue.o differ diff --git a/tp3/TP3/queuetest.c b/tp3/TP3/queuetest.c index 58af4c4..31c8342 100644 --- a/tp3/TP3/queuetest.c +++ b/tp3/TP3/queuetest.c @@ -1,21 +1,59 @@ #include "queuetest.h" #include "alloc.h" +#include "queue.h" +#include "testprint.h" #include -#include #include queue *random_queue(int size, int maxval) { - return NULL; + queue* p = create_queue(); + for (int i = 0; i < size; i++) { + enqueue(rand() % maxval, p); + // print_queue(p); + } + return p; } queue *copy_queue(queue *p) { - return NULL; + queue* p_out = create_queue(); + for (int i = 0; i < getsize_queue(p); i++) { + // printf("left: %d right: %d size: %d\n", p -> left, p -> right, p -> size_array); + int val = dequeue(p); + // printf("val: %d\n", val); + enqueue(val, p); + enqueue(val, p_out); + // print_queue(p); + // print_queue(p_out); + } + return p_out; } queue *mountain_queue(int n) { - return NULL; + queue* p = create_queue(); + for (int i = 0; i < n; i++) { + enqueue(n - i, p); + // print_queue(p); + } + return p; } void select_sort_queue(queue *p) { - return; + int n = getsize_queue(p); + for (int i = 0; i < n; i++) { + int max = dequeue(p); + for (int j = 1; j < n - i; j++) { + int val = dequeue(p); + if (val > max) { + enqueue(max, p); + max = val; + } else { + enqueue(val, p); + } + } + for (int j = 0; j < i; j++) { + enqueue(dequeue(p), p); + } + enqueue(max, p); + } + return; } diff --git a/tp3/TP3/queuetest.d b/tp3/TP3/queuetest.d index 658c9cb..760c521 100644 --- a/tp3/TP3/queuetest.d +++ b/tp3/TP3/queuetest.d @@ -1 +1,2 @@ -queuetest.o: queuetest.c queuetest.h queue.h alloc.h error.h +queuetest.o: queuetest.c queuetest.h queue.h alloc.h error.h testprint.h \ + stack.h diff --git a/tp3/TP3/queuetest.o b/tp3/TP3/queuetest.o index 37a5ad7..ec05e15 100644 Binary files a/tp3/TP3/queuetest.o and b/tp3/TP3/queuetest.o differ diff --git a/tp3/TP3/stack.h b/tp3/TP3/stack.h index 0afca61..82cab16 100644 --- a/tp3/TP3/stack.h +++ b/tp3/TP3/stack.h @@ -1,5 +1,4 @@ /***********************************************/ -/***********************************************/ /** Implémentation des piles par les tableaux **/ /***********************************************/ /***********************************************/ diff --git a/tp3/TP3/stack.o b/tp3/TP3/stack.o index 97bc496..0a0b26a 100644 Binary files a/tp3/TP3/stack.o and b/tp3/TP3/stack.o differ diff --git a/tp3/TP3/stackqueue b/tp3/TP3/stackqueue index 2c1feb2..1412cd4 100755 Binary files a/tp3/TP3/stackqueue and b/tp3/TP3/stackqueue differ diff --git a/tp3/TP3/stacktest.c b/tp3/TP3/stacktest.c index 7e53e16..c5a7667 100644 --- a/tp3/TP3/stacktest.c +++ b/tp3/TP3/stacktest.c @@ -64,5 +64,29 @@ void hanoi_rec(stack *p1, stack *p2, stack *p3, } void bubble_sort_stack(stack *p) { + int n = size_stack(p); + stack *p_aux = create_stack(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - 1; j++) { + push(pop(p), p_aux); + } + print_stacks(n - i, 2, p, p_aux); + for (int j = i; j < n - 1; j++) { + int a = pop(p); + int b = pop(p_aux); + if (a < b) { + push(a, p); + push(b, p); + } else { + push(b, p); + push(a, p); + } + } + for (int j = 0; j < i; j++) { + push(pop(p_aux), p); + } + printf("\n"); + } + delete_stack(p_aux); return; } diff --git a/tp3/TP3/stacktest.o b/tp3/TP3/stacktest.o index d54eca6..0a2ecc7 100644 Binary files a/tp3/TP3/stacktest.o and b/tp3/TP3/stacktest.o differ diff --git a/tp3/TP3/testprint.o b/tp3/TP3/testprint.o index 65ccefe..69db6ce 100644 Binary files a/tp3/TP3/testprint.o and b/tp3/TP3/testprint.o differ