-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLQueue.java
More file actions
51 lines (43 loc) · 847 Bytes
/
LLQueue.java
File metadata and controls
51 lines (43 loc) · 847 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
46
47
48
49
50
51
public class LLQueue <T> implements QueueInterface<T>{
private class ListNode{
private T data;
private ListNode link;
public ListNode(T aData, ListNode aLink) {
data = aData;
link = aLink;
}
}
private ListNode head;
private ListNode tail;
public LLQueue() {
head = tail = null;
}
public void enqueue(T aData) {
ListNode newNode = new ListNode(aData, null);
if(head==null) {
head = tail = newNode;
return;
}
tail.link = newNode;
tail = newNode; //or tail = tail.link
}
public T dequeue() {
if(head==null)
return null;
T ret = head.data;
head = head.link;
return ret;
}
public T peek() {
if(head == null)
return null;
return head.data;
}
public void print() {
ListNode temp = head;
while(temp!=null) {
System.out.println(temp.data);
temp = temp.link;
}
}
}