-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedQueue.java
57 lines (51 loc) · 1.64 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
public class LinkedQueue {
/**
* Enqueue, Dequeue, Peek, and isEmpty are all O(1) constant time compelxity
* The head and tail pointers are implemented to avoid traversing the queue in a linear fashion
*/
private class Node{
private int data;
private Node next;
public Node(int data){ this.data = data; }
}
//Head pointer to keep track of the first element added to the queue
private Node head;
//tail pointer is where elements are added, to the back of the queue
private Node tail;
public void enqueue(int data){
//new Node object with data
Node node = new Node(data);
//first element added
if(head == null){
//head and tail equal each other
head = node;
tail = head;
}
else{
//more than 1 element..
tail.next = node; //add to the tail's next (tail is at the prev element right now)
tail = tail.next; //make the tail equal the next node (last element added to the queue)
}
}
//remove from the head/front
public int dequeue(){
//retrieve data
int displayData = head.data;
//not deleting data but shifting the head pointer to the next node
head = head.next;
//removing the first element makes the queue empty
if(head == null){
tail = null;
}
return displayData;
}
public int peek(){
if(head == null){
throw new NullPointerException();
}
else{
return head.data;
}
}
public Boolean isEmpty(){return head == null;};
}