-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedQueue.java
60 lines (46 loc) · 1.08 KB
/
LinkedQueue.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
import java.util.EmptyStackException;
/**
* Created by jkeys on 10/14/2015.
*/
public class LinkedQueue<T> implements QueueInterface<T> {
private SNode<T> front;
private SNode<T> back;
private int size;
public LinkedQueue() {
front = back = null;
size = 0;
}
public void enqueue(T e) {
if(front == null) {
front = new SNode<T>(e, back);
back = front;
size++;
return;
}
SNode<T> newBack = new SNode<T>(e, null);
back.setNext(newBack);
back = newBack;
size++;
}
public T dequeue() {
if(front == null)
return null;
T result = front.getData();
SNode<T> newFront = front.getNext();
front.setNext(null);
front = newFront;
size--;
return result;
}
public T front() {
if (front == null)
return null;
return front.getData();
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
}