-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java~
53 lines (52 loc) · 1.25 KB
/
Queue.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
import java.util.NoSuchElementException;
public class Queue<T> implements QueueADT<T> {
private Object[] data;
public Queue() {
data = new Object[0];
}
public void enqueue(T item) {
Object[] tdata = new Object[data.length];
for(int i = 0; i < data.length; i++) {
tdata[i] = data[i];
}
data = new Object[tdata.length+1];
for(int i = 0; i < data.length; i++) {
data[i] = tdata[i];
}
data[data.length-1] = item;
}
public T dequeue() throws NoSuchElementException {
Object[] tdata = new Object[data.length];
for(int i = 0; i < data.length; i++) {
tdata[i] = data[i];
}
if(tdata.length > 1) {
data = new Object[tdata.length-1];
for(int i = 1; i < data.length; i++) {
data[i-1] = tdata[i];
}
return tdata[0];
} else {
throw new NoSuchElementException("You cannot pop a Stack with no elements");
}
}
public T front() throws NoSuchElementException {
if(data.length > 0) {
return data[0];
} else {
throw new NoSuchElementException("You cannot look at the top of a Stack with no elements");
}
}
public int size() {
return data.length;
}
public boolean isEmpty() {
if(data.length > 0) {
return false;
}
return true;
}
public void clear() {
data = new Object[0];
}
}