-
Notifications
You must be signed in to change notification settings - Fork 0
/
6circularqueue_array.cpp
161 lines (145 loc) · 3.54 KB
/
6circularqueue_array.cpp
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
160
161
// 6. Implement Circular Queue ADT using array.
// Theory:
// This code implements a queue data structure using an array with
// circular buffering to optimize space usage. It provides
// functionalities to enqueue elements into the queue, dequeue
// elements from the queue, and display the elements currently in the
// queue. The circular buffering technique ensures efficient
// utilization of the array.
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Queue
{
private:
int front, rear;
int arr[MAX_SIZE];
public:
Queue()
{
front = rear = -1;
}
bool isEmpty()
{
return (front == -1);
}
bool isFull()
{
return ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1));
}
void enqueue(int data)
{
if (isFull())
{
cout << "queue is full" << endl;
return;
}
else if (isEmpty())
{
front = rear = 0;
}
else if (rear == MAX_SIZE - 1)
{
rear = 0;
}
else
{
rear++;
}
arr[rear] = data;
cout << data << " enqueued to queue" << endl;
}
void dequeue()
{
if (isEmpty())
{
cout << "queue is empty" << endl;
return;
}
else if (front == rear)
{
cout << arr[front] << " dequeued from queue" << endl;
front = rear = -1;
}
else if (front == MAX_SIZE - 1)
{
cout << arr[front] << " dequeued from queue" << endl;
front = 0;
}
else
{
cout << arr[front] << " dequeued from queue" << endl;
front++;
}
}
void display()
{
if (isEmpty())
{
cout << "queue is empty" << endl;
return;
}
cout << "elements in queue: ";
if (rear >= front)
{
for (int i = front; i <= rear; i++)
{
cout << arr[i] << " ";
}
}
else
{
for (int i = front; i < MAX_SIZE; i++)
{
cout << arr[i] << " ";
}
for (int i = 0; i <= rear; i++)
{
cout << arr[i] << " ";
}
}
cout << endl;
}
};
int main()
{
Queue q;
int choice, data;
do
{
cout << "\n1. enqueue\n";
cout << "2. dequeue\n";
cout << "3. display\n";
cout << "4. exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "enter elements to enqueue: ";
cin >> data;
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
cout << "exited!" << endl;
break;
default:
cout << "invalid choice" << endl;
}
}
while (choice != 4);
return 0;
}
// Conclusion:
// The code offers an optimized implementation of a queue using an
// array with circular buffering, providing essential operations such
// as enqueue, dequeue, and display. It offers a user-friendly menu
// interface for interacting with the queue, allowing users to
// enqueue elements, dequeue elements, and view the elements currently
// in the queue.