forked from Adel-Nasim/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular Queue.txt
127 lines (111 loc) · 1.72 KB
/
Circular Queue.txt
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
#include <iostream>
#include <cassert>
using namespace std;
class arrayQueueType
{
int rear;
int front;
int length;
int *arr;
int maxSize;
public:
arrayQueueType(int size) {
if (size <= 0)
maxSize = 100;
else
maxSize = size;
front = 0;
arr = new int[maxSize];
rear = maxSize - 1;
length = 0;
}
int isEmpty()
{
return length == 0;
}
bool isFull()
{
return length == maxSize;
}
void addQueue(int Element)
{
if (isFull())
{
cout << "Queue Full Can't Enqueue ...!";
}
else
{
rear = (rear + 1) % maxSize;
arr[rear] = Element;
length++;
}
}
void deleteQueue()
{
if (isEmpty())
{
cout << "Empty Queue Can't Dequeue ...!";
}
else
{
front = (front + 1) % maxSize;
--length;
}
}
int frontQueue()
{
assert(!isEmpty());
return arr[front];
}
int rearQueue()
{
assert(!isEmpty());
return arr[rear];
}
void printQueue()
{
if (!isEmpty()) {
for (size_t i = front; i != rear; i = (i + 1) % maxSize)
{
cout << arr[i] << " ";
}
cout << arr[rear];
}
else
cout << "Empty Queue";
}
int queueSearch(int element) {
int pos = -1;
if (!isEmpty())
{
for (int i = front; i != rear; i = (i + 1) % maxSize)
if (arr[i] == element)
{
pos = i;
break;
}
if (pos == -1)
{
if (arr[rear] == element)
pos = rear;
}
}
else
cout << "Q is empty ! " << endl;
return pos;
}
~arrayQueueType() {
delete[]arr;
}
};
int main()
{
arrayQueueType q1(5);
q1.addQueue(10);
q1.addQueue(20);
q1.addQueue(30);
q1.addQueue(40);
q1.addQueue(50);
q1.printQueue();
return 0;
}