1 /*
2  * See Licensing and Copyright notice in naev.h
3  */
4 
5 /**
6  * @file queue.c
7  *
8  * @brief A dodgy implementation of a queue.
9  */
10 
11 
12 #include "queue.h"
13 
14 #include <stdlib.h>
15 
16 #include "log.h"
17 
18 
19 /**
20  * @brief Node struct.
21  */
22 typedef struct Node_ *Node;
23 typedef struct Node_ {
24    void *data; /**< Assosciated data. */
25    Node next; /**< Next node. */
26 } Node_;
27 
28 /**
29  * @brief Queue struct.
30  */
31 typedef struct Queue_ {
32    Node first; /**< First node in the queue. */
33    Node last; /**< Last node in the queue. */
34 } Queue_;
35 
36 /**
37  * @brief Creates a queue.
38  *
39  *    @return A pointer to a queue.
40  */
q_create(void)41 Queue q_create (void)
42 {
43    /* Create the queue. */
44    Queue q = malloc(sizeof(Queue_));
45 
46    /* Check that we didn't get a NULL. */
47 #ifdef DEBUGGING
48    if (q == NULL) {
49       WARN("q == NULL");
50       return NULL;
51    }
52 #endif /* DEBUGGING */
53 
54    /* Assign nothing into it. */
55    q->first = NULL;
56    q->last  = NULL;
57 
58    /* And return (a pointer to) the newly created queue. */
59    return q;
60 }
61 
62 /**
63  * @brief Destroys a queue.
64  *
65  *    @param q Queue to destroy.
66  */
q_destroy(Queue q)67 void q_destroy( Queue q )
68 {
69 #ifdef DEBUGGING
70    /* Check that we didn't get a NULL. */
71    if (q == NULL) {
72       WARN("q == NULL");
73       return;
74    }
75 #endif /* DEBUGGING */
76 
77    /* Free all the data. */
78    while(q->first != NULL)
79       q_dequeue(q);
80 
81    free(q);
82 
83    return;
84 }
85 
86 /**
87  * @brief Enqueues an item.
88  *
89  *    @param q Queue to use.
90  *    @param data Item to enqueue.
91  */
q_enqueue(Queue q,void * data)92 void q_enqueue( Queue q, void *data )
93 {
94    Node n;
95 
96 #ifdef DEBUGGING
97    /* Check that we didn't get a NULL. */
98    if (q == NULL) {
99       WARN("q == NULL");
100       return;
101    }
102 #endif /* DEBUGGING */
103 
104    /* Create a new node. */
105    n = malloc(sizeof(Node_));
106    n->data = data;
107    n->next = NULL;
108    if (q->first == NULL)
109       q->first = n;
110    else
111       q->last->next = n;
112    q->last = n;
113 
114    return;
115 }
116 
117 /**
118  * @brief Dequeues an item.
119  *
120  *    @param q Queue to use.
121  *    @return The data.
122  */
q_dequeue(Queue q)123 void* q_dequeue( Queue q )
124 {
125    void *d;
126    Node temp;
127 
128 #ifdef DEBUGGING
129    /* Check that we didn't get a NULL. */
130    if (q == NULL) {
131       WARN("q == NULL");
132       return NULL;
133    }
134 #endif /* DEBUGGING */
135 
136    /* Check that it's not empty. */
137    if (q->first == NULL)
138       return NULL;
139 
140    d        = q->first->data;
141    temp     = q->first;
142    q->first = q->first->next;
143    if (q->first == NULL)
144       q->last = NULL;
145    free(temp);
146 
147    return d;
148 }
149 
150 /**
151  * @brief Checks if the queue is empty.
152  *
153  *    @param q Queue to use.
154  *    @return 1 if it's empty, 0 if it has data.
155  */
q_isEmpty(Queue q)156 int q_isEmpty( Queue q )
157 {
158 #ifdef DEBUGGING
159    /* Check that we didn't get a NULL. */
160    if (q == NULL) {
161       WARN("q == NULL");
162       return -1;
163    }
164 #endif /* DEBUGGING */
165 
166    if (q->first == NULL)
167       return 1;
168    else
169       return 0;
170 }
171