Linked Lists
What is a linked list?
A linked list is a dynamic data structure that store item in nodes.
Can dynamically add new items, it has no fixed size
Arrays can mimic this but they essentially create copies of themselves with new dimensions when resizing
This has implications for both memory and processor usage
Nodes
Each node in a linked list has a pointer which points to the address of the next node. Each element of a linked list is called a node.
The start of a linked list is called the HEAD and the last element points to the NULL. Each node has its own address in memory, and stores the data item and a pointer to the next node. The following diagram represents a node.
Example Single Linked List
A simple example of a single linked list with four nodes is shown below. The four node linked list stores the words ‘Computing’, ‘Science’, ‘is’, and 'fun'.
Single Linked List
Points to note about Linked Lists
A linked list can store data of multiple data types; a 1-D array is usually limited to one.
A linked list is a linear data structure; to get to a specific data item, it must always start at the HEAD and work through each node until the data is found.
A single linked list can only be traversed in one direction, from HEAD to NULL.
Inserting and deleting data into/from a linked list is more efficient than a 1-D array
When inserting only a pointer is changed rather than shifting the contents of the list (array) into different memory locations.
When deleting only a pointer is changed rather than shifting the contents of the list (array) into different memory locations.
Single Linked List Operations - Inserting
To insert new data into the list, for example inserting the word ‘really’ between ‘is’ and ‘fun’.
A new node is created somewhere in memory.
The list is traversed until ‘is’ is found.
The pointers for the node (is) is updated to refer to the new node.
The pointer for the new node is set as the next existing item in the list.
Comparison - Before and After
Single Linked List Operations - Deleting
To remove data from the list, for example removing the word ‘Science’
The memory location where the node is stored is freed up.
The list is traversed until the node before ‘Science’ is found.
The pointer before science is updated to the pointer for the node after ‘Science’.
Comparison - Before and After
Linked List Operations
Adding a new node at the end
The pointer of the last node will have to point to the newly created node.
Inserting between existing nodes
When a new node is added then the pointer of the previous node has to be amended to point to the new node and the new node has to point to the node that was previously next in the list.
Deleting nodes
The pointer of the previous node before the item to be deleted will be amended to point to the next item in the list.
Traversing lists
Each node has to be visited and the pointer followed to the address of the next node.
What is a double linked list?
A double linked list is very similar to a single linked list, but has an additional pointer in each node that stores the address of the previous node. This means the list can be traversed in both directions.
The following diagram represents a node in a double linked list.
Example Double Linked List
As there is an extra pointer in each node it can store the address f the previous and the next node.
Double Linked List Operations - Inserting a new Node
To insert new data into the list, for example inserting the word ‘really’ between ‘is’ and ‘fun’
A new node is created somewhere in memory
The list is traversed until ‘is’ is found.
The pointers for the node (is) is updated to refer to the new node
The previous pointer for the new node is set as the next existing item in the list (is).
The next pointer for the new node is set to the address for the next node ( fun)
Comparison - Before and After
Double Linked List Operations - Deleting a Node
To remove data from the list, for example removing the word ‘really’
The memory location where the node is stored is freed up
The list is traversed until the node before ‘really’ is found.
The next node pointer before really is updated to the pointer for the node after ‘really’
The previous node pointer of the next item in the list is set to the new previous node ('is)
Comparison - Before and After
Types of Linked Lists: Summary
Single - Nodes have a data item as well as a pointer to the next field. The last node will contain a NULL pointer.
Double/Doubly - This is when each node will have a pointer to the next AND previous nodes.
Advantages and Disadvantages vs Arrays
Advantages
Linked lists are much more efficient in terms of memory and processor usage particularly when adding new items (as they don’t need to resize).
Inserting, deleting and re-ordering only requires pointer(s) to be changed .
Disadvantages
Cannot index a particular node of a single linked list as in an array, you have to traverse the linked list in order to reach a particular node (sequential vs direct).
Requires storing of additional pointers.
Nodes may not be stored contiguously (occupy adjacent areas of RAM).