Sorted Doubly Linked List Java
I got a problem here, implementing a Sorted Doubly Linked List of first and last names.
Add a field to each link that indicates the alphabetically next first name; the existing next link is used to indicate the alphabetically next last name. You will also need a second root link for the list - the existing root link indicates the alphabetically first last name, and you will need one indicating the alphab开发者_如何转开发etically first first name. Note that you will still only have one link object for each name entered in the list.
Having done this, make any necessary changes to your insert, lookup, and delete methods so that both interleaved lists are maintained. Also update the runtime estimates as required to remain accurate.
Finally, add a second lookup method that takes in a first name and returns all full names including that first name, and a second display method that prints the list of names out alphabetically by first name. Make sure you give runtime estimates for these methods as well.
And I am at a complete loss as how to do this. I already created a single linked list, with a first and last name, but that's as far as I was able to get.
Any Help would be great :D
Thank you.
- Create a Link Class that has two link fields (nextFirstName, nextLastName) and a field for the Name Object.
- On insert, first search the Link object that comes before (LastName) this new one and insert it after that using the nextLastName field. Then do the same with the FirstName, using the nextFirstName field as the link.
- ?????
- profit!
This does smell a lot like homework :)
Because you have already implemented a Singly Linked List, expanding to a doubly linked list shouldn't be too difficult. You already have references going forward (see picture below). Now you need to add references going backward as well. In addition, note the blue lines in the picture below. Add additional references for the spelling properties. So each node will have the variables:
private Node NextFirstName;
private Node PreviousFirstName;
private Node NextLastName;
private Node PreviousLastName;
You do NOT need backward-references! The task asks for something like 2 single-linked Lists, that use the same objects as entrys. Every object has two links: one to the next item in list A, and one to the next item in list B.
精彩评论