-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListQueue.java
executable file
·39 lines (36 loc) · 1020 Bytes
/
ListQueue.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
package pathByQueue;
public class ListQueue<E> implements Queue<E> {
private static class Node<E> {
public Node<E> next;
public E e;
public Node() {}
public Node(E e, Node<E>next) { this.e = e; this.next = next; }
}
private Node<E> head, tail;
private int size;
public ListQueue() {
head = tail = new Node<E>();
size = 0;
}
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public void enqueue(E e) {
//TODO: add a new node at head.next,
// move head, and increase size
head.next = new Node<E>(e, head.next);
head = head.next;
size++;
}
public E dequeue() {
E data = first();
//TODO: move tail and decrease size
tail = tail.next;
size --;
return data;
}
public E first() {
if(isEmpty())
throw new IllegalStateException("Queue is empty");
return tail.next.e;
}
}