Traitement-signal-plantes/Code-C/queue.c

115 lines
2.5 KiB
C
Raw Normal View History

2022-06-10 10:15:14 +02:00
#include <assert.h>
2022-06-10 10:47:17 +02:00
#include <stdlib.h>
#include "queue.h"
2022-06-10 10:15:14 +02:00
/**
2022-06-10 10:47:17 +02:00
* @brief struct queue struct used for queueing string name
2022-06-10 10:15:14 +02:00
*
* @param charLen lenght of tabChar pointeur
* @param tabChar char array pointer
* @param pNextE point next element
*/
struct queue {
int charLen;
char * tabChar;
Pqueue pNextE;
2022-06-10 10:47:17 +02:00
};
2022-06-10 10:15:14 +02:00
2022-06-10 10:47:17 +02:00
typedef struct queue *Pqueue;
2022-06-10 10:15:14 +02:00
/**
* @brief Create Element of queue
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
Pqueue createE(int lenChar){
2022-06-10 10:47:17 +02:00
Pqueue new = (Pqueue) malloc(sizeof(struct queue));
2022-06-10 10:15:14 +02:00
assert(new);
/* ???? tab char create or give pointer ???*/
2022-06-10 10:47:17 +02:00
new->charLen = lenChar;
new->tabChar = (char *) malloc(lenChar * sizeof(char));
new->pNextE = NULL;
2022-06-10 10:15:14 +02:00
return new;
}
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
Pqueue pNextE(Pqueue elem){
assert(elem);
2022-06-10 10:47:17 +02:00
return elem->pNextE;
2022-06-10 10:15:14 +02:00
}
/**
* @brief remove and free the last element of queue
*
* @param elem current element
* @return Pqueue current element
*/
Pqueue rmLastE(Pqueue elem){
assert(elem);
Pqueue tmp = elem, previous = NULL;
2022-06-10 10:47:17 +02:00
while(elem->pNextE != NULL){
2022-06-10 10:15:14 +02:00
previous = tmp;
tmp = pNextE(tmp);
}
2022-06-10 10:47:17 +02:00
free(tmp->tabChar);
2022-06-10 10:15:14 +02:00
free(tmp);
2022-06-10 10:47:17 +02:00
previous->pNextE = NULL;
2022-06-10 10:15:14 +02:00
return elem;
}
/**
* @brief remove and free the first element of queue
*
* @param elem target to remove (the element need to be the first)
* @return Pqueue the second element after remove first
*/
Pqueue rmFrstE(Pqueue elem){
assert(elem);
2022-06-10 10:47:17 +02:00
Pqueue tmp = elem->pNextE;
free(elem->tabChar);
2022-06-10 10:15:14 +02:00
free(elem);
return tmp;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
Pqueue nextDelFrst(Pqueue elem){
assert(elem);
2022-06-10 10:47:17 +02:00
Pqueue tmp = elem->pNextE;
2022-06-10 10:15:14 +02:00
rmFrstE(elem);
return tmp;
}
/**
* @brief add in the end of queue element with string parameter
*
* @param elem Pqueue will be added at the end of queue
* @param str the string will be bind at the element
* @param len the lenght of char array
* @return Pqueue pointer to the last element
*/
Pqueue addLastQ(Pqueue elem, const char* str, int len){
assert(elem);
assert(str);
Pqueue tmp = elem, previous = NULL;
2022-06-10 10:47:17 +02:00
while(elem->pNextE != NULL){
2022-06-10 10:15:14 +02:00
previous = tmp;
tmp = pNextE(tmp);
}
tmp = createE(len);
2022-06-10 10:47:17 +02:00
tmp->tabChar = str;
tmp->charLen = len;
2022-06-10 10:15:14 +02:00
return tmp;
}