-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue Data structure.c
62 lines (49 loc) · 1.19 KB
/
Queue Data structure.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Queue Data structure implementation
Date created: Saturday; February 01, 2023 */
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct queue {
int items[MAX_SIZE];
int front, rear;
} Queue;
void initialize(Queue *queue) {
queue->front = 0;
queue->rear = -1;
}
int isEmpty(Queue *queue) {
return queue->rear == -1;
}
int isFull(Queue *queue) {
return queue->rear == MAX_SIZE - 1;
}
void enqueue(Queue *queue, int item) {
if (isFull(queue)) {
printf("Queue is full.\n");
return;
}
queue->items[++queue->rear] = item;
printf("%d enqueued to queue\n", item);
}
int dequeue(Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return -1;
}
int item = queue->items[queue->front++];
if (queue->front > queue->rear) {
initialize(queue);
}
return item;
}
int main() {
Queue queue;
initialize(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("%d dequeued from queue\n", dequeue(&queue));
printf("%d dequeued from queue\n", dequeue(&queue));
printf("%d dequeued from queue\n", dequeue(&queue));
return 0;
}