This repository was archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.java
More file actions
151 lines (135 loc) · 3.24 KB
/
Copy pathArrayQueue.java
File metadata and controls
151 lines (135 loc) · 3.24 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
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.lang.NullPointerException;
/**
* Simplified ArrayQueue data structure which implements Queue<T>
* @author Hunter M. Shaw
*/
public class ArrayQueue<T> implements Queue<T> {
private Object [] queue;
private int size;
private int front;
private int back;
private int arrayCapacity;
public static final int DEFAULT_CAPACITY = 25;
public ArrayQueue() {
this(DEFAULT_CAPACITY);
}
public ArrayQueue(int arrayCapacity) {
this.arrayCapacity = arrayCapacity;
queue = new Object[arrayCapacity];
}
@Override
public boolean add(T obj) {
if (size == arrayCapacity-1) {
resizeArray(arrayCapacity*2);
}
queue[back] = obj;
back = (back + 1)%arrayCapacity;
size++;
return true;
}
@Override
public boolean offer(T obj) {
try {
return add(obj);
} catch (Exception ex) {
return false;
}
}
@SuppressWarnings("unchecked")
@Override
public T remove() {
if (size == 0) {
throw new NoSuchElementException("Empty queue");
}
Object o = queue[front];
front = (front + 1)%arrayCapacity;
size--;
return (T)o;
}
@Override
public T poll() {
try {
return remove();
} catch (NoSuchElementException ex) {
return null;
}
}
@SuppressWarnings("unchecked")
@Override
public T element() {
if (isEmpty()) {
throw new NoSuchElementException("Empty queue");
}
return (T)queue[front];
}
@Override
public T peek() {
try {
return element();
} catch (NoSuchElementException ex) {
return null;
}
}
@Override
public boolean contains(Object obj) {
boolean found = false;
int index = 0;
if (isEmpty()) {
return found;
}
while (index <= size && !found) {
if (queue[index].equals(obj)) {
found = true;
}
index++;
}
return found;
}
@Override
public int size() {
return size;
}
@Override
public void clear() {
size = 0;
front = 0;
back = 0;
queue = new Object[arrayCapacity];
}
@Override
public boolean isEmpty() {
return size == 0;
}
private void resizeArray(int newCapacity) {
Object [] arr = new Object[newCapacity];
int index = front;
int i = 0;
while (index != back) {
arr[i] = queue[index];
i++;
index = (index + 1)%arrayCapacity;
}
front = 0;
back = size;
arrayCapacity = newCapacity;
}
@Override
public Object[] toArray() {
Object [] arr = new Object[size];
int index = front;
int i = 0;
while (index != back) {
arr[i] = queue[index];
i++;
index = (index + 1)%arrayCapacity;
}
return arr;
}
@SuppressWarnings("unchecked")
@Override
public T[] toArray(T[] a) {
return (T[])Arrays.copyOf(this.toArray(), size, a.getClass());
}
}