-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneriacArrayQueue.java
More file actions
89 lines (75 loc) · 2.31 KB
/
Copy pathGeneriacArrayQueue.java
File metadata and controls
89 lines (75 loc) · 2.31 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
public class GenericArrayQueue<T> implements QueueInterface<T>
{
private T[] data;
private int front, size, back;
public final static int DEFAULT_INITIAL_ARRAY_SIZE = 16;
public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
public GenericArrayQueue() {
this(DEFAULT_INITIAL_ARRAY_SIZE);
}
public GenericArrayQueue(int capacity) throws IllegalArgumentException {
if (capacity < 1) {
throw new IllegalArgumentException("Queue capacity must be 1 or greater");
}
if (capacity > MAX_ARRAY_SIZE) {
throw new IllegalArgumentException("Stack capacity is greater then maximum array size");
}
T[] tempData = (T[]) new Object[capacity];
data = tempData;
}
public QueueInterface<T> add(T e) throws IllegalArgumentException {
if (size == data.length) {
throw new IllegalArgumentException("Ran out of memory to queue");
}
data[back] = e;
back = (back + 1) % data.length;
size++;
return this;
}
public T element() throws NoSuchElementException {
if (size == 0) {
throw new NoSuchElementException("Queue does not contain any items.");
}
return data[front];
}
public T remove() throws NoSuchElementException {
if (size == 0) {
throw new NoSuchElementException("Queue does not contain any items.");
}
size--;
T output = data[front];
data[front] = null;
front = (front + 1) % data.length;
return output;
}
public boolean offer(T e) {
if (size == data.length) {
return false;
}
size++;
data[back] = e;
back = (back + 1) % data.length;
return true;
}
public T peek() {
return size == 0 ? null : data[front];
}
public T poll() {
if (size == 0) {
return null;
}
T output= data[front];
data[front] = null;
front = (front + 1) % data.length;
return output;
}
}
interface QueueInterface<T>
{
public boolean offer(T e) ;
public GenericArrayQueue();
public T peek();
public T element() ;
public T remove() ;
public boolean offer(T e);
}