linked list

25
Data structures and Algorithms Linked List

Upload: aamir2686

Post on 10-May-2017

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked List

Data structures and AlgorithmsLinked List

Page 2: 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 classii. Nodeiii.link fieldiv. link

Page 3: Linked List

The linked-list datastructure• Self-referential class: a class with at least one field whose

reference type is the class name:class Employee {

private int empno; private String name; private double salary; public Employee next; // Other members

}

Page 4: Linked List

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.

Page 5: Linked List

Definition

• a linked list is a sequence of nodes that interconnect via the links in their link fields.

Page 6: Linked List

The linked-list datastructure

Figure 1

Page 7: Linked List

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.

Page 8: Linked List

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.

Page 9: 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 three-node 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

Page 10: Linked List

Singly linked listsDECLARE 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:

Page 11: Linked List

Insertion

Page 12: Linked List

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 Nodetop.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

Page 13: Linked List

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 temptemp = NEW Nodetemp.name = "B"temp.next = toptop = 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

Page 14: Linked List

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 Nodetemp.name = "C"temp.next = NULLDECLARE Node temp2temp2 = top // We assume top (and temp2) are not NULL because of the previous pseudocodeWHILE temp2.next IS NOT NULL temp2 = temp2.nextEND WHILE // temp2 now references the last nodetemp2.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

Page 15: 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:

Page 16: Linked List

The node must be inserted between two nodestemp = NEW Nodetemp.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.nextEND WHILE // temp2 now references Node A.temp.next = temp2.nexttemp2.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.

Page 17: Linked List

Searching

Page 18: Linked List

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.nextEND WHILE// temp now references the last Node.

Page 19: Linked List

Search for a specific Node

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

Node temp = topWHILE temp IS NOT NULL AND temp.name IS NOT "A" // Search for "A". temp = temp.nextEND WHILE// temp either references Node A or contains NULL if Node A not found.

Page 20: Linked List

Deletion

Page 21: Linked List

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:

Page 22: Linked List

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.

Page 23: Linked List

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 = topWHILE temp.name IS NOT "A" temp = temp.nextEND WHILE // We assume that temp references Node Atemp.next = temp.next.next // Node D no longer exists

• Figure 8 presents before and after views of a list where an intermediate Node is deleted. In that figure, Node D disappears.

Page 24: Linked List

Delete any node but the first node

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

Page 25: Linked List