-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueImpl.java
58 lines (49 loc) · 912 Bytes
/
QueueImpl.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
package randon;
import java.util.ArrayList;
public class QueueImpl<T> {
private ArrayList<T> list;
private int rear = 0, front = -1;
public QueueImpl() {
list = new ArrayList<T>();
}
public void enqueue(T data) {
list.add(data);
this.front++;
}
public boolean isEmpty() {
return front==-1;
}
public T dequeue() {
if (this.front < 0) {
return null;
}
T item = this.list.get(rear);
this.list.remove(rear);
this.list.trimToSize();
this.front--;
return item;
}
public int size() {
return this.list.size();
}
public T peek() {
if (front < 0) {
return null;
}
return this.list.get(rear);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return list.toString();
}
}
class EmptyException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public EmptyException(String msg) {
super(msg);
}
}