C_Algorithms 2.0.0
Documentation
Loading...
Searching...
No Matches
queue.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <assert.h>
4
5#include "queue.h"
6
13myqueue_t *CreateQueue(uint32_t capacity)
14{
15 myqueue_t *queue = (myqueue_t *)malloc(sizeof(myqueue_t));
16
17 if (queue == NULL)
18 return NULL;
19
20 size_t data_size = capacity * sizeof(queue_value_t);
21 queue_value_t *data = (queue_value_t *)malloc(data_size);
22
23 if (data == NULL)
24 {
25 free(queue);
26 return NULL;
27 }
28
29 queue->front_id = 0u;
30 queue->back_id = capacity-1;
31 queue->capacity = capacity;
32 queue->size = 0u;
33 queue->data = data;
34
35 return queue;
36}
37
45{
46 if (queue != NULL)
47 {
48 if (queue->data != NULL)
49 {
50 free(queue->data);
51 }
52 free(queue);
53 }
54 return NULL;
55}
56
65{
66 return (queue->size == 0u);
67}
68
77{
78 return (queue->size == queue->capacity);
79}
80
88{
89 if (QueueIsFull(queue))
90 return;
91
93 queue->data[queue->back_id] = value;
94 queue->size++;
95}
96
104{
105 if (QueueIsEmpty(queue))
106 return QUEUE_NO_VALUE;
107
110 queue->size--;
111 return value;
112}
113
126
139
146{
147 if (queue == NULL)
148 return;
149
150 printf(
151 "The queue contains %u elements with a capacity of %u.\n",
152 queue->size,
154 );
155
156 for (int32_t i = 0; i < queue->size; i++)
157 {
158 printf("Index: %d, Value %f\n", i, queue->data[i]);
159 }
160}
161
162/*<-- SIMPLE CALL TO NOT CLUTTER THE MAIN.C -->*/
163void Queue()
164{
165 uint32_t capacity = 4u;
166 myqueue_t *queue = CreateQueue(capacity);
167 PushQueue(queue, 2.0f);
168 PushQueue(queue, 3.0f);
171}
queue_value_t BackQueue(myqueue_t *queue)
Last element of the Queue.
Definition queue.c:133
void PrintQueue(myqueue_t *queue)
Print the whole queue.
Definition queue.c:145
void PushQueue(myqueue_t *queue, queue_value_t value)
Put new data in the Queue object.
Definition queue.c:87
myqueue_t * FreeQueue(myqueue_t *queue)
Delete Queue object.
Definition queue.c:44
bool QueueIsEmpty(myqueue_t *queue)
Check if Queue is empty.
Definition queue.c:64
myqueue_t * CreateQueue(uint32_t capacity)
Create a Queue object.
Definition queue.c:13
queue_value_t PopQueue(myqueue_t *queue)
Delete data from Queue object.
Definition queue.c:103
queue_value_t FrontQueue(myqueue_t *queue)
First value of the Queue.
Definition queue.c:120
bool QueueIsFull(myqueue_t *queue)
Check if Queue is full.
Definition queue.c:76
void Queue()
Definition queue.c:163
float queue_value_t
Definition queue.h:9
#define QUEUE_NO_VALUE
Definition queue.h:10
Definition queue.h:13
queue_value_t * data
Definition queue.h:18
u_int32_t front_id
Definition queue.h:14
uint32_t capacity
Definition queue.h:17
uint32_t size
Definition queue.h:16
uint32_t back_id
Definition queue.h:15