-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplement_Circular_queue.cpp
More file actions
95 lines (85 loc) · 1.84 KB
/
Implement_Circular_queue.cpp
File metadata and controls
95 lines (85 loc) · 1.84 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
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
class QueueArray
{
const int SIZE = 4;
int front = -1;
int rear = -1;
int *queue;
public:
QueueArray()
{
queue = new int[SIZE];
}
void enQueue(int data);
int deQueue();
};
// class QueueArray{
// const int SIZE = 4; // Size of queue array
// int front = -1; // front variable
// int rear = -1; // rear variable
// int *queue; // queue array
// public:
// QueueArray() // constructor
// void enQueue(int data); // add data to the queue
// int deQueue(); // remove data from the queue
// };
// The above declaration is already done. Complete the function given below.
// Add data to the circular queue
void QueueArray::enQueue(int data) {
// Write your code here
if ((front == 0 && rear == SIZE - 1) || (front == rear + 1))
return;
else {
if (front == -1)
front++;
rear = (rear + 1) % SIZE;
queue[rear] = data;
}
}
// Remove First element from queue
int QueueArray::deQueue() {
int x;
if (front == -1)
return -1;
else {
x = queue[front];
if (front == rear) {
front = -1;
rear = -1;
}
else
front = (front + 1) % SIZE;
return x;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
QueueArray *queue = new QueueArray();
while (n--)
{
int choice;
cin >> choice;
if (choice == 1)
{
int data;
cin >> data;
queue->enQueue(data);
}
else if (choice == 2)
{
cout << queue->deQueue() << ' ';
}
}
cout << endl;
}
return 0;
}