-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueWithLinkedList.java
60 lines (48 loc) · 1.12 KB
/
QueueWithLinkedList.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
package com.brian.portfolio.datastructures;
public class QueueWithLinkedList {
private Node first;
private Node last;
private int count;
public void enqueue(int value) {
var node = new Node(value);
if (isEmpty())
first = last = node;
else {
last.next = node;
last = node;
}
count++;
}
public int dequeue() {
if (isEmpty())
throw new IllegalStateException();
var value = first.value;
var second = first.next;
if (first == last)
first = last = null;
else {
first.next = null;
first = second;
}
count--;
return value;
}
public int peek() {
if (isEmpty())
throw new IllegalStateException();
return first.value;
}
public int size() {
return count;
}
public boolean isEmpty() {
return first == null;
}
private class Node {
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
}