3 * @brief Functions used to manage a queue used by A* Algorithm.
4 * @author Jose Maria Martin Laguna <jmmartin@etud.insa-toulouse.fr>
11 #include "pathqueue.h"
14 * @name Queue functions
15 * This functions implements a queue needed by other functions.
19 * @brief Tests if a queue is empty
21 * @return 1 if the queue is empty, 0 otherwise
23 int queueIsEmpty(NodeQueue *q){
24 if ((q->first==NULL) && (q->last==NULL)) return 1;
29 * @brief Push a node into the queue in a FIFO mode
31 * @param x Coordonate X of a cell
32 * @param y Coordonate Y of a cell
34 void pushNode(NodeQueue * q, int x, int y){
35 Node *n= malloc(sizeof(Node));
37 /* Memory could not be allocated, so print an error and exit. */
38 fprintf(stderr, "Couldn't allocate memory\n");
46 if (queueIsEmpty(q)) {
56 * @brief Push a node into the queue in a LIFO mode
58 * @param x Coordonate X of a cell
59 * @param y Coordonate Y of a cell
61 void pushNodeLast(NodeQueue * q, int x, int y){
62 Node *n= malloc(sizeof(Node));
64 /* Memory could not be allocated, so print an error and exit. */
65 fprintf(stderr, "Couldn't allocate memory\n");
72 if (queueIsEmpty(q)) {
83 * @brief Pop the first node of the queue
85 * @param n Pointer to a node
86 * @return 1 if node was succesfully poped 0 if the queue is empty
88 int popNode(NodeQueue * q, Node * n){
90 if (!queueIsEmpty(q)){
93 tmp = (q->first)->next;
96 if ( (q->first) == NULL) q->last = NULL;
102 * @brief Print the queue
104 * @note Used only for debug
106 void printQueue(NodeQueue * q){
110 printf("(%d,%d)\n", n->x, n->y);
113 printf("No more Nodes in the queue!\n");
118 * @brief Init queue values
120 NodeQueue * newQueue(void){
122 q = malloc(sizeof(NodeQueue));
128 * @brief Free queue memory
130 * @return 1 if memory was succesfully free 0 if the queue is not empty
132 int delQueue(NodeQueue * q){
133 if (queueIsEmpty(q)) {
140 * @brief Finds out if the node (X,Y) is in the queue
142 * @param x Coordonate X of a cell
143 * @param y Coordonate Y of a cell
144 * @return 1 if (X,Y) was founded, 0 otherwise
146 int isInQueue(NodeQueue * q, int x, int y){
151 if((tmp->x==x)&&(tmp->y==y)) return 1;
158 * @brief Free memory allocated in the queue
161 void drainQueue(NodeQueue * q){
163 while(popNode(q, &n));