-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedPQ.java
More file actions
41 lines (40 loc) · 979 Bytes
/
LinkedPQ.java
File metadata and controls
41 lines (40 loc) · 979 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
public class LinkedPQ<T> {
private int size;
private PQNode<T> head;
/* tail is of no use here. */
public LinkedPQ() {
head = null;
size = 0;
}
public int length (){
return size;
}
public boolean full () {
return false;
}
public void enqueue(T e, int pty) {
PQNode<T> tmp = new PQNode<T>(e, pty);
if((size == 0) || (pty > head.priority)) {
tmp.next = head;
head = tmp;
}
else {
PQNode<T> p = head;
PQNode<T> q = null;
while((p != null) && (pty <= p.priority)) {
q = p;
p = p.next;
}
tmp.next = p;
q.next = tmp;
}
size++;
}
public PQElement<T> serve(){
PQNode<T> node = head;
PQElement<T> pqe=new PQElement<T>(node.data, node.priority);
head = head.next;
size--;
return pqe;
}
}