-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.c
41 lines (34 loc) · 830 Bytes
/
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
#include "Queue.h"
void QueueInitialize(Queue* _queue)
{
StackInitialize(&_queue->first);
StackInitialize(&_queue->second);
_queue->size = 0;
}
bool QueueEmpty(const Queue* _queue)
{
return StackEmpty(&_queue->first) && StackEmpty(&_queue->second);
}
voidPtr_t QueueDequeue(Queue* _queue)
{
if(QueueEmpty(_queue)) return NULL;
if(StackEmpty(&_queue->second))
{
while(!StackEmpty(&_queue->first))
{
StackPush(&_queue->second, StackPop(&_queue->first));
}
}
_queue->size--;
return StackPop(&_queue->second);
}
void QueueEnqueue(Queue* _queue, voidPtr_t _value)
{
StackPush(&_queue->first, _value);
_queue->size = _queue->first.size + _queue->second.size;
}
void QueueDestroy(const Queue* _queue)
{
StackDestroy(&_queue->first);
StackDestroy(&_queue->second);
}