-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedListQueue.java
More file actions
51 lines (43 loc) · 1.04 KB
/
LinkedListQueue.java
File metadata and controls
51 lines (43 loc) · 1.04 KB
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 LinkedListQueue <T> implements QueueInterface {
ListNode head;//Can't add objects to middle
ListNode tail;//Only can remove at head, add at tail, FIFO Rules
private class ListNode {
T data;
ListNode link;
public ListNode(T aData, ListNode aLink) {
data = aData;
link = aLink;
}//ListNode
}//ListNode
public LinkedListQueue() {
head = tail = null;
}//LinkedListQueue
public void enqueue(Object data) {
ListNode newNode = new ListNode((T)data,null);
if(head == null) {
head = newNode;
tail = newNode;
return;
}//if list is empty
tail.link = newNode;
tail = newNode;
}//enqueue
public T dequeue() {
ListNode temp = head;
head = head.link;
return temp.data;
}
public T peek() {
if(head != null) {
return head.data;
}
return null;
}//peek
public void print() {
ListNode temp = head;
while(temp != null) {
System.out.println(temp.data.toString());
temp = temp.link;
}//while
}//print
}//LinkedListQueue