-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
59 lines (49 loc) · 1.44 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
54
55
56
57
58
59
/* Joshua Villasenor
masc0431
*/
package data_structures;
import java.util.*;
public class Queue<E> implements Iterable<E> {
ListADT <E> list;
public Queue(){
list = new LinkedListDS <E>();
}
// inserts the object obj into the queue
public void enqueue(E obj) {
list.addLast(obj);
}
// removes and returns the object at the front of the queue
public E dequeue() {
return list.removeFirst();
}
// returns the number of objects currently in the queue
public int size() {
return list.size();
}
// returns true if the queue is empty, otherwise false
public boolean isEmpty() {
return list.isEmpty();
}
// returns but does not remove the object at the front of the queue
public E peek() {
return list.peekFirst();
}
// returns true if the Object obj is in the queue
public boolean contains(E obj) {
return list.contains(obj);
}
// returns the queue to an empty state
public void makeEmpty() {
list.makeEmpty();
}
// removes the Object obj if it is in the queue and
// returns true, otherwise returns false.
public boolean remove(E obj){
return list.remove(obj);
}
// returns an iterator of the elements in the queue. The elements
// must be in the same sequence as dequeue would return them.
public Iterator<E> iterator() {
return list.iterator();
}
}