-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericQueue.java
More file actions
72 lines (52 loc) · 1.52 KB
/
Copy pathGenericQueue.java
File metadata and controls
72 lines (52 loc) · 1.52 KB
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
import java.util.ArrayList;
import java.util.Iterator;
//import GenericList.Node;
public class GenericQueue<T> extends GenericList<T> {
private Node<T> tail;
//Overloaded constructor for add. It has to be done per guidelines
public void add(T data, int code) {
Node<T> node = new Node<>(data, code, null); // constructor sets next = null
if (getHead() == null) {
// empty list → make this node the head
// requires the protected helper inside GenericList
setHeadNode(node);
setLength(1);
tail = node;
return;
}
Node<T> current = getHead();
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(node);
tail = node;
setLength(getLength() + 1);
}
// Adds new element to the end of queue (data)
public void enqueue(T data) {
add(data);
}
// Adds new element to the end of queue (data, code)
public void enqueue(T data, int code) {
add(data, code);
}
// Deletes element from the end of list
public T dequeue() {
return delete();
}
// Returns last element in queue
public Node<T> getTail() {
Node<T> current = getHead();
if (current == null) {
return null;
}
while(current.getNext() != null) {
current = current.getNext();
}
return current;
}
@Override
public Iterator<T> iterator() {
return null;
}
}