From e292f69c97a867ddf5321da61dc010b151407f51 Mon Sep 17 00:00:00 2001 From: thib <tempo2riz@gmail.com> Date: Wed, 9 Dec 2020 17:28:11 +0100 Subject: [PATCH] git add . --- TP9-queue/Makefile | 18 +++++++ TP9-queue/queue_array.c | 83 +++++++++++++++++++++++++++++ TP9-queue/queue_array.h | 29 +++++++++++ TP9-queue/queue_chained_list.c | 95 ++++++++++++++++++++++++++++++++++ TP9-queue/queue_chained_list.h | 33 ++++++++++++ 5 files changed, 258 insertions(+) create mode 100644 TP9-queue/Makefile create mode 100644 TP9-queue/queue_array.c create mode 100644 TP9-queue/queue_array.h create mode 100644 TP9-queue/queue_chained_list.c create mode 100644 TP9-queue/queue_chained_list.h diff --git a/TP9-queue/Makefile b/TP9-queue/Makefile new file mode 100644 index 0000000..35c4807 --- /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 0000000..84b21f6 --- /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 0000000..4b2e182 --- /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 0000000..cb3a1a3 --- /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 0000000..4099c69 --- /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 + -- GitLab