-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularQueue.c
112 lines (101 loc) · 3.1 KB
/
circularQueue.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
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
#include<stdio.h>
#include<stdlib.h>
// Structure to represent a circular queue
struct queue {
int* a; // Array to store elements of the queue
int front; // Front of the queue
int rear; // Rear of the queue
} q;
int size; // Size of the circular queue
// Function to enqueue an element into the circular queue
void enqueue() {
int item, i;
printf("Enter element to enqueue: ");
scanf("%d", &item);
// Check if the circular queue is full and reallocate memory if necessary
if ((q.rear + 1) % size == q.front) {
printf("Circular queue is full, reallocating memory...\n");
size++;
q.a = (int*)realloc(q.a, size * sizeof(int));
}
// If circular queue is empty, set front and rear to 0 and insert the element
if (q.front == -1) {
q.front = q.rear = 0;
*(q.a + q.rear) = item;
return;
}
// Increment rear in a circular manner and insert the element
q.rear = (q.rear + 1) % size;
*(q.a + q.rear) = item;
}
// Function to dequeue an element from the circular queue
void dequeue() {
// Check if the circular queue is empty
if (q.front == -1) {
printf("Circular queue is empty\n");
return;
}
// Print the dequeued element and update front
printf("Dequeued element: %d\n", *(q.a + q.front));
// If circular queue has only one element, set front and rear to -1
if (q.front == q.rear) {
q.front = q.rear = -1;
} else {
// Increment front in a circular manner
q.front = (q.front + 1) % size;
}
}
// Function to display the elements of the circular queue
void display() {
// Check if the circular queue is empty
if (q.front == -1) {
printf("Circular queue is empty\n");
return;
}
// Print the elements of the circular queue
printf("Circular queue elements: ");
if (q.front <= q.rear) {
for (int i = q.front; i <= q.rear; i++) {
printf("%d ", *(q.a + i));
}
} else {
for (int i = q.front; i < size; i++) {
printf("%d ", *(q.a + i));
}
for (int i = 0; i <= q.rear; i++) {
printf("%d ", *(q.a + i));
}
}
printf("\n");
}
// Main function
void main() {
q.front = q.rear = -1;
int choice;
printf("Enter the size of the circular queue: ");
scanf("%d", &size);
q.a = (int*)malloc(size * sizeof(int));
printf("Menu:\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
enqueue();
display();
break;
case 2:
dequeue();
display();
break;
case 3:
display();
break;
case 4:
printf("Exiting the program\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
}