naev 0.12.5
queue.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
9
11#include <stdlib.h>
13
14#include "queue.h"
15
16#include "log.h"
17
21typedef struct Node_ *Node;
22typedef struct Node_ {
23 void *data;
25} Node_;
26
30typedef struct Queue_ {
33} Queue_;
34
40Queue q_create( void )
41{
42 /* Create the queue. */
43 Queue q = malloc( sizeof( Queue_ ) );
44
45 /* Check that we didn't get a NULL. */
46#ifdef DEBUGGING
47 if ( q == NULL ) {
48 WARN( "q == NULL" );
49 return NULL;
50 }
51#endif /* DEBUGGING */
52
53 /* Assign nothing into it. */
54 q->first = NULL;
55 q->last = NULL;
56
57 /* And return (a pointer to) the newly created queue. */
58 return q;
59}
60
66void q_destroy( Queue q )
67{
68#ifdef DEBUGGING
69 /* Check that we didn't get a NULL. */
70 if ( q == NULL ) {
71 WARN( "q == NULL" );
72 return;
73 }
74#endif /* DEBUGGING */
75
76 /* Free all the data. */
77 while ( q->first != NULL )
78 q_dequeue( q );
79
80 free( q );
81
82 return;
83}
84
91void q_enqueue( Queue q, void *data )
92{
93 Node n;
94
95#ifdef DEBUGGING
96 /* Check that we didn't get a NULL. */
97 if ( q == NULL ) {
98 WARN( "q == NULL" );
99 return;
100 }
101#endif /* DEBUGGING */
102
103 /* Create a new node. */
104 n = malloc( sizeof( Node_ ) );
105 n->data = data;
106 n->next = NULL;
107 if ( q->first == NULL )
108 q->first = n;
109 else
110 q->last->next = n;
111 q->last = n;
112
113 return;
114}
115
122void *q_dequeue( Queue q )
123{
124 void *d;
125 Node temp;
126
127#ifdef DEBUGGING
128 /* Check that we didn't get a NULL. */
129 if ( q == NULL ) {
130 WARN( "q == NULL" );
131 return NULL;
132 }
133#endif /* DEBUGGING */
134
135 /* Check that it's not empty. */
136 if ( q->first == NULL )
137 return NULL;
138
139 d = q->first->data;
140 temp = q->first;
141 q->first = q->first->next;
142 if ( q->first == NULL )
143 q->last = NULL;
144 free( temp );
145
146 return d;
147}
148
155int q_isEmpty( Queue q )
156{
157#ifdef DEBUGGING
158 /* Check that we didn't get a NULL. */
159 if ( q == NULL ) {
160 WARN( "q == NULL" );
161 return -1;
162 }
163#endif /* DEBUGGING */
164
165 if ( q->first == NULL )
166 return 1;
167 else
168 return 0;
169}
void * q_dequeue(Queue q)
Dequeues an item.
Definition queue.c:122
int q_isEmpty(Queue q)
Checks if the queue is empty.
Definition queue.c:155
void q_destroy(Queue q)
Destroys a queue.
Definition queue.c:66
Queue q_create(void)
Creates a queue.
Definition queue.c:40
void q_enqueue(Queue q, void *data)
Enqueues an item.
Definition queue.c:91
static const double d[]
Definition rng.c:263
Node struct.
Definition queue.c:22
void * data
Definition queue.c:23
Node next
Definition queue.c:24
Represents a node of an object. Each node can have multiple meshes and children nodes with an associa...
Definition gltf.h:105
void * data
Definition threadpool.c:54
struct Node_ * next
Definition threadpool.c:55
Queue struct.
Definition queue.c:30
Node first
Definition queue.c:31
Node last
Definition queue.c:32