-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueArray.java
More file actions
60 lines (45 loc) · 1.53 KB
/
QueueArray.java
File metadata and controls
60 lines (45 loc) · 1.53 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
public class QueueArray {
private long queueArray[];
private int capacity, numItems, front, back;
/*
* Space Complexity - O(k) where k = size of the array
* Time Complexity - O(1) for Enqueue, Dequeue, Peek, isEmpty, and isFull
* This implementation of Dequeue does not remove the value and shift all array elements
* It will shift the front pointer (so the value can't be seen) and overwrite in the next pass
*/
public QueueArray(int capacity){
this.capacity = capacity;
this.queueArray = new long[capacity];
numItems = 0;
front = 0;
back = -1;
}
public void enqueue(long data){
//if the queue becomes full, then begin overwriting elements in the front (wraparound)
if(back == capacity){
back = -1;
}
queueArray[++back] = data;
numItems++;
}
public long dequeue(){
numItems--;
//get the value at the front being removed, then increase the position of the queue's front
long displayData = queueArray[front++];
//the front pointer was moved all the way to the end.
// To prevent going out of bounds, shift the front back to the first index of the array
if(front == capacity){
front = 0;
}
return displayData;
}
public long peek(){
return queueArray[front];
}
public Boolean isEmpty(){
return numItems == 0;
}
public boolean isFull(){
return numItems == capacity;
}
}