naev 0.11.5
queue.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
4
13#include <stdlib.h>
16#include "queue.h"
17
18#include "log.h"
19
20
24typedef struct Node_ *Node;
25typedef struct Node_ {
26 void *data;
28} Node_;
29
33typedef struct Queue_ {
36} Queue_;
37
43Queue q_create (void)
44{
45 /* Create the queue. */
46 Queue q = malloc(sizeof(Queue_));
47
48 /* Check that we didn't get a NULL. */
49#ifdef DEBUGGING
50 if (q == NULL) {
51 WARN("q == NULL");
52 return NULL;
53 }
54#endif /* DEBUGGING */
55
56 /* Assign nothing into it. */
57 q->first = NULL;
58 q->last = NULL;
59
60 /* And return (a pointer to) the newly created queue. */
61 return q;
62}
63
69void q_destroy( Queue q )
70{
71#ifdef DEBUGGING
72 /* Check that we didn't get a NULL. */
73 if (q == NULL) {
74 WARN("q == NULL");
75 return;
76 }
77#endif /* DEBUGGING */
78
79 /* Free all the data. */
80 while (q->first != NULL)
81 q_dequeue(q);
82
83 free(q);
84
85 return;
86}
87
94void q_enqueue( Queue q, void *data )
95{
96 Node n;
97
98#ifdef DEBUGGING
99 /* Check that we didn't get a NULL. */
100 if (q == NULL) {
101 WARN("q == NULL");
102 return;
103 }
104#endif /* DEBUGGING */
105
106 /* Create a new node. */
107 n = malloc(sizeof(Node_));
108 n->data = data;
109 n->next = NULL;
110 if (q->first == NULL)
111 q->first = n;
112 else
113 q->last->next = n;
114 q->last = n;
115
116 return;
117}
118
125void* q_dequeue( Queue q )
126{
127 void *d;
128 Node temp;
129
130#ifdef DEBUGGING
131 /* Check that we didn't get a NULL. */
132 if (q == NULL) {
133 WARN("q == NULL");
134 return NULL;
135 }
136#endif /* DEBUGGING */
137
138 /* Check that it's not empty. */
139 if (q->first == NULL)
140 return NULL;
141
142 d = q->first->data;
143 temp = q->first;
144 q->first = q->first->next;
145 if (q->first == NULL)
146 q->last = NULL;
147 free(temp);
148
149 return d;
150}
151
158int q_isEmpty( Queue q )
159{
160#ifdef DEBUGGING
161 /* Check that we didn't get a NULL. */
162 if (q == NULL) {
163 WARN("q == NULL");
164 return -1;
165 }
166#endif /* DEBUGGING */
167
168 if (q->first == NULL)
169 return 1;
170 else
171 return 0;
172}
void * q_dequeue(Queue q)
Dequeues an item.
Definition queue.c:125
int q_isEmpty(Queue q)
Checks if the queue is empty.
Definition queue.c:158
void q_destroy(Queue q)
Destroys a queue.
Definition queue.c:69
Queue q_create(void)
Creates a queue.
Definition queue.c:43
void q_enqueue(Queue q, void *data)
Enqueues an item.
Definition queue.c:94
static const double d[]
Definition rng.c:273
Node struct.
Definition queue.c:25
void * data
Definition queue.c:26
Node next
Definition queue.c:27
Node in the thread queue.
Definition threadpool.c:51
Queue struct.
Definition queue.c:33
Node first
Definition queue.c:34
Node last
Definition queue.c:35