# Priority Queue using comparable

• December 3rd, 2010, 03:05 PM
jkalm
Priority Queue using comparable
Hey,

Im trying to make a Prioity Queue that uses the queue interface and comparable.. The objects will be placed in depending on their priority.. i'm having a bit of trouble with it, probaly cause i'm quite new to Comparables..
I have the following but am having trouble even understanding what it is my code is doing!!

Code java:

public int compareTo(Object o)
{
// 1:   films duration is greater than 'o'
if(this.duration > ((Film)o).duration)
return 1;
// 0:   films duration equals 'o'
else if(this.duration == ((Film)o).duration)
return 0;
// -1:   films durations is less than 'o'
return -1;
}

Code java:

public void enqueue (Comparable o)
{

if(isEmpty())
{
int temp =rear;
rear= (rear + 1) % queue.length;
if (front ==rear)
{
rear = temp;
throw new FullQueueException ();
}
queue[rear] = o;
}

else
{

for(int i = 0; i <= queue.length; i++)
{
//duration is greater so everything must be moved right
if(o.compareTo(queue[i]) == 1 )
{
for(int x = 0; x <= queue.length; x++)
{
int temp =rear;
rear= (rear + 1) % queue.length;
if (front ==rear)
{
rear = temp;
throw new FullQueueException ();
}
queue[rear] = queue[rear+1];
}
queue[front] = o;
}

if(o.compareTo(queue[i]) == 0 )
{

}

Any help appreciated
• December 3rd, 2010, 03:30 PM
copeg
Re: Priority Queue using comparable
I presume you are writing this yourself as an exercise or homework? Java provides the PriorityQueue class.

What exactly is the problem you are having, as its not obvious (at least to me) from the posted code?
• December 4th, 2010, 02:23 AM
helloworld922
Re: Priority Queue using comparable
I take it that the variable duration is a number? You can just subtract the two numbers. Also, Comparable interfaces must implement what they are comparable to (generally you don't want this to be a wild-card or the base Object class)

Code Java:

public class Film implements Comparable<Film>
{
// ... other code

public int compareTo(Film o)
{
return this.duration - o.duration;
}
}

Code Java:

public void enqueue (Comparable<Film> o)

• December 4th, 2010, 09:45 AM
jkalm
Re: Priority Queue using comparable
I think we have to make it generic and able to work with any kind of object?? Ye its a class exercise.. im just wondering if my enqueue method is on the right track.. "Joining the queue (enqueue)will involve finding the correct position for the new item, based on its priority, i.e. all the items before it in the queue should have >= priority; all items after it should have a lower (<) priority."
• December 4th, 2010, 05:29 PM
helloworld922
Re: Priority Queue using comparable
I kind of doubt it, it makes no sense to try and compare say a BufferedImage object to your Film object.
• December 4th, 2010, 05:44 PM
jkalm
Re: Priority Queue using comparable
Ok, ill give it try.. Am i along the right track with what i have though??
• December 5th, 2010, 10:02 PM
jkalm
Re: Priority Queue using comparable
I'm having trouble trying to move all elements right in this queue. For example, I need to place something in between others because it's priority says it's in there, so how would i make space for it in my circular array queue?? thanks again, hope i'm making sense!