-
Notifications
You must be signed in to change notification settings - Fork 1
/
026_Circular_Queue_Properties.c
164 lines (132 loc) · 5.3 KB
/
026_Circular_Queue_Properties.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Question : Write a program to implement circular queue (Push, Pop, isEmpty, isFull, Display);
=======================================================================
Algorithm :
--->Create a queue structure with front, rear, capacity and array as members.
--->Create a function to create a queue of given capacity, allocate memory for the queue, initialize front and rear of the queue and set the capacity of the queue.
--->Create a function to check if the queue is full, by comparing the rear+1 and front of the queue.
--->Create a function to check if the queue is empty, by checking if the front pointer is -1.
--->Create a function to insert an element to the queue, check if the queue is full, if not, increment rear pointer, add the item to the array and set the
front pointer if it is -1.
--->Create a function to remove an element from the queue, check if the queue is empty, if not, store the value of the front element in the variable 'item',
set the front pointer to the next element and return the value stored in 'item'
--->Create a function to display the elements of the queue, check if the queue is empty, if not, print the elements from front to rear.
--->In the main function, create a queue of given capacity, use a while loop and switch case to handle the users choice of operation, call the appropriate
function accordingly;
Code Below ::::
------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
// define a queue structure
typedef struct queue {
int front, rear;
int capacity;
int* array;
} Queue;
// function to create a queue of given capacity
Queue* createQueue(int capacity) {
// allocating memory for the queue
Queue* queue = (Queue*)malloc(sizeof(Queue));
// initializing the front and rear of the queue
queue->front = queue->rear = -1;
// setting the capacity of the queue
queue->capacity = capacity;
// allocating memory for the array of the queue
queue->array = (int*)malloc(queue->capacity * sizeof(int));
return queue;
}
// function to check if the queue is full
int isFull(Queue* queue) {
//checking the condition of full queue
return (queue->rear + 1) % queue->capacity == queue->front;
}
// function to check if the queue is empty
int isEmpty(Queue* queue) {
// checking the condition of empty queue
return queue->front == -1;
}
// function to insert an element to the queue
void enqueue(Queue* queue, int item) {
if (isFull(queue)) {
// if queue is full, print "Queue is full"
printf("Queue is full\n");
return;
}
//incrementing the rear of the queue and mod it with capacity to wrap around if necessary
queue->rear = (queue->rear + 1) % queue->capacity;
// adding the item to the array of the queue
queue->array[queue->rear] = item;
if (queue->front == -1)
// setting the front of the queue
queue->front = queue->rear;
}
// function to remove an element from the queue
int dequeue(Queue* queue) {
if (isEmpty(queue)) {
// if queue is empty, print "Queue is empty"
printf("Queue is empty\n");
return -1;
}
// store the value of the front element in the variable 'item'
int item = queue->array[queue->front];
if (queue->front == queue->rear)
// setting the front and rear pointers to -1 to indicate an empty queue
queue->front = queue->rear = -1;
else
// increment the front pointer by 1 and mod it with capacity to wrap around if necessary
queue->front = (queue->front + 1) % queue->capacity;
return item;
}
// function to display the elements of the queue
void display(Queue* queue) {
if (isEmpty(queue)) {
// if queue is empty, print "Queue is empty"
printf("Queue is empty\n");
return;
}
printf("Elements in the queue: ");
int i;
if (queue->rear >= queue->front) {
// if rear pointer is greater than front pointer, loop through the queue from front to rear
for (i = queue->front; i <= queue->rear; i++)
printf("%d ", queue->array[i]);
} else {
// if rear pointer is lesser than front pointer, loop through the queue from front to capacity
for (i = queue->front; i < queue->capacity; i++)
printf("%d ", queue->array[i]);
// then loop through the queue from 0 to rear
for (i = 0; i <= queue->rear; i++)
printf("%d ", queue->array[i]);
}
printf("\n");
}
int main() {
// create a queue of capacity 5
Queue* queue = createQueue(5);
int choice, item;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to be inserted: ");
scanf("%d", &item);
enqueue(queue, item);
break;
case 2:
item = dequeue(queue);
if (item != -1)
printf("Dequeued item: %d\n", item);
break;
case 3:
display(queue);
break;
case 4:
exit(0);
}
}
return 0;
}