zeroSquitto – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /*************************************************************************/ |
2 | /* Circular queue implementation. */ |
||
3 | /*************************************************************************/ |
||
4 | |||
5 | #include "circularQueue.h" |
||
6 | #include <stdio.h> |
||
7 | #include <stdlib.h> |
||
8 | #include <string.h> |
||
9 | |||
10 | /*************************************************************************/ |
||
11 | /* Constructor with given size. */ |
||
12 | /*************************************************************************/ |
||
13 | CircularQueue *queueCreate(int size) { |
||
14 | CircularQueue *q = (CircularQueue *)calloc(1, sizeof(CircularQueue)); |
||
15 | if ((q->store = (char **)calloc(size, sizeof(char *))) == NULL) |
||
16 | return NULL; |
||
17 | q->size = size; |
||
18 | q->head = 0; |
||
19 | q->tail = 0; |
||
20 | return q; |
||
21 | } |
||
22 | |||
23 | /*************************************************************************/ |
||
24 | /* Enqueue an item to the queue. */ |
||
25 | /*************************************************************************/ |
||
26 | void queueEnqueue(CircularQueue *q, char *e) { |
||
27 | if (q->tail > q->size - 1) |
||
28 | q->store = (char **)realloc(q->store, ++q->size * sizeof(char *)); |
||
29 | q->store[q->tail] = (char *)calloc(strlen(e) + 1, sizeof(char)); |
||
30 | memcpy(q->store[q->tail], e, strlen(e) + 1); |
||
31 | ++q->tail; |
||
32 | } |
||
33 | |||
34 | /*************************************************************************/ |
||
35 | /* Dequeue an item off the queue. */ |
||
36 | /*************************************************************************/ |
||
37 | char *queueDequeue(CircularQueue *q) { |
||
38 | char *e; |
||
39 | if (queueIsEmpty(q)) |
||
40 | return NULL; |
||
41 | e = (char *)calloc(strlen(q->store[q->head]) + 1, sizeof(char)); |
||
42 | memcpy(e, q->store[q->head], strlen(q->store[q->head]) + 1); |
||
43 | queueEnqueue(q, e); |
||
44 | ++q->head; |
||
45 | return e; |
||
46 | } |
||
47 | |||
48 | /*************************************************************************/ |
||
49 | /* Clears the queue. */ |
||
50 | /*************************************************************************/ |
||
51 | CircularQueue *queueClear(CircularQueue *q) { |
||
52 | if (q != NULL) |
||
53 | free(q); |
||
54 | return queueCreate(1); |
||
55 | } |
||
56 | |||
57 | /*************************************************************************/ |
||
58 | /* Prints the items in the queue. */ |
||
59 | /*************************************************************************/ |
||
60 | void queuePrint(CircularQueue *q) { |
||
61 | int i; |
||
62 | if (queueIsEmpty(q)) { |
||
63 | printf("Queue is empty.\n"); |
||
64 | return; |
||
65 | } |
||
66 | printf("Items: "); |
||
67 | i = q->head; |
||
68 | do { |
||
69 | printf("%s ", q->store[i]); |
||
70 | } while (++i < q->tail); |
||
71 | printf("\n"); |
||
72 | } |
||
73 |