-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.h
More file actions
64 lines (53 loc) · 1.18 KB
/
queue.h
File metadata and controls
64 lines (53 loc) · 1.18 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
59
60
61
62
63
64
#include <stdbool.h>
#include <stdio.h>
#define NUM_FRAMES 8
#define MAX_SIZE NUM_FRAMES
// Defining the Queue structure
typedef struct {
int items[MAX_SIZE];
int front;
int rear;
int size;
} Queue;
void initializeQueue(Queue *q) {
q->front = 0;
q->rear = -1;
q->size = 0;
}
bool isEmpty(Queue *q) {
return (q->size == 0);
}
bool isFull(Queue *q) {
return (q->size == MAX_SIZE);
}
void enqueue(Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full\n");
return;
}
q->rear = (q->rear + 1) % MAX_SIZE;
q->items[q->rear] = value;
q->size++;
}
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
int value = q->items[q->front];
q->front = (q->front + 1) % MAX_SIZE;
q->size--;
return value;
}
void printQueue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
}
printf("Current Queue: ");
for (int i = 0; i < q->size; i++) {
int index = (q->front + i) % MAX_SIZE;
printf("%d ", q->items[index]);
}
printf("\n");
}