-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeArray.java
More file actions
73 lines (61 loc) · 1.33 KB
/
DequeArray.java
File metadata and controls
73 lines (61 loc) · 1.33 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
public class DequeArray<T> {
// Attributes
private ArrayDynamic<T> deque;
private int size;
private int capacity;
// Constructors
public DequeArray() {
this.capacity = 10;
this.deque = new ArrayDynamic<T>(this.capacity);
this.size = 0;
}
public DequeArray(int capacity) {
this.capacity = capacity;
this.deque = new ArrayDynamic<T>(this.capacity);
this.size = 0;
}
// Methods
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void addFirst(T data) {
deque.prepend(data);
size++;
}
public void addLast(T data) {
deque.add(data);
size++;
}
public T removeFirst() {
if (isEmpty()) {
return null;
}
size--;
return deque.remove(0);
}
public T removeLast() {
if (isEmpty()) {
return null;
}
size--;
return deque.remove(size);
}
public T peekFirst() {
if (isEmpty()) {
return null;
}
return deque.get(0);
}
public T peekLast() {
if (isEmpty()) {
return null;
}
return deque.get(size - 1);
}
public String toString() {
return deque.toString();
}
}