Linked List
Alinked list is a linear data structure which can store a collection of "nodes" connected together via
links i.e. pointers.
Linked lists nodes are not stored at a contiguous location, rather they are linked using pointers to the
different memory locations.
A node consists of the data value and a pointer to the address of the next node within the linked list.
A linked list is a dynamic linear data structure whose memory size can be allocated or de-allocated at
run time based on the operation insertion or deletion, this helps in using system memory efficiently.
Linked lists can be used to implement various data structures like a stack, queue, graph, hash maps,
etc.
It mainly allows efficient insertion and deletion operations compared to arrays.
3.
• A linkedlist starts with a head node which points to the first node.
• Every node consists of data which holds the actual data (value)
associated with the node and a next pointer which holds the
memory address of the next node in the linked list.
• The last node is called the tail node in the list which points
to null indicating the end of the list.
4.
Components of LinkedList
1. Node
•The fundamental building block of a linked list.
•Contains two main parts:
•Data: The actual value stored in the node.
•Pointer/Reference: A link to the next node in the sequence.
2. Head
•The first node of the linked list.
•Used as the entry point to traverse the list.
3. Tail (Optional)
•The last node of the linked list.
•Usually, its pointer is set to NULL, indicating the end of the list.
4. Pointer (Next/Prev)
•Each node has a pointer that connects it to the next node.
•In a singly linked list, each node points only to the next node.
•In a doubly linked list, each node has two pointers: one to the next node and one to the previous node.
5. Length (Optional)
•Some implementations keep track of the total number of nodes for efficiency.
5.
Array vs LinkedList
Array: Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements
stored and this allows faster access to an element at a specific index.
Linked List: Linked lists are less rigid in their storage structure and elements are usually not stored in contiguous
locations, hence they need to be stored with additional tags giving a reference to the next element.
6.
Example
In asystem, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000,
2040].
If we want to insert a new ID 1005, then to maintain the sorted order, we have to move
all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used.
For example, to delete 1010 in id[], everything after 1010 has to be moved due to this so
much work is being done which affects the efficiency of the code.
7.
Representation of aLinked List in Memory
This representation of a linked list depicts that each node consists of two fields.
The first field consists of data, and the second field consists of pointers that point to another node.
8.
Types of LinkedList
Singly Linked List
Doubly Linked List
Circular Linked List
9.
Singly Linked Lists
Singly linked lists contain two "buckets" in one node; one bucket holds the data and the other
bucket holds the address of the next node of the list.
Traversals can be done in one direction only as there is only a single link between two nodes of
the same list.
10.
Doubly Linked Lists
Doubly Linked Lists contain three "buckets" in one node; one bucket holds the data and the
other buckets hold the addresses of the previous and next nodes in the list.
The list is traversed twice as the nodes in the list are connected to each other from both sides.
11.
Circular Linked Lists
Circular linked lists can exist in both singly linked list and doubly linked list.
Since the last node and the first node of the circular linked list are connected, the traversal in this linked
list will go on forever until it is broken.
12.
Circular Doubly LinkedList
A circular doubly linked list is defined as a circular linked list in which each node has two links
connecting it to the previous node and the next node.
13.
Operations on SinglyLinked List
A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a
reference to the next node in the linked list.
The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion
operations.
In a singly linked list, each node consists of two parts: data and a pointer to the next node.
This structure allows nodes to be dynamically linked together, forming a chain-like sequence.
14.
Operations on SinglyLinked List
Traversal
Searching
Length
Insertion:
Insert at the beginning
Insert at the end
Insert at a specific position
Deletion:
Delete from the beginning
Delete from the end
Delete a specific nodetraversal
• Searching
• Length
• Insertion:
• Insert at the beginning
• Insert at the end
• Insert at a specific position
• Deletion:
• Delete from the beginning
• Delete from the end
• Delete a specific node
15.
Traversal of SinglyLinked List
Traversal involves visiting each node in the linked list and performing some operation on the data.
A simple traversal function would print or process the data of each node.
Step-by-step approach:
Initialize a pointer current to the head of the list.
Use a while loop to iterate through the list until the current pointer reaches NULL.
Inside the loop, print the data of the current node and move the current pointer to the next node.
16.
Example
// Function totraverse and print the elements
// of the linked list
void traverseLinkedList(struct Node* head)
{
// Start from the head of the linked list
struct Node* current = head;
// Traverse the linked list until reaching the end (NULL)
while (current != NULL) {
// Print the data of the current node
printf("%d ", current->data);
// Move to the next node
current = current->next;
}
printf("n");
}
17.
Searching in SinglyLinked List
Searching in a Singly Linked List refers to the process of looking for a specific element or value within the
elements of the linked list.
Step-by-step approach:
Traverse the Linked List starting from the head.
Check if the current node's data matches the target value.
If a match is found, return true.
Otherwise, Move to the next node and repeat steps 2.
If the end of the list is reached without finding a match, return false.
18.
Example:
// Function tosearch for a value in the Linked List
bool searchLinkedList(struct Node* head, int target)
{
// Traverse the Linked List
while (head != NULL) {
// Check if the current node's
// data matches the target value
if (head->data == target) {
return true; // Value found
}
// Move to the next node
head = head->next;
}
return false; // Value not found
}
19.
Length of SinglyLinked List
Finding Length in Singly Linked List refers to the process of determining the total number of nodes in a singly linked
list.
Step-by-step approach:
Initialize a counter length to 0.
Start from the head of the list, assign it to current.
Traverse the list:
Increment length for each node.
Move to the next node (current = current->next).
Return the final value of length.
20.
// Definition ofa Node in a singly linked list
struct Node {
int data; // Data part of the node
struct Node* next; // Pointer to the next node in the list
};
// Function to find the length of the linked list
int findLength(struct Node* head)
{
// Initialize a counter for the length
int length = 0;
// Start from the head of the list
struct Node* curr = head;
// Traverse the list and increment
// the length for each node
while (curr != NULL) {
length++;
curr = curr->next;
}
// Return the final length of the linked list
return length;}
21.
Insertion in SinglyLinked List
Insertion is a fundamental operation in linked lists that involves adding a new node to the list.
22.
Step-by-step approach:
Createa new node with the given value.
Set the next pointer of the new node to the current head.
Move the head to point to the new node.
Return the new head of the linked list.
Example:
// Function to insert a new node at the beginning of the linked list
struct Node* insertAtBeginning(struct Node* head, int value)
{
// Create a new node with the given value
struct Node* new_node = newNode(value);
// Set the next pointer of the new node to the current head
new_node->next = head;
// Move the head to point to the new node
head = new_node;
// Return the new head of the linked list
return head;
}
23.
Insertion at theEnd of Singly Linked List:
To insert a node at the end of the list, traverse the list until the last node is reached, and
then link the new node to the current last node-
24.
Step-by-step approach:
Createa new node with the given value.
Check if the list is empty:
If it is, make the new node the head and return.
Traverse the list until the last node is reached.
Link the new node to the current last node by setting the last node's next pointer to the new node.
25.
Insertion at aSpecific Position of the Singly Linked List:
To insert a node at a specific position, traverse the list to the desired position, link the new node to the next node, and
update the links accordingly.
26.
Deletion in SinglyLinked List
Deletion involves removing a node from the linked list.
Similar to insertion, there are different scenarios for deletion:
Deletion at the Beginning of Singly Linked List:
To delete the first node, update the head to point to the second node in the list.
27.
Steps-by-step approach:
Checkif the head is NULL.
If it is, return NULL (the list is empty).
Store the current head node in a temporary variable temp.
Move the head pointer to the next node.
Delete the temporary node.
Return the new head of the linked list.
28.
Example:
// Function toremove the first node of the linked list
struct Node* removeFirstNode(struct Node* head)
{
if (head == NULL)
return NULL;
// Move the head pointer to the next node
struct Node* temp = head;
head = head->next;
// Free the memory of the old head
free(temp);
return head;
}
29.
Deletion at theEnd of Singly Linked List:
To delete the last node, traverse the list until the second-to-last node and update its
next field to None.
30.
Step-by-step approach:
Checkif the head is NULL.
If it is, return NULL (the list is empty).
Check if the head's next is NULL (only one node in the list).
If true, delete the head and return NULL.
Traverse the list to find the second last node (second_last).
Delete the last node (the node after second_last).
Set the next pointer of the second last node to NULL.
Return the head of the linked list.
31.
Deletion at aSpecific Position of Singly Linked List:
To delete a node at a specific position, traverse the list to the desired position, update the links to bypass the node to
be deleted.
32.
Step-by-step approach:
Checkif the list is empty or the position is invalid, return if so.
If the head needs to be deleted, update the head and delete the node.
Traverse to the node before the position to be deleted.
If the position is out of range, return.
Store the node to be deleted.
Update the links to bypass the node.
Delete the stored node.