-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.java
79 lines (67 loc) · 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Program #2
* The following program uses the methods from LinearList class
* to implement a Queue
* CS310-1
* 03-11-2019
* @author Pranav Kalra cssc1483
*/
package data_structures;
import java.util.Iterator;
public class Queue<E extends Comparable<E>> implements Iterable<E> {
private LinearList<E> linearList = new LinearList<E>();
public Queue() {}
/*inserts the object obj into the queue
*/
public void enqueue(E obj) {
linearList.addLast(obj);
}
/* removes and returns the object at the front of the queue
*/
public E dequeue() {
return linearList.removeFirst();
}
/* returns the number of objects currently in the queue
*/
public int size() {
return linearList.size();
}
/* returns true if the queue is empty, otherwise false
*/
public boolean isEmpty() {
return linearList.isEmpty();
}
/* returns but does not remove the object at the front of the queue
*/
public E peek() {
return linearList.peekFirst();
}
/* returns true if the Object obj is in the queue
*/
public boolean contains(E obj) {
return linearList.contains(obj);
}
/* returns the queue to an empty state
*/
public void makeEmpty() {
linearList.clear();
}
/* removes the Object obj if it is in the queue and
* returns true, otherwise returns false.
*/
public boolean remove(E obj) {
if (linearList.remove(obj) == null)
return false;
else {
linearList.remove(obj);
return true;
}
}
/* 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 linearList.iterator();
}
}
//-----------------------------------END-OF-Queue-Class-------------------------------------------