C_Algorithms 2.0.0
Documentation
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
queue.h File Reference
#include <stdint.h>
#include <math.h>
#include <stdbool.h>
Include dependency graph for queue.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  queue
 

Macros

#define QUEUE_NO_VALUE   (queue_value_t)INFINITY
 

Typedefs

typedef float queue_value_t
 
typedef struct queue myqueue_t
 

Functions

myqueue_tCreateQueue (uint32_t capacity)
 Create a Queue object.
 
myqueue_tFreeQueue (myqueue_t *queue)
 Delete Queue object.
 
bool QueueIsEmpty (myqueue_t *queue)
 Check if Queue is empty.
 
bool QueueIsFull (myqueue_t *queue)
 Check if Queue is full.
 
void PushQueue (myqueue_t *queue, queue_value_t value)
 Put new data in the Queue object.
 
queue_value_t PopQueue (myqueue_t *queue)
 Delete data from Queue object.
 
queue_value_t FrontQueue (myqueue_t *queue)
 First value of the Queue.
 
queue_value_t BackQueue (myqueue_t *queue)
 Last element of the Queue.
 
void PrintQueue (myqueue_t *queue)
 Print the whole queue.
 
void Queue ()
 

Macro Definition Documentation

◆ QUEUE_NO_VALUE

#define QUEUE_NO_VALUE   (queue_value_t)INFINITY

Definition at line 10 of file queue.h.

Referenced by BackQueue(), FrontQueue(), and PopQueue().

Typedef Documentation

◆ myqueue_t

typedef struct queue myqueue_t

◆ queue_value_t

typedef float queue_value_t

Definition at line 9 of file queue.h.

Function Documentation

◆ BackQueue()

queue_value_t BackQueue ( myqueue_t * queue)

Last element of the Queue.

Parameters
queue
Returns
queue_value_t

Definition at line 133 of file queue.c.

134{
135 if (QueueIsEmpty(queue))
136 return QUEUE_NO_VALUE;
137 return queue->data[queue->back_id];
138}
bool QueueIsEmpty(myqueue_t *queue)
Check if Queue is empty.
Definition queue.c:64
#define QUEUE_NO_VALUE
Definition queue.h:10
Definition queue.h:13
queue_value_t * data
Definition queue.h:18
uint32_t back_id
Definition queue.h:15

References queue::back_id, queue::data, QUEUE_NO_VALUE, and QueueIsEmpty().

Here is the call graph for this function:

◆ CreateQueue()

myqueue_t * CreateQueue ( uint32_t capacity)

Create a Queue object.

Parameters
capacity
Returns
myqueue_t*

Definition at line 13 of file queue.c.

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}
float queue_value_t
Definition queue.h:9
u_int32_t front_id
Definition queue.h:14
uint32_t capacity
Definition queue.h:17
uint32_t size
Definition queue.h:16

References queue::back_id, queue::capacity, queue::data, queue::front_id, and queue::size.

Referenced by Queue().

Here is the caller graph for this function:

◆ FreeQueue()

myqueue_t * FreeQueue ( myqueue_t * queue)

Delete Queue object.

Parameters
queue
Returns
myqueue_t*

Definition at line 44 of file queue.c.

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}

References queue::data.

Referenced by Queue().

Here is the caller graph for this function:

◆ FrontQueue()

queue_value_t FrontQueue ( myqueue_t * queue)

First value of the Queue.

Parameters
queue
Returns
queue_value_t

Definition at line 120 of file queue.c.

121{
122 if (QueueIsEmpty(queue))
123 return QUEUE_NO_VALUE;
124 return queue->data[queue->front_id];
125}

References queue::data, queue::front_id, QUEUE_NO_VALUE, and QueueIsEmpty().

Here is the call graph for this function:

◆ PopQueue()

queue_value_t PopQueue ( myqueue_t * queue)

Delete data from Queue object.

Parameters
queue
Returns
queue_value_t

Definition at line 103 of file queue.c.

104{
105 if (QueueIsEmpty(queue))
106 return QUEUE_NO_VALUE;
107
110 queue->size--;
111 return value;
112}

References queue::capacity, queue::data, queue::front_id, QUEUE_NO_VALUE, QueueIsEmpty(), and queue::size.

Here is the call graph for this function:

◆ PrintQueue()

void PrintQueue ( myqueue_t * queue)

Print the whole queue.

Parameters
queue

Definition at line 145 of file queue.c.

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}

References queue::capacity, queue::data, and queue::size.

Referenced by Queue().

Here is the caller graph for this function:

◆ PushQueue()

void PushQueue ( myqueue_t * queue,
queue_value_t value )

Put new data in the Queue object.

Parameters
queue
value

Definition at line 87 of file queue.c.

88{
89 if (QueueIsFull(queue))
90 return;
91
93 queue->data[queue->back_id] = value;
94 queue->size++;
95}
bool QueueIsFull(myqueue_t *queue)
Check if Queue is full.
Definition queue.c:76

References queue::back_id, queue::capacity, queue::data, QueueIsFull(), and queue::size.

Referenced by Queue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Queue()

void Queue ( )

Definition at line 163 of file queue.c.

164{
165 uint32_t capacity = 4u;
166 myqueue_t *queue = CreateQueue(capacity);
167 PushQueue(queue, 2.0f);
168 PushQueue(queue, 3.0f);
171}
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
myqueue_t * CreateQueue(uint32_t capacity)
Create a Queue object.
Definition queue.c:13

References CreateQueue(), FreeQueue(), PrintQueue(), and PushQueue().

Referenced by FunctionCall().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ QueueIsEmpty()

bool QueueIsEmpty ( myqueue_t * queue)

Check if Queue is empty.

Parameters
queue
Returns
true
false

Definition at line 64 of file queue.c.

65{
66 return (queue->size == 0u);
67}

References queue::size.

Referenced by BackQueue(), FrontQueue(), and PopQueue().

Here is the caller graph for this function:

◆ QueueIsFull()

bool QueueIsFull ( myqueue_t * queue)

Check if Queue is full.

Parameters
queue
Returns
true
false

Definition at line 76 of file queue.c.

77{
78 return (queue->size == queue->capacity);
79}

References queue::capacity, and queue::size.

Referenced by PushQueue().

Here is the caller graph for this function: