Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2010, 06:54 PM   PM User | #1
Bobjames786
New to the CF scene

 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Bobjames786 is an unknown quantity at this point
Sorting Objects in a Linked List

Hi all, Im wondering if I can get any help from experts here as im a student and im in the learning process.

So here is what the program is:
There is a class called Flight :



public class Flight {

private String flightID;
Integer priority;

public Flight (Integer priority, String flightID) {
this.flightID = flightID;
this.priority = priority;
}

public Flight () {
flightID = "";
priority = 0;
}

public String getFlightID() {
return flightID;
}

public void setFlightID(String flightID) {
this.flightID = flightID;
}

public Integer getPriority() {
return priority;
}

public void setPriority(Integer priority) {
this.priority = priority;
}

public String toString() {
return "Flight Number "+flightID+" ,Is in position number "+priority;
}

}


2nd class called FlightQueue:
The class needs to add,remove,show all the flights in the list:
here is the code so far:


import java.util.LinkedList;

public class FlightQueue {
private LinkedList <Flight> flights = new LinkedList <Flight> ();

public void joinQueue(Flight f){
flights.add(f);
}

public void landFlight(Flight f){
flights.remove(f);
}

public boolean isEmpty (){
return flights.isEmpty();
}

public int size (){
return flights.size();
}

public void clear (){
flights.clear();
}

public void display (){
System.out.println(flights);
}
}


Lastly I need to create a Test class which will demonstrate that will demonstrate the queue in action:
These are the things that are needed:
add a Flight object to the FlightQueue
remove
show the size


This is the problem i have, i need to Sort the FlightQueue in order of the priority variable in the Flight class, from 1-9, 9 with the highest priority and 1 being the lowest.
So when i remove an object from the list it should remove the Flight object with the highest number.

Any help would be welcomed.

Thanks
Bobjames786 is offline   Reply With Quote
Old 11-08-2010, 04:48 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The flightQueue will need to be rewritten to deal with the priority.
Java has a builtin PriorityQueue: http://download.oracle.com/javase/1....rityQueue.html
In order to use it, you need to implement Comparable[<T>] on your Flight class. This allows it to compare itself to other flight's to determine the best location to place it in the internal list. You can use the PriorityQueue for your underlying storage. The comparator will need to compare the priority. The API does not list whether the PriorityQueue class acts as a stable or instable insertion, but given the properties of a Queue its likely instable.

If you need to write this yourself, stick with a custom linked list. LinkedLists are great datastructures for priority queues as you can easily drop and insert from locations within the list with minimal additional overhead. This would require you to do your own checking for the Flight objects in order to determine their best route.

Also, this looks very odd:
PHP Code:
public Flight (Integer priorityString flightID
Is there a particular reason / requirement that you are using the Integer class for the priority? Integer != int, so these are incomparable directly. I would probably chain a primitive constructor as well and store priority with an underlying int instead of Integer.

And in the future, if you could please wrap all code blocks in [code][/code] tags in order to preserve the formatting for the code.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 PM.


Advertisement
Log in to turn off these ads.