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

115 lines
2.5 KiB
C

#include <assert.h>
#include <stdlib.h>
#include "queue.h"
/**
* @brief struct queue struct used for queueing string name
*
* @param charLen lenght of tabChar pointeur
* @param tabChar char array pointer
* @param pNextE point next element
*/
struct queue {
int charLen;
char * tabChar;
Pqueue pNextE;
};
typedef struct queue *Pqueue;
/**
* @brief Create Element of queue
*
* @param lenChar size of char array that will be created
* @return Pqueue new element created
*/
Pqueue createE(int lenChar){
Pqueue new = (Pqueue) malloc(sizeof(struct queue));
assert(new);
/* ???? tab char create or give pointer ???*/
new->charLen = lenChar;
new->tabChar = (char *) malloc(lenChar * sizeof(char));
new->pNextE = NULL;
return new;
}
/**
* @brief switch to the next element
*
* @param elem current element
* @return Pqueue next element
*/
Pqueue pNextE(Pqueue elem){
assert(elem);
return elem->pNextE;
}
/**
* @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;
while(elem->pNextE != NULL){
previous = tmp;
tmp = pNextE(tmp);
}
free(tmp->tabChar);
free(tmp);
previous->pNextE = NULL;
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);
Pqueue tmp = elem->pNextE;
free(elem->tabChar);
free(elem);
return tmp;
}
/**
* @brief delete the first value and return the next value
*
* @param elem
* @return Pqueue
*/
Pqueue nextDelFrst(Pqueue elem){
assert(elem);
Pqueue tmp = elem->pNextE;
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;
while(elem->pNextE != NULL){
previous = tmp;
tmp = pNextE(tmp);
}
tmp = createE(len);
tmp->tabChar = str;
tmp->charLen = len;
return tmp;
}