Hello all, this is my first post and I'm excited to have found this place. I want to be a programmer, in my second semester of Java programming. It always feels like the professor is going too fast for me, though!
For our first homework, we are to write a program that can...
Read a file that lists cities of origin followed by a list of destination cities and the cost of going from the origin to the destination. The format of the file is:
origin dest1 cost1 dest2 cost2 .... destn costn
The list of destinations for a particular city of origin is referred to as an adjacency list. You should keep track of each adjacency lists as a linked list. You should keep track of the entire list of cities using a binary search tree keyed on the name of the city so that you can search by name efficiently.
Currently, I am stuck on storing the destinations of an origin. My code looks like this:
Now I don't know how to take the Flight object, f, and store it in an adjacency list. I have a TreeNode class and a LinkedList class. I'm not even sure I have the flight declaration in the right place
I'd greatly appreciate any light anyone can shine on this for me!
Umm.... is there a file for each origin, or are all the origins in the same file?
What does your Flight class look like?
EDIT - I guess what I'm saying is, that you're not following the instructions of the section of the directions that requires you to store the destination and cost in a tree, unless there's something I'm not seeing. Have you looked at the API for TreeMap ? Are you allowed to use it for this example?
When you do things right, people won't be sure you've done anything at all.
Joined: Feb 20, 2010
Hi Janeice, thanks for replying!
My flight class contains a constructor for a Flight object with origin, destination, and cost, and getters and setters for each.
He wants us to store each adjacency list (containing an origin's destinations and costs) as a linked list and then,
...keep track of the entire list of cities using a binary search tree keyed on the name of the city so that you can search by name efficiently.
I guess what I'm wondering is, how do I store each new adjacency list in a linked list?
I saw your post yesterday, and didn't understand the problem, and I still don't understand the problem. Please find out why a linked list is a good solution to the adjacency list. It sounds a good solution for successive stops on the same flight, however. When I first saw the question, I immediately thought of a Map, which you can link airports to lists of airports with, but didn't immediately see how a linked list would help. Janeiece's suggestion of a TreeMap sounds promising, but I don't know the details of that class.
I just suppose I wouldn't have a Flight object for this problem... I might have an Origin object that contains 3 attributes: City, State, DestinationList.... but I still don't see how having an object REALLY solves the problem according to the spec of the example.
I THINK (from googling and looking at This Page), it is a requirement to use a TreeMap as a part of your AdjacencyList class. It looks like you may need to create this class and use it to hold and manipulate your information somehow....
Take a look at that page, and let me know if it helps.... I suppose I'm not really sure what's involved in this program either, but we can figure it out together
EDIT : And I don't think TreeNode is the answer here.... look at this Java Tutorial... it seems like TreeNode is part of JTree which is a Swing component.... unless this is a SWING assignment.
Ultimately, and I'm still thinking about this.... I think you need to do 2 things.
1. Create an adjacency list class which can store the information in a thoughtful way. You need to store destination cities and cost, right? So I think your DestinationList class might have an attribute that is a LinkedList and each index can hold a String, where index 0 is the destination and index 1 is the cost. I would still have used a Map for this, but if the requirement is a LinkedList, then so be it. The method, addDestination(), can create the array from a String and an Integer and append it to the end of the LinkedList. You can also have methods removeDestination(), findDestination(), showDestinations().... ad nauseum depending on how far you want to take this thing.
2. Store that list in a TreeMap, HashMap, or some other kind of Map that stores the name of the origin city as the KEY, and the DestinationList as the VALUE.... look at the API for TreeMap or HashMap for how to do that.
I still don't see a reason to store anything in an object... other than the DestinationList..... for this application.