-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular_Queue.cpp
149 lines (116 loc) · 2.38 KB
/
Circular_Queue.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
#include<iostream>
using namespace std;
class Queue
{
private:
int MAXIMUM_NUMBER_OF_ITEMS{5};
int beginningIndex = -1;
int currentIndex = -1;
int mainQueue[5]{};
public:
Queue() = default;
void enqueue(int element);
int dequeue();
void moveLeft();
bool isEmpty();
bool isFull();
int peek();
int count();
//void change(int element, int index);
void display();
};
bool Queue::isFull()
{
return((beginningIndex == 0) && (currentIndex == MAXIMUM_NUMBER_OF_ITEMS - 1));
}
bool Queue::isEmpty()
{
return((beginningIndex == -1) && (currentIndex == -1));
}
int Queue::peek()
{
return mainQueue[currentIndex];
}
int Queue::count()
{
return(currentIndex - beginningIndex + 1);
}
void Queue::display()
{
if(isEmpty())
{
cout << "Queue is empty\n";
return;
}
cout << "Front -> ";
for(int i = 0; i < MAXIMUM_NUMBER_OF_ITEMS; i++)
{
cout << mainQueue[i] << " -> ";
}
cout << "Rear\n";
}
void Queue::enqueue(int element)
{
if(isFull())
{
cout << "Queue is Full\n";
return;
}
if(beginningIndex == -1)
{
++beginningIndex;
}
currentIndex = (currentIndex + 1) % MAXIMUM_NUMBER_OF_ITEMS;
mainQueue[currentIndex] = element;
}
/*
void Queue::moveLeft()
{
for(int i = 1; i <= currentIndex; i++)
{
mainQueue[i-1] = mainQueue[i];
}
if(beginningIndex == currentIndex)
{
--beginningIndex;
}
--currentIndex;
}
*/
int Queue::dequeue()
{
if(isEmpty())
{
cout << "Queue is Empty\n";
}
int temp = mainQueue[beginningIndex];
mainQueue[beginningIndex] = 0;
beginningIndex = (beginningIndex+1) % MAXIMUM_NUMBER_OF_ITEMS;
return temp;
}
/* void Queue::change(int element, int index)
{
if((index >= 0) && (index < MAXIMUM_NUMBER_OF_ITEMS))
{
mainQueue[index] = element;
return;
}
cout << "Please Input Valid Index\n";
return;
} */
int main()
{
Queue S;
S.display();
S.enqueue(1);
S.enqueue(2);
S.enqueue(3);
S.display();
S.dequeue();
S.display();
S.dequeue();
S.display();
S.dequeue();
S.display();
return 0;
}