Introducing linked lists
The linked list is an important and popular data structure with the following properties:
- The data elements are stored in memory in different locations that are connected through pointers. A pointer is an object that can store the memory address of a variable, and each data element points to the next data element and so on until the last element, which points to
None
. - The length of the list can increase or decrease during the execution of the program.
Contrary to arrays, linked lists store data items sequentially in different locations in memory, wherein each data item is stored separately and linked to other data items using pointers. Each of these data items is called a node. More specifically, a node stores the actual data and a pointer. In Figure 4.2, nodes A and B store the data independently, and node A is connected to node B.
Figure 4.2: A linked list with two nodes
Moreover, the nodes can have links to other nodes...