-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayDeque.java
172 lines (145 loc) · 3.87 KB
/
ArrayDeque.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package Vaje01;
class ArrayDeque<T> implements Deque<T>, Stack<T>, Sequence<T> {
private static final int DEFAULT_CAPACITY = 64;
private T[] array;
private int size, front, back;
// ArrayDeque
public ArrayDeque() {
array = (T[]) new Object[DEFAULT_CAPACITY];
}
// Deque
@Override
public T front() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(Collection.ERR_MSG_EMPTY);
}
return array[front];
}
@Override
public T back() throws CollectionException {
return top();
}
@Override
public void enqueue(T x) throws CollectionException {
push(x);
}
@Override
public void enqueueFront(T x) throws CollectionException {
if (isFull()) {
throw new CollectionException(Collection.ERR_MSG_FULL);
}
front = prev(front);
array[front] = x;
size++;
}
@Override
public T dequeue() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(Collection.ERR_MSG_EMPTY);
}
T object = array[front];
array[front] = null;
front = next(front);
size--;
return object;
}
@Override
public T dequeueBack() throws CollectionException {
return pop();
}
// Sequence
@Override
public T get(int i) throws CollectionException {
if (i < 0 || i >= size) {
throw new CollectionException(Sequence.ERR_MSG_INDEX);
}
return array[index(i)];
}
@Override
public void add(T x) throws CollectionException {
push(x);
}
// Implementacija dodatnih metod
@Override
public T set(int i, T x) throws CollectionException {
if (i < 0 || i >= size) {
throw new CollectionException(Sequence.ERR_MSG_INDEX);
}
T object = array[index(i)];
array[index(i)] = x;
return object;
}
@Override
public int indexOf(T x) throws CollectionException {
int i = 0;
while (index(i) < size) {
if (x == array[index(i)]) {
return index(i);
}
i++;
}
return -1;
}
// Konec implementacije dodatnih metod
private int index(int i) {
return (front + i) % DEFAULT_CAPACITY;
}
// Stack
@Override
public T top() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(Collection.ERR_MSG_EMPTY);
}
return array[prev(back)];
}
@Override
public void push(T x) throws CollectionException {
if (isFull()) {
throw new CollectionException(Collection.ERR_MSG_FULL);
}
array[back] = x;
back = next(back);
size++;
}
@Override
public T pop() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(Collection.ERR_MSG_EMPTY);
}
back = prev(back);
T object = array[back];
array[back] = null;
size--;
return object;
}
// Collection
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean isFull() {
return size == DEFAULT_CAPACITY;
}
@Override
public int size() {
return size;
}
@Override
public String toString() {
StringBuilder objects = new StringBuilder("[");
if (size > 0) {
objects.append(array[front].toString());
}
for (int i = 0; i < size - 1; i++) {
objects.append(", ").append(array[next(front + i)]);
}
return objects.append("]").toString();
}
private int next(int i) {
return (i + 1) % DEFAULT_CAPACITY;
}
private int prev(int i) {
return (DEFAULT_CAPACITY + i - 1) % DEFAULT_CAPACITY;
}
}