-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
121 lines (97 loc) · 2.87 KB
/
Copy pathqueue.c
File metadata and controls
121 lines (97 loc) · 2.87 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Recent Changes:
- Renamed `initialize()` to `initializeQueue()`
- Renamed `display()` to `displayQueue(Queue *q)`
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−*/
/*
Includes definitions:
- typedef struct Queue {...} Queue;
- Queue* initializeQueue();
- int isFull(Queue *q);
- int isEmpty(Queue *q);
- int enqueue(Queue *q, int value);
- int dequeue(Queue *q);
- void display(Queue *q);
*/
// Standard Libraries
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Some Global Definitions
#define MAX 100
// Definition of Queue
typedef struct Queue {
int array[MAX];
int front, rear;
} Queue;
/* Allocates memory and initializes a `Queue` and returns its memory address. */
Queue* initializeQueue() {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = -1;
q->rear = -1;
return q;
}
/*
Checks if the Queue is full or not. Returns `1` if full, else returns `0`
It takes the following parameters:
- q (`Queue *`) : Pointer of the Queue
*/
int isFull(Queue *q) {
return ((q->rear + 1) % MAX == q->front);
}
/*
Checks if the Queue is empty or not. Returns `1` if empty, else returns `0`
It takes the following parameters:
- q (`Queue *`) : Pointer of the Queue.
*/
int isEmpty(Queue *q) {
return (q->front == -1);
}
/*
Inserts a new value at the end of the queue. Returns `0` if successful, else returns `-1`
It takes the following parameters:
- q (`Queue *`) : Pointer of the Queue.
*/
int enqueue(Queue *q, int value) {
if (isFull(q)) return -1;
if (isEmpty(q)) q->front = 0;
q->rear = (q->rear + 1) % MAX;
q->array[q->rear] = value;
return 0;
}
/*
Deletes value from the front of the queue. Returns the value it deleted if
successful, else returns `INT_MIN` from `<limits.h>`.
It takes the following parameters:
- q (`Queue *`) : Pointer of the Queue.
*/
int dequeue(Queue *q) {
if (isEmpty(q)) return INT_MIN;
int value = q->array[q->front];
if (q->front == q->rear) { // If last element is dequeued
q->front = -1;
q->rear = -1;
} else {
q->front = (q->front + 1) % MAX;
}
return value;
}
/*
prints the Queue in an array like fashion.
It takes the following parameters:
- q (`Queue *`) : Pointer of the Queue.
*/
void displayQueue(Queue *q) {
if (isEmpty(q)) {
printf("[]\n");
return;
}
printf("[");
int i = q->front;
while (1) {
printf("%d, ", q->array[i]);
if (i == q->rear) break;
i = (i + 1) % MAX;
}
printf("]\n");
}