-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.c
executable file
·136 lines (114 loc) · 3.41 KB
/
Queue.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Queue.h"
typedef struct QueueNode
{
void *data;
struct QueueNode *next;
} Node;
//------------------------------------------------------------------------------
// Returns by reference the data at the front of the queue, and removes the
// front node from the queue.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int dequeue(Queue *q, void *data)
{
if (q->size > 0)
{
memcpy(data, q->head->data, q->memSize);
Node *temp = q->head->next;
free(q->head->data);
free(q->head);
if (q->size > 1)
q->head = temp;
else
q->head = q->tail = NULL;
q->size--;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Adds the given data to the back of the queue.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int enqueue(Queue *q, const void *data)
{
if (q->memSize > 0)
{
Node *node = (Node *) malloc(sizeof(Node));
if (node == NULL)
return -1;
node->data = malloc(q->memSize);
if (node->data == NULL)
{
free(node);
return -1;
}
node->next = NULL;
memcpy(node->data, data, q->memSize);
if (q->size == 0)
q->head = q->tail = node;
else
q->tail = q->tail->next = node;
q->size++;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Frees all the nodes and data. Head and tail point to NULL, size is reset to
// zero, but memSize is kept intact.
//------------------------------------------------------------------------------
void queueClear(Queue *q)
{
while(q->size > 0)
{
Node *temp = q->head;
q->head = q->head->next;
free(temp->data);
free(temp);
q->size--;
}
q->head = q->tail = NULL;
}
//------------------------------------------------------------------------------
// Returns 1 if the queue is empty, 0 otherwise.
//------------------------------------------------------------------------------
int queueEmpty(const Queue *q)
{
return (q->size == 0);
}
//------------------------------------------------------------------------------
// Initializes the queue.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int queueInit(Queue *q, const size_t memSize)
{
if (memSize > 0)
{
q->memSize = memSize;
q->size = 0;
q->head = q->tail = NULL;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Returns by reference the data at the front of the queue.
// Returns 0 on success, -1 on failure.
//------------------------------------------------------------------------------
int queuePeek(const Queue *q, void *data)
{
if (q->size > 0)
{
memcpy(data, q->head->data, q->memSize);
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
// Returns the size of the queue.
//------------------------------------------------------------------------------
size_t queueSize(const Queue *q)
{
return q->size;
}