Skip to content
Snippets Groups Projects
Commit e292f69c authored by thib's avatar thib
Browse files

git add .

parent e5191ecd
No related branches found
No related tags found
No related merge requests found
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
#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
#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
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment