Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •