|
|
||
| You are here: | ||
|
HL Mastery aspect: ADT's
Compare the code for this Class with that of the StudenRecordNode. This Class has only one data field - it could have more. DoubleNode.java
NameList.java The Applet does not do a lot. It allows you to add names to the list and then move a pointer to the currently selected. This uses the setNext() and setPrevious() methods of the DoubleNode Class. You could modify it easily to highlight the selected item in a different colour or with a coloured background. It would be useful, for example, for presenting a set of search results then allowing the user to select one.
Here we will flag the display routine that next or previous was pressed and move to the appropriate node before displaying the list. Strictly speaking it's not the best idea to get one method to do two tasks but, hey, everyone has lazy days :-)
next button was pressed, move current to next node (if it exists)
same deal for previous button.
as before
except for marking the current item Here is where you could add some color/colour.
If you know where the tail is, the head is always given by tail.getNext(), providing there is at least 1 item in the list.
FileNode.java
OrderedList.java
Notice how we split of the adding parts into their own methods to make this part of the code simpler.
A better approach would be to create a Class with all of the required methods. This would satisfy the mastery of ADT's requirement.
Now that you have seen this example you should be able to add nodes in order to the doubly-linked list. This could be worth up to 4 mastery factors. |
On this page: [ doubly-linked lists | NameList Applet | Circular List ] A simple modification of the Node Structure allows a reference field to point to the previous node as well as one pointing to the next:
In Java, the class might look like this: /** // The fields that links nodes together. public DoubleNode( ) Applet Code import java.applet.Applet; // data members Exercises Add a new nodes to the tail of the list instead of the head. Add new nodes in their correct order (difficult). If you succeed, add a tail pointer too. Then you can traverse the ordered list easily in any direction. In this type of linear data structure , the last node is pointed back to the head of the list instead of being a null reference. Depending on the use of the list, it is quite often more convenient to keep a tail pointer rather than a head pointer.
If there is only one node it is pointed at itself. The node structure is the same as for a "normal"inked list: /** public FileNode() // modifier methods Example Applet:
import java.applet.Applet; // data members Exercises Change the code so it adds nodes in reverse order. Add a button and a method to remove a node from the list. Add code to eliminate any duplicate nodes. A mini-project might be to create a Class that combines some of the above techniques to implement all the methods necessary to maintain a list in order. Page 66 of the Subject Guide suggests the methods that a list ADT should implement. Related: [ Java home | Previous: simple lists | Next: stacks ] |
|
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008 |