-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
57 lines (49 loc) Β· 1.01 KB
/
Queue.h
File metadata and controls
57 lines (49 loc) Β· 1.01 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
#include <stdlib.h>
struct Queue{
int size;
int front;
int rear;
int *Q;
};
struct Queue createQueue(int size) {
struct Queue q;
q.size = size;
q.Q = (int *)malloc(q.size * sizeof(int));
q.front = q.rear = -1;
return q;
}
void enqueue(struct Queue *q , int data){
if(q->rear == q->size-1){
printf("Queue is full.");
}else{
q->rear++;
q->Q[q->rear] = data;
}
}
int dequeue(struct Queue *q){
int x = -1;
if(q->front == q->rear){
printf("Queue is empty.");
}else{
q->front++;
x = q->Q[q->front];
}
return x;
}
int isEmpty(struct Queue *q){
return (q->front == q->rear)? 1 : 0;
}
void display(struct Queue *q) {
if (q->front == q->rear) {
printf("Queue is empty\n");
return;
}
// Use a temporary index to iterate through the queue
int i = q->front + 1;
printf("Queue elements: ");
while (i <= q->rear) {
printf("%d ", q->Q[i]);
i++;
}
printf("\n");
}