add files queue c and h
This commit is contained in:
parent
98e11c2443
commit
9ab534b727
112
queue.c
Normal file
112
queue.c
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* @brief squeue 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;
|
||||
}squeue;
|
||||
|
||||
typedef squeue *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(squeue));
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in a new issue