forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
45 lines (36 loc) · 1009 Bytes
/
LinkedQueue.java
File metadata and controls
45 lines (36 loc) · 1009 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
39
40
41
42
43
44
45
import java.util.ArrayList;
public class LinkedQueue<E> {
private class Node<E> {
E data;
Node<E> next;
Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
private Node<E> head;
private Node<E> last;
public void enqueue(E item) {
Node<E> newNode = new Node<E>(item, null);
if (null == head) head = newNode;
if (null != last) last.next = newNode;
last = newNode;
}
public E dequeue() {
E answer = head.data;
head = head.next;
return answer;
}
public boolean isEmpty() {
return (head == null);
}
public static void main(String[] args) {
LinkedQueue<Integer> ints = new LinkedQueue<>();
ints.enqueue(1);
ints.enqueue(2);
ints.enqueue(3);
System.out.println(ints.dequeue());
System.out.println(ints.dequeue());
System.out.println(ints.dequeue());
}
}