我在队列中实现了各种方法,然后想对优先级队列执行相同的操作,但是有没有办法访问属性 priority
?
例如,在方法 isInQueue
中,如果作为参数接收的值和优先级可用,则此方法应返回 true 是优先级队列。
public boolean isInQueue(int value) {
PriorityQ q = new PriorityQ();
if(this.isEmpty())
return false;
while(!this.isEmpty()) {
if(this.queueFront() == value)
return true;
q.add(this.queueFront());
this.remove();
}
while(!q.isEmpty()) {
this.add(q.queueFront());
q.remove();
}
return false;
}
这是全班:
public class PriorityQ {
class Element {
int data; // ELEMENT'S DATA
int priority; // ELEMENT’S PRIORITY
Element next; // REFERENCE TO THE NEXT ELEMENT
final static int DEFAULT_PRIORITY = 0;
Element(int value, int priority) {
this.data = value;
this.priority = priority;
this.next = this;
}
Element(int value) {
this(value, DEFAULT_PRIORITY);
}
}
private Element queue = null;
public PriorityQ() {
this.queue = null;
}
public boolean isEmpty() {
return this.queue == null;
}
public void remove() {
if (this.isEmpty())
System.exit(0);
if (this.queue.next == this.queue)
this.queue = null;
else
this.queue.next = this.queue.next.next;
}
public int queueFront() {
if (this.isEmpty())
System.exit(0);
return this.queue.next.data;
}
public void add(int value, int priority) {
Element tmp = new Element(value, priority);
// special case 1: Empty queue
if (this.isEmpty())
this.queue = tmp;
// Special case 2: insert a high priority element
else if (tmp.priority > this.queue.priority) {
tmp.next = this.queue.next;
this.queue.next = tmp;
this.queue = tmp;
}
// Special case 3: insert a low priority element
else if (tmp.priority <= this.queue.next.priority) {
tmp.next = this.queue.next;
this.queue.next = tmp;
}
else { // insertion according to the priority
Element cur = queue.next;
while (cur.next.priority < tmp.priority)
cur = cur.next;
tmp.next = cur.next;
cur.next = tmp;
if (cur == this.queue)
this.queue = this.queue.next;
}
}
public void add(int value) {
this.add(value, Element.DEFAULT_PRIORITY);
}
@Override
public String toString() {
String s = " ";
if(this.isEmpty())
return"Empty list";
Element cur = this.queue;
s = " | " + cur.data + " | ";
cur = cur.next;
while(cur != this.queue) {
s+= cur.data + ", " + cur.priority + " | ";
cur = cur.next;
}
return s;
}
public int search(int value, int p) {
PriorityQ q1 = new PriorityQ();
int index = -1;
int c = 0; //counter
if(this.isEmpty()) {
System.out.println("the queue is empty");
return -1;
}
while(!this.isEmpty() && index == -1) {
if(this.queueFront() == value)
index = c;
q1.add(this.queueFront(), p);
this.remove();
c++;
}
while(!q1.isEmpty()) {
this.add(q1.queueFront(), p);
q1.remove();
}
return index;
}
public boolean isInQueue(int value) {
PriorityQ q = new PriorityQ();
if(this.isEmpty())
return false;
while(!this.isEmpty()) {
if(this.queueFront() == value)
return true;
q.add(this.queueFront());
this.remove();
}
while(!q.isEmpty()) {
this.add(q.queueFront());
q.remove();
}
return false;
}
public static void main(String[] args) {
PriorityQ q = new PriorityQ();
q.add(1, 1);
q.add(3, 4);
q.add(3, 3);
q.add(4, 1);
q.add(5, 0);
q.add(6, 8);
System.out.println(q);
System.out.println(q.search(1, 1));
System.out.println(q.queueFront());
System.out.println(q.isInQueue(3));
}
}