2
Most read
3
Most read
4
Most read
Linked Lists
What is Linked List
A linked list is a linear data structure, in which the elements are not
stored at contiguous memory locations. The elements in a linked list
are linked using pointers as shown in the below image:
Linked-List-Data-Structure
In simple words, a linked list consists of nodes where each node
contains a data field and a reference(link) to the next node in the list.
Understanding the basics of Linked List
Linked List is a linear data structure, in which elements are not
stored at a contiguous location, rather they are linked using
pointers. Linked List forms a series of connected nodes, where each
node stores the data and the address of the next node.
Node Structure: A node in a linked list typically consists of two
components:
Data: It holds the actual value or data associated with the node.
Next Pointer: It stores the memory address (reference) of the next
node in the sequence.
Head and Tail: The linked list is accessed through the head node,
which points to the first node in the list. The last node in the list
points to NULL or nullptr, indicating the end of the list. This node is
known as the tail node.
Why linked list data structure needed?
Here are a few advantages of a linked list that is listed below, it will
help you understand why it is necessary to know.
 Dynamic Data structure: The size of memory can be allocated
or de-allocated at run time based on the operation insertion or
deletion.
 Ease of Insertion/Deletion: The insertion and deletion of
elements are simpler than arrays since no elements need to be
shifted after insertion and deletion, Just the address needed to be
updated.
 Efficient Memory Utilization: As we know Linked List is a
dynamic data structure the size increases or decreases as per the
requirement so this avoids the wastage of memory.
 Implementation: Various advanced data structures can be
implemented using a linked list like a stack, queue, graph, hash
maps, etc.
Types of linked lists:
There are mainly three types of linked lists:
1. Single-linked list
2. Double linked list
3. Circular linked list
1. Single-linked list:
In a singly linked list, each node contains a reference to the next
node in the sequence. Traversing a singly linked list is done in a
forward direction.
Single-linked list
// A Single linked list node
struct Node {
int data;
struct Node* next;
};
2. Double-linked list:
In a doubly linked list, each node contains references to both the
next and previous nodes. This allows for traversal in both forward
and backward directions, but it requires additional memory for the
backward reference.
Double-linked list
/* Node of a doubly linked list */
struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
3. Circular linked list:
In a circular linked list, the last node points back to the head node,
creating a circular structure. It can be either singly or doubly linked.
Circular linked list
Syntax:
struct node{
<data__type> data;
struct node* next;
};
Operations on Linked Lists
1. Insertion: Adding a new node to a linked list involves adjusting
the pointers of the existing nodes to maintain the proper
sequence. Insertion can be performed at the beginning, end, or
any position within the list
Insertion in Linked List
Given a Linked List, the task is to insert a new node in this given
Linked List at the following positions:
 At the front of the linked list
 After a given node.
 At the end of the linked list.
How to Insert a Node at the Front/Beginning of Linked List
Approach:
To insert a node at the start/beginning/front of a Linked List, we
need to:
 Make the first node of Linked List linked to the new node
 Remove the head from the original first node of Linked List
 Make the new node as the Head of the Linked List.
Below is the implementation of the approach in C
/* Given a reference (pointer to pointer) to the head of a
list
and an int, inserts a new node on the front of the list.
*/
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}
How to Insert a Node after a Given Node in Linked List
Approach:
To insert a node after a given node in a Linked List, we need to:
 Check if the given node exists or not.
 If it do not exists,
 terminate the process.
 If the given node exists,
 Make the element to be inserted as a new node
 Change the next pointer of given node to the
new node
 Now shift the original next pointer of given
node to the next pointer of new node
Below is the implementation of the approach:
/* Given a node prev_node, insert a new node after the given
prev_node */
void insertAfter(struct Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL) {
printf("the given previous node cannot be NULL");
return;
}
/* 2. allocate new node */
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. move the next of prev_node as new_node */
prev_node->next = new_node;
}
How to Insert a Node at the End of Linked List
Approach:
To insert a node at the end of a Linked List, we need to:
 Go to the last node of the Linked List
 Change the next pointer of last node from NULL to the new node
 Make the next pointer of new node as NULL to show the end of
Linked List
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so
make next of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new
* node as head */
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}
2. Deletion: Removing a node from a linked list requires adjusting
the pointers of the neighboring nodes to bridge the gap left by
the deleted node. Deletion can be performed at the beginning,
end, or any position within the list.
3. Searching: Searching for a specific value in a linked list
involves traversing the list from the head node until the value is
found or the end of the list is reached.
Advantages of Linked Lists
 Dynamic Size: Linked lists can grow or shrink dynamically, as
memory allocation is done at runtime.
 Insertion and Deletion: Adding or removing elements from a
linked list is efficient, especially for large lists.
 Flexibility: Linked lists can be easily reorganized and modified
without requiring a contiguous block of memory.
Disadvantages of Linked Lists
 Random Access: Unlike arrays, linked lists do not allow direct
access to elements by index. Traversal is required to reach a
specific node.
 Extra Memory: Linked lists require additional memory for
storing the pointers, compared to arrays.
Conclusion:
Linked lists are versatile data structures that provide dynamic
memory allocation and efficient insertion and deletion operations.
Understanding the basics of linked lists is essential for any
programmer or computer science enthusiast. With this knowledge,
you can implement linked lists to solve various problems and
expand your understanding of data structures and algorithms.

Linked list.docx

  • 1.
    Linked Lists What isLinked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image: Linked-List-Data-Structure In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. Understanding the basics of Linked List Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather they are linked using pointers. Linked List forms a series of connected nodes, where each node stores the data and the address of the next node.
  • 2.
    Node Structure: Anode in a linked list typically consists of two components: Data: It holds the actual value or data associated with the node. Next Pointer: It stores the memory address (reference) of the next node in the sequence. Head and Tail: The linked list is accessed through the head node, which points to the first node in the list. The last node in the list points to NULL or nullptr, indicating the end of the list. This node is known as the tail node. Why linked list data structure needed? Here are a few advantages of a linked list that is listed below, it will help you understand why it is necessary to know.  Dynamic Data structure: The size of memory can be allocated or de-allocated at run time based on the operation insertion or deletion.  Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than arrays since no elements need to be shifted after insertion and deletion, Just the address needed to be updated.  Efficient Memory Utilization: As we know Linked List is a dynamic data structure the size increases or decreases as per the requirement so this avoids the wastage of memory.  Implementation: Various advanced data structures can be implemented using a linked list like a stack, queue, graph, hash maps, etc.
  • 3.
    Types of linkedlists: There are mainly three types of linked lists: 1. Single-linked list 2. Double linked list 3. Circular linked list 1. Single-linked list: In a singly linked list, each node contains a reference to the next node in the sequence. Traversing a singly linked list is done in a forward direction. Single-linked list // A Single linked list node struct Node { int data; struct Node* next; }; 2. Double-linked list: In a doubly linked list, each node contains references to both the next and previous nodes. This allows for traversal in both forward and backward directions, but it requires additional memory for the backward reference.
  • 4.
    Double-linked list /* Nodeof a doubly linked list */ struct Node { int data; struct Node* next; // Pointer to next node in DLL struct Node* prev; // Pointer to previous node in DLL }; 3. Circular linked list: In a circular linked list, the last node points back to the head node, creating a circular structure. It can be either singly or doubly linked. Circular linked list Syntax: struct node{ <data__type> data;
  • 5.
    struct node* next; }; Operationson Linked Lists 1. Insertion: Adding a new node to a linked list involves adjusting the pointers of the existing nodes to maintain the proper sequence. Insertion can be performed at the beginning, end, or any position within the list Insertion in Linked List Given a Linked List, the task is to insert a new node in this given Linked List at the following positions:  At the front of the linked list  After a given node.  At the end of the linked list. How to Insert a Node at the Front/Beginning of Linked List Approach: To insert a node at the start/beginning/front of a Linked List, we need to:  Make the first node of Linked List linked to the new node  Remove the head from the original first node of Linked List  Make the new node as the Head of the Linked List. Below is the implementation of the approach in C /* Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the front of the list. */ void push(struct Node** head_ref, int new_data) {
  • 6.
    /* 1. allocatenode */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make next of new node as head */ new_node->next = (*head_ref); /* 4. move the head to point to the new node */ (*head_ref) = new_node; } How to Insert a Node after a Given Node in Linked List Approach: To insert a node after a given node in a Linked List, we need to:  Check if the given node exists or not.  If it do not exists,  terminate the process.  If the given node exists,  Make the element to be inserted as a new node  Change the next pointer of given node to the new node  Now shift the original next pointer of given node to the next pointer of new node Below is the implementation of the approach: /* Given a node prev_node, insert a new node after the given
  • 7.
    prev_node */ void insertAfter(structNode* prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf("the given previous node cannot be NULL"); return; } /* 2. allocate new node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* 3. put in the data */ new_node->data = new_data; /* 4. Make next of new node as next of prev_node */ new_node->next = prev_node->next; /* 5. move the next of prev_node as new_node */ prev_node->next = new_node; } How to Insert a Node at the End of Linked List Approach: To insert a node at the end of a Linked List, we need to:  Go to the last node of the Linked List  Change the next pointer of last node from NULL to the new node  Make the next pointer of new node as NULL to show the end of Linked List /* Given a reference (pointer to pointer) to the head
  • 8.
    of a listand an int, appends a new node at the end */ void append(struct Node** head_ref, int new_data) { /* 1. allocate node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = *head_ref; /* used in step 5*/ /* 2. put in the data */ new_node->data = new_data; /* 3. This new node is going to be the last node, so make next of it as NULL*/ new_node->next = NULL; /* 4. If the Linked List is empty, then make the new * node as head */ if (*head_ref == NULL) { *head_ref = new_node; return; } /* 5. Else traverse till the last node */ while (last->next != NULL) last = last->next; /* 6. Change the next of last node */ last->next = new_node; return; }
  • 9.
    2. Deletion: Removinga node from a linked list requires adjusting the pointers of the neighboring nodes to bridge the gap left by the deleted node. Deletion can be performed at the beginning, end, or any position within the list. 3. Searching: Searching for a specific value in a linked list involves traversing the list from the head node until the value is found or the end of the list is reached. Advantages of Linked Lists  Dynamic Size: Linked lists can grow or shrink dynamically, as memory allocation is done at runtime.  Insertion and Deletion: Adding or removing elements from a linked list is efficient, especially for large lists.  Flexibility: Linked lists can be easily reorganized and modified without requiring a contiguous block of memory. Disadvantages of Linked Lists  Random Access: Unlike arrays, linked lists do not allow direct access to elements by index. Traversal is required to reach a specific node.  Extra Memory: Linked lists require additional memory for storing the pointers, compared to arrays. Conclusion: Linked lists are versatile data structures that provide dynamic memory allocation and efficient insertion and deletion operations. Understanding the basics of linked lists is essential for any programmer or computer science enthusiast. With this knowledge, you can implement linked lists to solve various problems and expand your understanding of data structures and algorithms.