-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueGeneric.java
More file actions
38 lines (27 loc) · 812 Bytes
/
QueueGeneric.java
File metadata and controls
38 lines (27 loc) · 812 Bytes
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
public class Queue<T> implements Iterable<T>{
private java.util.LinkedList<T> list = new java.util.LinkedList<T>();
public Queue(){}
public Queue(T data){
enqueue(data);
}
public int size(){
return list.size();
}
public boolean isEmpty(){
return size() == 0;
}
public T peek(){
if(isEmpty()) throw new RuntimeException('The queue is empty');
return list.peekFirst();
}
public T dequeue(){
if(isEmpty()) throw new RuntimeException('The queue is empty');
return list.removeFirst();
}
public void enqueue(T data){
list.addLast(data);
}
@Override public java.util.Iterator <T> iterator(){
return list.iterator();
}
}