Escolar Documentos
Profissional Documentos
Cultura Documentos
Linked List
i. the self-referential class ii. Node iii. link field iv. link
Self-referential class: a class with at least one field whose reference type is the class name:
private int empno;
class Employee {
// Other members
}
Link: the reference in a link field. In the code fragment above, next's
reference to an Employee node is a link.
Definition
Figure 1
You can create many kinds of linked lists, three popular linked list variants
are the single, double, and circular linked lists. Let's explore those variants, beginning with the singly linked list.
Figure 2
Insertion
This is the simplest case. Create a Node, assign its reference to top, initialize
its nonlink field, and assign NULL to its link field. The following pseudocode performs those tasks:
top = NEW Node top.name = "A" top.next = NULL
Figure 3 shows the initial singly linked list that emerges from the
pseudocode above.
Figure 3. The initial singly linked list contains the solitary Node A
Create a Node, initialize its nonlink field, assign top's reference to the next
link field, and assign the newly created Node's reference to top. The following pseudocode (which assumes that the previous pseudocode has executed) performs those tasks:
DECLARE Node temp temp = NEW Node temp.name = "B" temp.next = top top = temp
Create a Node, initialize its nonlink field, assign NULL to the link field, traverse the singly linked list to find the last Node, and assign the newly created Node's reference to the last Node's next link field. The following pseudocode performs those tasks:
temp = NEW Node temp.name = "C" temp.next = NULL DECLARE Node temp2
temp2 = top
// We assume top (and temp2) are not NULL because of the previous pseudocode
WHILE temp2.next IS NOT NULL temp2 = temp2.next END WHILE // temp2 now references the last node temp2.next = temp
Figure 5 reveals the list following the insertion of Node C after Node A.
Figure 5. Node C comes last in the expanded three-node singly linked list
This is the most complex case. Create a Node, initialize its nonlink field, traverse
the list to find the Node that appears before the newly created Node to be inserted, assign the link in that previous Node's next link field to the newly created Node's next link field, and assign the newly created Node's reference to the previous Node's next link field. The following pseudocode performs those tasks:
Figure 6 presents the list following the insertion of Node D between Nodes A and C.
Figure 6 presents the list following the insertion of Node D between Nodes A and C.
Searching
Node temp = top // We use temp and not top. If top were used, we
// couldn't access the singly linked list after // the search finished because top would refer // to the final Node. WHILE temp.next IS NOT NULL temp = temp.next END WHILE
WHILE temp IS NOT NULL AND temp.name IS NOT "A" // Search for "A".
temp = temp.next END WHILE
Deletion
Figure 7 presents before and after views of a list where the first Node is
deleted. In that figure, Node C disappears and Node B becomes the first Node.
Figure 7. Before and after views of a singly linked list where the first Node is deleted.
Locate the Node that precedes the Node to be deleted and assign the link in the
Node-to-be-deleted's next link field to the preceding Node's next link field. The following pseudocode (which assumes Figure 6's linked list and extends that figure's associated pseudocode) deletes Node D:
temp = top WHILE temp.name IS NOT "A" temp = temp.next END WHILE // We assume that temp references Node A // Node D no longer exists temp.next = temp.next.next
Figure 8 presents before and after views of a list where an intermediate Node is
deleted. In that figure, Node D disappears.
Figure 8. Before and after views of a singly linked list where an intermediate Node is deleted.