-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosqueue.c
More file actions
68 lines (48 loc) · 976 Bytes
/
osqueue.c
File metadata and controls
68 lines (48 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "osqueue.h"
#include <stdlib.h>
OSQueue* osCreateQueue()
{
OSQueue* q = malloc(sizeof(OSQueue));
if(q == NULL)
return NULL;
q->head = q->tail = NULL;
return q;
}
void osDestroyQueue(OSQueue* q)
{
if(q == NULL)
return;
while(osDequeue(q) != NULL);
free(q);
}
int osIsQueueEmpty(OSQueue* q)
{
return (q->tail == NULL && q->head == NULL);
}
void osEnqueue(OSQueue* q, void* data)
{
OSNode* node = malloc(sizeof(OSNode));
node->data = data;
node->next = NULL;
if(q->tail == NULL)
{
q->head=q->tail=node;
return;
}
q->tail->next = node;
q->tail = node;
}
void* osDequeue(OSQueue* q)
{
OSNode* previousHead;
void* data;
previousHead = q->head;
if(previousHead == NULL)
return NULL;
q->head = q->head->next;
if (q->head == NULL)
q->tail = NULL;
data = previousHead->data;
free(previousHead);
return data;
}