forked from Telodendria/Telodendria
Add basic Queue implementation.
This commit is contained in:
parent
b632411b61
commit
ab4afe1587
2 changed files with 214 additions and 0 deletions
164
src/Queue.c
Normal file
164
src/Queue.c
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
* (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge,
|
||||||
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
|
* and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include <Queue.h>
|
||||||
|
|
||||||
|
struct Queue
|
||||||
|
{
|
||||||
|
void **items;
|
||||||
|
size_t size;
|
||||||
|
int front;
|
||||||
|
int rear;
|
||||||
|
};
|
||||||
|
|
||||||
|
Queue *
|
||||||
|
QueueCreate(size_t size)
|
||||||
|
{
|
||||||
|
Queue *q;
|
||||||
|
|
||||||
|
if (!size)
|
||||||
|
{
|
||||||
|
/* Can't have a queue of length zero */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
q = malloc(sizeof(Queue));
|
||||||
|
if (!q)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->items = malloc(size * sizeof(void *));
|
||||||
|
if (!q->items)
|
||||||
|
{
|
||||||
|
free(q);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->size = size;
|
||||||
|
q->front = -1;
|
||||||
|
q->rear = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
QueueFree(Queue * q)
|
||||||
|
{
|
||||||
|
if (q)
|
||||||
|
{
|
||||||
|
free(q->items);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
QueueFull(Queue *)
|
||||||
|
{
|
||||||
|
if (!q)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((q->front == q->rear + 1) || (q->front == 0 && q->rear == q->size - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
QueueEmpty(Queue *)
|
||||||
|
{
|
||||||
|
if (!q)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return q->front == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
QueuePush(Queue * q, void *element)
|
||||||
|
{
|
||||||
|
if (!q || !element)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (QueueFull(q))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (q->front == -1)
|
||||||
|
{
|
||||||
|
q->front = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
q->rear = (q->rear + 1) % q->size;
|
||||||
|
q->items[q->rear] = element;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
QueuePop(Queue * q)
|
||||||
|
{
|
||||||
|
void *element;
|
||||||
|
|
||||||
|
if (!q)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (QueueEmpty(q))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
element = q->items[q->front];
|
||||||
|
|
||||||
|
if (q->front == q->rear)
|
||||||
|
{
|
||||||
|
q->front = -1;
|
||||||
|
q->rear = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
q->front = (q->front + 1) % q->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
QueuePeek(Queue * q)
|
||||||
|
{
|
||||||
|
if (!q)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (QueueEmpty(q))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return q->items[q->front];
|
||||||
|
}
|
50
src/include/Queue.h
Normal file
50
src/include/Queue.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
* (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge,
|
||||||
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
|
* and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef TELODENDRIA_QUEUE_H
|
||||||
|
#define TELODENDRIA_QUEUE_H
|
||||||
|
|
||||||
|
typedef struct Queue Queue;
|
||||||
|
|
||||||
|
extern Queue *
|
||||||
|
QueueCreate(size_t);
|
||||||
|
|
||||||
|
extern void
|
||||||
|
QueueFree(Queue *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
QueuePush(Queue *, void *);
|
||||||
|
|
||||||
|
extern void *
|
||||||
|
QueuePop(Queue *);
|
||||||
|
|
||||||
|
extern void *
|
||||||
|
QueuePeek(Queue *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
QueueFull(Queue *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
QueueEmpty(Queue *);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue