Linked List
Here we will discuss linked lists, what they are, and when to use them.
What is a linked list?
A linked list is a linear collection of data elements. It consists of a number of nodes which represent a sequence. Unlike arrays, data in a linked list is not stored contiguously in memory, and the items' placement in memory does not determine their order. Instead, each node in a list contains a reference to the next node in the list.
Why use them?
Linked lists allow for insertion and removal of data without having to reallocate or reorganize the entire structure. They allow for insertion and removal of nodes at any point in the list with a constant amount of operations. However, random access is not possible; access time in a linked list is linear.
What are they used for?
Linked lists are one of the simplest and most common data structures. They are often used to implement other data structures (stacks, queues, hash tables, etc.) and are useful when an array-like structure without a fixed size is needed.
Implementation
Basic Structure
A linked list can be thought of as a group of nodes that are connected to each other. Each node contains data and a reference to the next node in the list. An implementation in C is displayed below. Note that here we utilize a separate list
structure to organize our data, but it is also common to work directly with the nodes, without such a structure.
Our list structure consists of three pieces of data: a pointer to the list head, a pointer to the list tail, and the list size. The head pointer in a linked list is a reference to its first item. It is not a separate node itself. The tail pointer is a reference to the last node in the list. If the list is empty, both are NULL
. The size
variable is here for convenience, so that we do not have to manually count the number of items each time we need the size of the list.
And here is a representation of what a list looks like:
Traversal and Accessing Data
Traversal is the process of visiting nodes in a linked list. This is usually done to access the data inside nodes. To do this, we create a new pointer to the head of the list and move it using the next
pointer in each node:
The loop will terminate when current
is NULL
, which happens when calling current->next
from the last item in the list.
Adding Items
To add an item to a linked list we create a new node and connect it to the existing nodes. If the list is empty, then we just set the head and tail to point to the new node. Otherwise, we traverse the list and add the node. Here is an example of adding a node to the end of a list:
Here is an example of adding to the front of a list:
And here is inserting into the middle of a list:
Removing Items
Removing items from a linked list is slightly more complex than adding them. When removing an item from a list, we must make sure to connect the items before and after the node being removed. Otherwise, the list will be split in two and no longer connected.
Here is an implementation of removing the last item in a list:
Here is removal from the front of a list:
And here is removing from the middle of the list:
Doubly-Linked Lists
A doubly-linked list is a linked list in which each node contains a pointer to both the next and the previous node:
Adding and removing items in a doubly-linked list is slightly different from a regular linked list (called a singly-linked list). Doubly-linked lists also allow us to traverse the list backwards.
Links
Last updated