Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathCircularQueue.java
More file actions
Latest commit
138 lines (129 loc) · 4.1 KB
/
Copy pathCircularQueue.java
File metadata and controls
138 lines (129 loc) · 4.1 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
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
packagecom.thealgorithms.datastructures.queues;
/**
* The CircularQueue class represents a generic circular queue data structure that uses an array to
* store elements. This queue allows efficient utilization of space by wrapping around the array,
* thus avoiding the need to shift elements during enqueue and dequeue operations.
*
* <p>When the queue reaches its maximum capacity, further enqueues will raise an exception.
* Similarly, attempts to dequeue or peek from an empty queue will also result in an exception.
*
* <p>Reference: <a href="https://en.wikipedia.org/wiki/Circular_buffer">Circular Buffer</a>
*
* <p>Usage Example:
* <pre>
* CircularQueue<Integer> queue = new CircularQueue<>(3);
* queue.enQueue(1);
* queue.enQueue(2);
* queue.enQueue(3);
* queue.deQueue(); // Removes 1
* queue.enQueue(4); // Wraps around and places 4 at the position of removed 1
* </pre>
*
* @param <T> the type of elements in this queue
*/
publicclassCircularQueue<T> {
privateT[] array;
privateinttopOfQueue;
privateintbeginningOfQueue;
privatefinalintsize;
privateintcurrentSize;
/**
* Constructs a CircularQueue with a specified capacity.
*
* @param size the maximum number of elements this queue can hold
* @throws IllegalArgumentException if the size is less than 1
*/
@SuppressWarnings("unchecked")
publicCircularQueue(intsize) {
if (size < 1) {
thrownewIllegalArgumentException("Size must be greater than 0");
}
this.array = (T[]) newObject[size];
this.topOfQueue = -1;
this.beginningOfQueue = -1;
this.size = size;
this.currentSize = 0;
}
/**
* Checks if the queue is empty.
*
* @return {@code true} if the queue is empty; {@code false} otherwise
*/
publicbooleanisEmpty() {
returncurrentSize == 0;
}
/**
* Checks if the queue is full.
*
* @return {@code true} if the queue has reached its maximum capacity; {@code false} otherwise
*/
publicbooleanisFull() {
returncurrentSize == size;
}
/**
* Adds a new element to the queue. If the queue is full, an exception is thrown.
*
* @param value the element to be added to the queue
* @throws IllegalStateException if the queue is already full
*/
publicvoidenQueue(Tvalue) {
if (isFull()) {
thrownewIllegalStateException("Queue is full");
}
if (isEmpty()) {
beginningOfQueue = 0;
}
topOfQueue = (topOfQueue + 1) % size;
array[topOfQueue] = value;
currentSize++;
}
/**
* Removes and returns the element at the front of the queue.
*
* @return the element at the front of the queue
* @throws IllegalStateException if the queue is empty
*/
publicTdeQueue() {
if (isEmpty()) {
thrownewIllegalStateException("Queue is empty");
}
TremovedValue = array[beginningOfQueue];
array[beginningOfQueue] = null; // Optional: Nullify to help garbage collection
beginningOfQueue = (beginningOfQueue + 1) % size;
currentSize--;
if (isEmpty()) {
beginningOfQueue = -1;
topOfQueue = -1;
}
returnremovedValue;
}
/**
* Returns the element at the front of the queue without removing it.
*
* @return the element at the front of the queue
* @throws IllegalStateException if the queue is empty
*/
publicTpeek() {
if (isEmpty()) {
thrownewIllegalStateException("Queue is empty");
}
returnarray[beginningOfQueue];
}
/**
* Deletes the entire queue by resetting all elements and pointers.
*/
publicvoiddeleteQueue() {
array = null;
beginningOfQueue = -1;
topOfQueue = -1;
currentSize = 0;
}
/**
* Returns the current number of elements in the queue.
*
* @return the number of elements currently in the queue
*/
publicintsize() {
returncurrentSize;
}
}