Você está na página 1de 25

Data structures and Algorithms

Linked List

The linked-list datastructure


In addition to the array, another datastructure that receives much use is the
linked list. The linked-list datastructure involves four concepts:

i. the self-referential class ii. Node iii. link field iv. link

The linked-list datastructure

Self-referential class: a class with at least one field whose reference type is the class name:
private int empno;

class Employee {

private String name;


private double salary; public Employee next;

// Other members
}

The linked-list datastructure


Node: an object you create from a self-referential class. Link field: a field whose reference type is the class name. In the code
fragment above, next is a link field. In contrast, empno, name, and salary are nonlink fields.

Link: the reference in a link field. In the code fragment above, next's
reference to an Employee node is a link.

Definition

a linked list is a sequence of nodes that interconnect via


the links in their link fields.

The linked-list datastructure

Figure 1

The linked-list datastructure


Figure 1 presents three nodes: A, B, and C. Each node divides into a contents
area (in orange) and one or more link areas (in green). The contents area represents all nonlink fields, and each link area represents a link field. A's single link area and C's link areas incorporate an arrow to signify a reference to some other node of the same type (or a subtype). B's single link area incorporates an X to signify a null reference. In other words, B doesn't connect to any other node.

The linked-list datastructure

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.

Singly linked lists


A singly linked list is a linked list of nodes, where each node has a single link
field. A reference variable holds a reference to the first node, each node (except the last) links to the next node, and the last node's link field contains null to signify the list's end. Although the reference variable is commonly named top, you can choose any name you want. Figure 2 presents a threenode singly linked list, where top references the A node, A connects to B, B connects to C, and C is the final node.

Figure 2

Singly linked lists


DECLARE CLASS Node DECLARE STRING name

DECLARE Node next


END DECLARE DECLARE Node top = NULL

The pseudocode above declares a Node self-referential class with a name


nonlink field and a next link field. The pseudocode also declares a top reference variable (of type Node) that holds a reference to the first Node in a singly linked list. Because the list does not yet exist, top's initial value is NULL. Each of the following four cases assumes the Node and top declarations:

Insertion

The singly linked list does not exist

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

The node must be inserted before the first node

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

The resulting two-Node list appears in Figure 4.


Figure 4. The expanded two-Node singly linked list places Node B ahead of Node A

The node must be inserted after the last node

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

The node must be inserted between two nodes

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:

The node must be inserted between two nodes


temp = NEW Node temp.name = "D" temp2 = top // We assume that the newly created Node is inserted after Node A and // that Node A exists. In the real world, there is no guarantee that any // Node exists, so we would need to check for temp2 containing NULL in // both the WHILE loop's header and after the WHILE loop completes. WHILE temp2.name IS NOT "A" temp2 = temp2.next END WHILE // temp2 now references Node A. temp.next = temp2.next temp2.next = temp

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

Search for the final Node

// Assume top references a singly linked list of at least one Node.

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

// temp now references the last Node.

Search for a specific Node

// Assume top references a singly linked list of at least one Node.


Node temp = top

WHILE temp IS NOT NULL AND temp.name IS NOT "A" // Search for "A".
temp = temp.next END WHILE

// temp either references Node A or contains NULL if Node A not found.

Deletion

Singly linked lists

A second common singly-linked list algorithm is node-deletion. Unlike


node-insertion, there are only two cases to consider: delete the first node and delete any node but the first node. Each case assumes a singly linked list with at least one node exists:

Delete the first node

Assign the link in the top-referenced Node's next field to top:


top = top.next; // Reference the second Node (or NULL if there is only one Node)

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.

Delete any node but the first node

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.

Delete any node but the first node

Figure 8. Before and after views of a singly linked list where an intermediate Node is deleted.

Você também pode gostar