-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
58 lines (48 loc) · 1.11 KB
/
queue.c
File metadata and controls
58 lines (48 loc) · 1.11 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
// Copy string (internal function)
// The caller is responsible for freeing the returned pointer.
static unsigned char *clone_string(const unsigned char *in)
{
size_t len = strlen((char*)in);
unsigned char *out = calloc(len + 1, sizeof(unsigned char));
strcpy((char*)out, (char*)in);
return out;
}
Queue *queue_create(void)
{
Queue *que = malloc(sizeof(Queue));
que -> list = list_create();
que->size = 0;
return que;
}
void queue_destroy(Queue *q)
{
list_destroy(q -> list);
free (q);
}
// Enqueues values from the end of the list.
void queue_enqueue(Queue *q, const unsigned char *value)
{
list_insert(list_end(q -> list) , value);
q->size ++;
}
// Dequeues values from the first list position.
unsigned char *queue_dequeue(Queue *q)
{
ListPos first = list_first(q -> list);
unsigned char *str = clone_string(first.node -> value);
list_remove(first);
q->size--;
return str;
}
bool queue_is_empty(const Queue *q)
{
return list_is_empty(q -> list);
}
int queue_size(const Queue *q)
{
return q->size;
}