-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLQueue.java
58 lines (56 loc) · 945 Bytes
/
LLQueue.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
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; //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() //BIG EXAM QUESTION
{
ListNode temp = head;
while(temp != null)
{
System.out.println(temp.data);
temp = temp.link;
}
}
}