diff --git a/TP9-queue/Makefile b/TP9-queue/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..35c48074fd7fd2ae96094d8da275ac2b7e816a17 --- /dev/null +++ b/TP9-queue/Makefile @@ -0,0 +1,18 @@ +cc=gcc +LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm + +queue_array.x: queue_array.o + $(cc) -o $@ $^ $(LIBS) + +queue_list.x: queue_chained_list.o + $(cc) -o $@ $^ $(LIBS) + +queue_array.o: queue_array.c queue_array.h + $(cc) -c $^ $(LIBS) +queue_chained_list.o: queue_chained_list.c queue_chained_list.h + $(cc) -c $^ $(LIBS) + + +clean: + rm -f *.o *.x *.gch + diff --git a/TP9-queue/queue_array.c b/TP9-queue/queue_array.c new file mode 100644 index 0000000000000000000000000000000000000000..84b21f6cdaff9592d777fc1d7eea616e298b5fe9 --- /dev/null +++ b/TP9-queue/queue_array.c @@ -0,0 +1,83 @@ +#include "queue_array.h" + + +// typedef struct _queue { +// T* data; +// int lastIN; //dernier ajouté (head) +// //int firstIN; //index 0 est le premier ajouté +// int capacity; +// } queue; + +queue createQ() { + queue Q; + Q.capacity = 1; + Q.lastIN = -1; + Q.data = malloc(sizeof(int)); + return Q; +} + +void destroyQ(queue *Q) { + free(Q->data); + Q->capacity = -1; +} + +void resizeQ(queue *Q, int max) { + Q->capacity = max; + Q->data = realloc(Q->data, max * sizeof(int)); +} + +void insertQ(queue *Q, T value) { + if (Q->capacity - 1 == Q->lastIN) { // IF FULL + resizeQ(Q, Q->capacity + 1); + } + Q->lastIN++; + Q->data[Q->lastIN] = value; +} + +T extractFirst(queue *Q) { // a l'index 0 + T tmp=Q->data[0]; + + for(int i=0;i<Q->lastIN;i++){ + Q->data[i]=Q->data[i+1]; + } + + Q->lastIN--; + + resizeQ(Q, Q->capacity-1); + return tmp; + } + +T getFirst(queue Q){ + return Q.data[Q.lastIN]; +} + +bool isQueueEmpty(queue Q){ + return Q.capacity==-1; +} +int queueCount(queue Q){ + return Q.capacity; +} + +void displayQ(queue Q){ + for(int i=0;i<Q.capacity;i++){ + printf("%d ",Q.data[i]); + } + printf("\n"); +} + + +int main() { + + queue Q = createQ(); + + for (int i = 0; i < 10; i++) { + insertQ(&Q, i); + } + displayQ(Q); + extractFirst(&Q); + displayQ(Q); + + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/TP9-queue/queue_array.h b/TP9-queue/queue_array.h new file mode 100644 index 0000000000000000000000000000000000000000..4b2e18225847369e4f819b396126747c6b982c38 --- /dev/null +++ b/TP9-queue/queue_array.h @@ -0,0 +1,29 @@ +#ifndef _QUEUE_ARRAY_H_ +#define _QUEUE_ARRAY_H_ + +#include <assert.h> +#include <math.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + + +typedef int T; + +typedef struct _queue { + T* data; + int lastIN; //dernier ajouté (head) + //int firstIN; //index 0 est le premier ajouté + int capacity; +} queue; + +queue createQ(); +void destroyQ(queue *Q); +void insertQ(queue *Q, T value); //a la fin +T extractFirst(queue *Q); //a l'index 0 +T getFirst(queue Q); +bool isQueueEmpty(queue Q); +int queueCount(queue Q); +void displayQ(queue Q); + +#endif \ No newline at end of file diff --git a/TP9-queue/queue_chained_list.c b/TP9-queue/queue_chained_list.c new file mode 100644 index 0000000000000000000000000000000000000000..cb3a1a3304d3e96659aa632218fcef29f9d732ed --- /dev/null +++ b/TP9-queue/queue_chained_list.c @@ -0,0 +1,95 @@ +#include "queue_chained_list.h" +#include <stdio.h> +#include <stdlib.h> + +// typedef struct _element { +// T data; +// struct _element *next; +// } element; + +// typedef struct _queue { +// element* firstIn; //head +// element* lastIn; //debut +// } queue; + +queue createQ(){ + queue Q; + Q.firstIn=NULL; + Q.lastIn=NULL; + return Q; +} + +void destroyQ(queue *Q){ + + while(Q->firstIn!=NULL){ + element* tmp=Q->firstIn->next; + free(Q->firstIn); + Q->firstIn=tmp; + } +} + +void insertQ(queue *Q, T value){ + element *tmp = malloc(sizeof(element)); + tmp->data=value; + tmp->next=NULL; + if(isQueueEmpty(*Q)){ + Q->firstIn=tmp; + }else{ + Q->lastIn->next=tmp; + } + Q->lastIn= tmp; +} + +T extractFirst(queue *Q) { + T val=Q->firstIn->data; + element* oldFirst=Q->firstIn; + Q->firstIn=Q->firstIn->next; //next ! + free(oldFirst); + return val; +} + +T getFirst(queue Q){ + return Q.firstIn->data; +} + +bool isQueueEmpty(queue Q){ + return Q.firstIn==NULL; +} + +int queueCount(queue Q){ + int count=0; + + while (Q.firstIn!=NULL) { + count++; + Q.firstIn=Q.firstIn->next; + } + return count; +} + +void displayQ(queue Q){ + while (Q.firstIn!=NULL) { + printf("%d ",Q.firstIn->data); + Q.firstIn=Q.firstIn->next; + } + printf("\n"); +} + + + + + +int main() { + + queue Q = createQ(); + + for (int i = 0; i < 10; i++) { + insertQ(&Q, i); + } + displayQ(Q); + extractFirst(&Q); + displayQ(Q); + destroyQ(&Q); + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/TP9-queue/queue_chained_list.h b/TP9-queue/queue_chained_list.h new file mode 100644 index 0000000000000000000000000000000000000000..4099c693bb38253e4ce66e104b78148cbe2bc5ad --- /dev/null +++ b/TP9-queue/queue_chained_list.h @@ -0,0 +1,33 @@ +#ifndef _QUEUE_LIST_H_ +#define _QUEUE_LIST_H_ + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +typedef int T; + +typedef struct _element { + T data; + struct _element *next; +} element; + +typedef struct _queue { + element* firstIn; //head + element* lastIn; //debut +} queue; + +queue createQ(); +void destroyQ(queue *Q); +void insertQ(queue *Q, T value); //a la fin +T extractFirst(queue *Q); //a l'index 0 +T getFirst(queue Q); +bool isQueueEmpty(queue Q); +int queueCount(queue Q); +void displayQ(queue Q); + + +#endif +