Professional Documents
Culture Documents
Data Structure
11-3 LINKED LISTS
- A collection of data in which each element contains
the location of the next element.
- Each element contains two parts: data and link.
-Name of the list is the same as name of this pointer
variable.
-Data element representing the information in the
current position of the list
-A Link/Pointer/Address to the next node in the list
Figure 11.9 shows a linked list: scores - contains four elements.
-An empty linked list to be only a null pointer.
-No node in Linked lists has its data portion empty.
Figure 11.12 The name of a linked list versus the names of nodes
11.3.3 Operations on linked lists
Operations were defined for array can be applied to linked
list.
Searching a linked list
- Nodes have no names, two pointers, pre (previous) and cur
(current) are used.
- Begin searching:
+ Pre pointer is null and cur pointer points to first node.
+ Search algorithm moves two pointers together towards the
end of list.
Figure 11.13 Moving of pre and cur pointers in searching a linked list
Figure 11.14 Values of pre and cur pointers in different cases
Inserting a node
- Before insertion, apply searching algorithm.
-If the flag returned from searching algorithm is false, allow
insertion, otherwise abort the insertion algorithm.
Empty list.
Beginning of the list.
End of the list.
Middle of the list.
Figure 11.15 Inserting a node at the beginning of a linked list
Figure 11.16 Inserting a node at the end of the linked list
Figure 11.17 Inserting a node in the middle of the linked list
Deleting a node
- Before deleting, apply search algorithm.
-If the flag returned from search algorithm is true (the node
is found), delete the node from linked list.
(deletion is simpler than insertion) Two cases:
First node
Other node.
Figure 11.18 Deleting the first node of a linked list
Figure 11.19 Deleting a node at the middle or end of a linked list
Retrieving a node
- Randomly accessing a node for purpose of inspecting or
copying the data contained.
- Before retrieving, apply search algorithm. If data item is
found => retrieved, otherwise process is aborted.
- Uses only the cur pointer (found by search algorithm).