|
|
||
| You are here: | ||
|
HL Mastery aspects: Linked lists ADT's
output each node's contents into the TextArea, one at a time.
The earth symbol is sometimes used to indicate a null pointer. In Node represenations, often a diagonal bar is put through the reference field:
StudentRecordNode source code
|
On this page: [ example list | Applet ] The Linked List Using Object References Using the object reference together with a record type Class allows the formation of lists linked by their reference fields:
A typical Class for defining such a linked list could be: /** A linked list of Student Nodes The use of this linked record structure to construct a list of linked student object instances is illustrated in the following Applet: /** * StudentList.java * * @author Mr J * @version 20050209 */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class StudentList extends Applet implements ActionListener add(addButton); add(textWindow); At the start of the program, root points to null: Suppose "Fred" and "Xyz" are in the text boxes (nameField and tutorField), we now have a new StudentRecordNode object created in memory: StudentRecordNode theNode = new StudentRecordNode( nameField.getText(), tutorField.getText(),
Later on in the program, root will be pointing to an existing list of nodes, so effectively we have now added this node to the head. However, it remains to point root back to this node as it is now the first in the list, so we use the line: // now we point the root pointer to this node To prove that it really does work, the linked list is displayed in a TextArea. Notice the way that this methods "walks" down the list starting at root. It uses a temp reference to point to each object in turn. One has to take care with methods that access linked lists, always use a while loop so that you don't generate an error when accessing an empty list (the root pointer is still null). The program as it stands is incomplete, in particular, nodes can only be added and not removed. Also nodes can only be added at the head (front, root) of the list. The following exercises will (hopefully) help your understanding of these useful structures: Add a new button and method to remove a node from the head of the list. Be careful to check that the list is not empty first! Try adding/removing nodes at the tail of the list instead of the head. You can use a method similar to displayList to get to the end node. Related: [ Java home | data structures home | Next: more lists ] |
This structure is called a node .
A list of linked nodes. A Flash movie illustrating adding to the head of a linked list. |
|
|
|||
|
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 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |