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!!
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; }
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