-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
56 lines (49 loc) · 998 Bytes
/
Copy pathQueue.cpp
File metadata and controls
56 lines (49 loc) · 998 Bytes
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
#include "Queue.h"
void Queue::resize() {
int newCapacity = capacity * 2;
int* newArr = new int[newCapacity];
for (int i = 0; i < length; i++) {
newArr[i] = arr[(front + i) % capacity];
}
delete[] arr;
arr = newArr;
capacity = newCapacity;
front = 0;
rear = length - 1;
}
Queue::Queue(int initialCapacity) {
arr = new int[initialCapacity];
capacity = initialCapacity;
length = 0;
front = 0;
rear = -1;
}
Queue::~Queue() {
delete[] arr;
}
void Queue::enquque(int x) {
if (length == capacity) {
resize();
}
rear = (rear + 1) % capacity;
arr[rear] = x;
length++;
}
void Queue::dequeue() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return;
}
front = (front + 1) % capacity;
length--;
}
int Queue::Front() {
if (isEmpty()) {
cout << "Queue is empty\n";
return 0;
}
return arr[front];
}
bool Queue::isEmpty() {
return length == 0;
}