You are on page 1of 26

CHAPTER 11

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.9 shows an empty linked list.

Figure 11.9 Linked lists


Notation : - Connection between two nodes: line
- End of the line: arrowhead.
- Other end: solid circle.

Figure 11.10 The concept of copying and storing pointers


11.3.1 Arrays versus linked lists
- Both are representations of a list of items in memory.
- Difference: the way in which the items are linked together.
Figure 11.11 compares two representations: list of five
integers.

Figure 11.11 Array versus linked list


Array :
-Contiguous
-Access quickly
-Requires movement of many elements
-Size is fixed
Linked list:
-Scattered throughout memory
-Access in order, linear access mechanism
-The length can change any time, making it a
dynamic data structure
-Insertion and Deletion only requires re-
assignment of a few pointer
11.3.2 Linked list names versus nodes names
- A linked list must have a name.
- Name of linked list: name of head pointer - first node of
list. Nodes do not have explicit names in linked list, just
implicit ones.

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

Algorithm 11.6 shows the pseudocode.


Traversing a linked list
- Traverse the list, use “walking” pointer.
- “Walking” pointer is a pointer that moves from node to
node as each element is processed.
- Setting the walking pointer to the first node in list. Using
loop, continue with all data in the list.
- (Figure 11.20).
Figure 11.20 Traversing a linked list
11.3.4 Applications of linked lists
- Very efficient sorted list, many insertions and deletions.
- A dynamic data structure, can start with no nodes and then
grow as new nodes. Node can be deleted without moving
others.

A linked list is a suitable structure if a large number


of insertions and deletions are needed, but searching
a linked list is slower that searching an array.

You might also like