-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperDeque.java
More file actions
145 lines (132 loc) · 4.14 KB
/
SuperDeque.java
File metadata and controls
145 lines (132 loc) · 4.14 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
/**
* @author Brianna Penkala
* This class represents a deque
*/
public class SuperDeque <E> {
/** A field that tracks the front of the deque */
private int front;
/** A field that tracks the back of the deque */
private int back;
/** A field for the array of the deque */
private E[] dq;
/** A field that sets the initial capacity of the deque */
private static final int DEFAULT_CAP = 1;
/** A method that returns the length of the deque array
* @return the length of the deque
* */
int getDQLength() {
return this.dq.length;
}
/** The constructor for the deque */
public SuperDeque() {
this.dq = (E[]) new Object[DEFAULT_CAP];
this.front = -1;
this.back = -1;
}
/** A method that adds an element to the front of the deque
* @param element the element to add to the front of the deque
* */
public void push(E element) {
if (front == (back + 1) % dq.length) { //if array full
doubleSize();
}
else if (front == -1) { //initializes circular array
front = 0;
back = 0;
}
if (front == 0) //if front reaches the end of the circle, moves it to the other end
front = dq.length - 1;
else
front--;
dq[front] = element;
}
/** A method that removes an element from the front of the deque
* @return the element removed
* */
public E pop() {
if (isEmpty())
return null;
else {
E save = dq[front];
dq[front] = null;
front = (front + 1) % dq.length;
return save;
}
}
/** A method that returns the first element in the deque
* @return the element at the front of the deque
* */
public E peek() {
if (isEmpty())
return null;
else
return dq[front];
}
/** A method that adds an element to the back of a deque
* @param element the element to add to the back of the deque
* */
public void enqueue(E element) {
if (front == (back + 1) % dq.length) { //if array full
doubleSize();
back++;
}
else if (front == -1) { //initializes circular array
front = 0;
back = 0;
}
else
back++;
dq[back] = element;
}
/** A method that removes an element from the front of the deque
* @return the element removed from the deque
* */
public E dequeue() {
if (isEmpty())
return null;
else {
E save = dq[front];
dq[front] = null;
front = (front + 1) % dq.length;
return save;
}
}
/** A method that checks if a deque is empty
* @return true if the deque is empty
* */
public boolean isEmpty() {
return dq.length < 1;
}
/** A method that doubles the size of the deque **/
private void doubleSize() {
int currentLength = dq.length;
int newLength = currentLength * 2;
E[] newArray = (E[]) new Object[newLength];
if (dq.length == 1) //if array has just been initialized, only has one element
newArray[0] = dq[front];
else {
for (int i = 0; i < currentLength; i++) { //add all elements to the new array
newArray[i] = dq[front];
front = (front + 1) % currentLength;
}
}
front = 0;
back = currentLength - 1;
dq = newArray;
}
/** A method that converts the deque to a string
* @return the string version of the deque
* */
public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i != dq.length; i++) { //goes through array
int index = (front + i) % dq.length; //helps to specify the element at the index
if (builder.length() >= 1 && dq[index] != null) //if there are proceeding elements
builder.append(", ");
if (dq[index] != null) {
builder.append(dq[index]);
}
}
return builder.toString();
}
}