Life of the IT-Drummer

A web log from Tobias Rusås Olsen.

A SimplePriorityQueue in Java.

For some crazy reason, I today decided to implement the most simple form for priority queue possible. And since I just got Syntax highlighting on my site, I decided to put it out here.

PS: If you are looking for a PriorityQueue, please use this one, it’s a better solution, based on a heap implementation.

This SimplePriorityQueue has takes O(n) for inserting, and O(1) for getting the object with highest value. I believe it’s stable (if the values are equal, then what you put in first comes out first). Ow, I have not looked that hard into the code, so there might be some unused methods and such.

You need this two classes:

Node.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
 
/**
 * Simple Node class, used in SimplePriorityQueue.
 * @author tol060
 *
 * @param <E>
 */
public class Node<E> {
	private E content;
	private Node<E> next, previous;
	private int value;
	public E getContent() { return content; }
	public void setContent(E content) { this.content = content; }
	public Node<E> getNext() {	return next; }
	public void setNext(Node<E> next) { this.next = next; }
	public Node<E> getPrevious() { return previous; }
	public void setPrevious(Node<E> previous) { this.previous = previous; }
	public int getValue() { return value; }
	public void setValue(int value) { this.value = value; }
	public Node(E newObj, int newValue) {
		this.content = newObj;
		value = newValue;
	}
}

The priorityqueue:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
 
/**
 * A simple priority queue. Insert values are O(n), getting values are O(1).
 * @author tol060
 *
 * @param <E>
 */
public class SimplePriorityQueue<E> {
	private Node<E> startNode;
	private int size;
 
	public SimplePriorityQueue() {
		size = 0;
	}
 
	/**
	 * Insert an object with a given value. O(n).
	 * @param Object to be inserted
	 * @param Weight of object
	 */
	public void insertValue(E obj, int value) {
		Node<E> newNode = new Node<E>(obj,value);
		if(size == 0) insertFirst(newNode);
		else insertGeneral(newNode);
		size++;
	}
 
	/**
	 * Retrieves the lowest valued object in the queue. O(1).
	 * Returns null if it's empty.
	 * @return Lowest valued object, null if empty queue
	 */
	public E getValue() {
		if(size==0) return null;
		Node<E> returnNode = startNode;
 
		if(startNode.getNext()==null) startNode = null;
		else startNode = startNode.getNext(); 
		size--;
		return (E) returnNode.getContent();
	}
 
	private void insertFirst(Node<E> newNode) {
		startNode = newNode;
	}
 
	private void insertGeneral(Node<E> newNode) {
		Node<E> current = startNode;
		Node<E> previous = null;
		boolean first = true;
		while(newNode.getValue() >= current.getValue()) {
			first = false;
			if(current.getNext()!=null) {
				previous = current;
				current = current.getNext();
			}
			else {
				current.setNext(newNode);
				newNode.setPrevious(current);
				return;
			}
		}
		if(previous!=null) previous.setNext(newNode);
 
		current.setPrevious(newNode);
		newNode.setNext(current);
		newNode.setPrevious(previous);	
		if(first) startNode = newNode;
	}
}

Yeah, the syntax highlighting looks great. :)

RSS 2.0 | Trackback | Comment

One Response to “A SimplePriorityQueue in Java.”

  1. Arne

    I think i’d implement an empty head node which contains pointer to the first and last node in order to get faster inserting when using fifo (first in first out). you’d have to call headnode.next() to get the first, and headnode.last() to get the last node.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">