写道
public class RecurringQueue {
private Node head;
private Node tail;
private Node cursor;
public void add(Node node) {
if (head == null) {
head = node;
tail = node;
head.setNext(tail);
tail.setNext(head);
} else {
if (node.getPriority() <= head.getPriority()) {
node.setNext(head);
head = node;
tail.setNext(head);
return;
}
if (node.getPriority() >= tail.getPriority()) {
tail.setNext(node);
tail = node;
tail.setNext(head);
return;
}
Node iter = head;
while (iter != null) {
Node next = iter.getNext();
if (next.getPriority() >= node.getPriority()) {
node.setNext(next);
iter.setNext(node);
break;
}
iter = iter.getNext();
}
}
}
public Node next() {
if (cursor == null) {
cursor = head;
} else {
cursor = cursor.getNext();
}
return cursor;
}
class Node {
private String name;
private Integer priority;
private Node next;
public Node(String name, Integer priority) {
this.name = name;
this.priority = priority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}