-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
43 lines (35 loc) · 1006 Bytes
/
queue.c
File metadata and controls
43 lines (35 loc) · 1006 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
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = queue->size = 0;
queue->rear = QUEUE_SIZE - 1;
queue->array = (char**)malloc(QUEUE_SIZE * sizeof(char*));
return queue;
}
int isFull(Queue* queue) {
return (queue->size == QUEUE_SIZE);
}
int isEmpty(Queue* queue) {
return (queue->size == 0);
}
void enqueue(Queue* queue, char* item) {
if (isFull(queue))
return;
queue->rear = (queue->rear + 1) % QUEUE_SIZE;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
}
// gotta make sure i run free() on the char* returned
char* dequeue(Queue* queue) {
if (isEmpty(queue))
return NULL;
char* item = queue->array[queue->front];
queue->front = (queue->front + 1) % QUEUE_SIZE;
queue->size = queue->size - 1;
return item;
}
// Queue* queue = createQueue();
// char* item1 = (char*)malloc(ARRAY_SIZE * sizeof(char));
// char* dequeuedItem1 = dequeue(queue);