-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularQueue.cpp
More file actions
84 lines (77 loc) · 1.48 KB
/
Copy pathcircularQueue.cpp
File metadata and controls
84 lines (77 loc) · 1.48 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
class circularQueue
{
int *data;
int nextIndex;
int firstIndex;
int size;
int capacity;
public:
circularQueue(int s)
{
data = new int[s];
nextIndex = 0;
firstIndex = -1;
size = 0;
capacity = s;
}
int getSize()
{
return size;
}
bool isEmpty()
{
return size == 0;
}
// insert elements in the queue
void enqueue(int element)
{
if (size == capacity)
{
cout << "Queue is Full " << endl;
return;
}
data[nextIndex] = element;
nextIndex = (nextIndex + 1) % capacity;
if (firstIndex == -1)
{
firstIndex = 0;
}
size++;
}
int front()
{
if (isEmpty())
{
cout << "Queue is empty" << endl;
return 0;
}
return data[firstIndex];
}
int dequeue()
{
if (isEmpty())
{
cout << "Queue is empty" << endl;
return 0;
}
int ans = data[firstIndex];
firstIndex = (firstIndex + 1) % capacity;
size--;
if (size == 0)
{
firstIndex = -1;
nextIndex = 0;
}
return ans;
}
};
int main()
{
circularQueue c(20);
c.enqueue(3);
c.enqueue(5);
c.enqueue(7);
c.enqueue(9);
c.dequeue();
return 0;
}