-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListQueue.java
62 lines (58 loc) · 1004 Bytes
/
LinkedListQueue.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
61
62
/*
* Written by Noah Shaw
*/
public class LinkedListQueue <T> {
//Internal class
private class ListNode
{
private T data;
private ListNode link;
public ListNode(T aData, ListNode aLink)
{
data = aData;
link = aLink;
}
}
//Attributes
private ListNode head; //Points to the first item in the list
private ListNode tail; //Points to the last item in the list
//Other methods
public void enqueue(T aData)
{
ListNode newNode = new ListNode(aData, null);
if(head == null)
{
head = tail = newNode;
return;
}
tail.link = newNode;
tail = newNode;
}
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;
}
}
}