• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing objects in a linked list?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



Then,

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!

Thanks.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Umm.... is there a file for each origin, or are all the origins in the same file?

What does your Flight class look like?

Janeice

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?
 
Bobby Burch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 hope I'm explaining it okay!

Thanks again.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome (again)

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.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Janeice

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.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[2], 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.
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic