-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLinkedListQueue.java
More file actions
54 lines (45 loc) · 1.16 KB
/
Copy pathLinkedListQueue.java
File metadata and controls
54 lines (45 loc) · 1.16 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
package Unit_3_Labs_Stacks_Queues;
public class LinkedListQueue<T> {
private static class Node<T> {
private T value;
private Node<T> next;
}
// ADD YOUR CODE HERE.
private int size;
private Node<T> head;
private Node<T> tail;
public LinkedListQueue() {
// ADD YOUR CODE HERE.
head = null;
tail = null;
size = 0;
}
public void enqueue(T newItem) {
if (newItem == null) throw new IllegalArgumentException();
Node<T> curr = tail;
tail = new Node<T>();
tail.next = null;
tail.value = newItem;
if (isEmpty()) head = tail;
else curr.next = tail;
size++;
}
public T dequeue() {
if (isEmpty()) throw new IllegalStateException();
T t = (T)head.value;
head = head.next;
size--;
if (isEmpty()) tail = null;
return t;
}
public T peek() {
if (isEmpty()) throw new IllegalStateException();
return (T)head.value;
}
public boolean isEmpty() {
return head == null;
}
public int size() {
return size;
}
}