You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
1.1 KiB

#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
// Ajoute un élément à la fin d'une file
void push(Queue** queue, QData data)
{
Queue* cell = malloc(sizeof(Queue));
cell->data = data;
if (*queue == NULL)
{
*queue = cell;
cell->next = cell;
}
else
{
cell->next = (*queue)->next;
(*queue)->next = cell;
*queue = cell;
}
}
// Supprime un élément d'une file et retourne sa valeur
QData pop(Queue** queue)
{
if (*queue == NULL)
{
fprintf(stderr, "(AI) pop: empty Queue\n");
return NULL;
}
QData data = (*queue)->next->data;
Queue* tmp;
if ((*queue)->next == *queue)
{
free(*queue);
*queue = NULL;
}
else
{
tmp = (*queue)->next->next;
free((*queue)->next);
(*queue)->next = tmp;
}
return data;
}
// Supprime une file
void deleteQueue(Queue** queue)
{
if (*queue == NULL)
return;
Queue* tmp = (*queue)->next;
while (tmp != *queue)
{
Queue* tmp2 = tmp->next;
free(tmp);
tmp = tmp2;
}
*queue = NULL;
}