Chapter 4
Chapter 4
LINKED LISTS
1 06/10/2025
Introduction
Linked lists and arrays are similar since they both
4 06/10/2025
Linked List Concepts:
List is a collection of elements i.e organized sequentially (finite sequence)
A linked list is a linear collection of data structure
Its is a collection of data elements called nodes storing data and links to other
nodes.
A node contains some information useful for a specific application and a pointer
Head node is the first element in the list and key is a value in the
information field which contains the storage address of the succossor
node
The data items in the linked list are not in consecutive memory locations.
5 06/10/2025
Cont…
Generally linked list is a linear collection
of connected nodes
6 06/10/2025
Cont…
Head(start) :pointer to the first node
7 06/10/2025
Composition of a Linked List
A linked list is called “linked” because each node
in the series
9 06/10/2025
Definition of link list (node of list)
Struct Node
{
data type data;
Node * next; // pointer to next node in the list
};
Example:
struct node
{ char name[20]; // Name of up to 20 letters
int age ;
float height;
node *nxt;// Pointer to next node
};
struct node *start_ptr = NULL;
10 06/10/2025
Advantages of linked lists:
Linked lists have many advantages:
Linked lists are dynamic data structures.
12 06/10/2025
Types of Linked Lists:
17 06/10/2025
Single Linked List
The beginning of the linked list is stored in a "start or
and so on.
The last node in the list has its next field set to NULL to
• Creation.
• Insertion.
• Deletion.
• Traversing.
19 06/10/2025
Creating a node for Single Linked List:
Creating a singly linked list starts with creating a node.
20 06/10/2025
Single linked list with 4 node
21 06/10/2025
The function createlist(), is used
to create ‘n’ number of nodes
22 06/10/2025
Insertion of a Node:
One of the most primitive operations that can be
24 06/10/2025
Inserting a node at the beginning:
The following steps are to be followed to insert a new
node at the beginning of the list:
newnode = getnode();
start = newnode;
25 06/10/2025
shows inserting a node into the single linked
list at the beginning
26 06/10/2025
The function for inserting a node at the
beginning
27 06/10/2025
Inserting a node at the end:
Get the new node using getnode()
newnode = getnode();
temp = start;
29 06/10/2025
The function for inserting a node at the end.
30 06/10/2025
Inserting a node at middle
position:
31 06/10/2025
Deletion of a node:
Another primitive operation that can be done in a
deleted.
A node can be deleted from the list from three
temp = start;
free(temp);
33 06/10/2025
shows deleting a node at the
beginning of a single linked list
34 06/10/2025
The function used for deleting the first
node in the list.
35 06/10/2025
Deleting a node at the end:
If list is empty then display ‘Empty List’ message.
prev=temp;
free(temp);
36 06/10/2025
shows deleting a node at the end
of a single linked list
37 06/10/2025
The function is used for deleting the
last node in the list.
38 06/10/2025
Double linked List
A double linked list is a two-way list in which all
nodes will have two links.
This helps in accessing both successor node and
predecessor node from the given node position.
It provides bi-directional traversing. Each node
contains three fields:
• Left link.(Pointer to previous node)
• Data.
• Right link.(pointer to next node)
The left link points to the predecessor node and
the right link points to the successor node.
The data field stores the required data.
39 06/10/2025
Double linked list
Many applications require searching forward
and backward through nodes of a list.
For example searching for a name in a
telephone directory would need forward and
backward scanning through a region of the
whole list.
40 06/10/2025
Operation on double linked List
The basic operations in a double linked list are:
Creation.
Insertion.
Deletion.
Traversing
41 06/10/2025
Creating Doubly Linked Lists
The nodes for a doubly linked list would be defined as
follows:
struct node
{
node *nxt; // Pointer to next node
43 06/10/2025
Node * temp= new Node;
// fill the information in temp
If (!tail)
{
Head= temp;
Tail = temp;
Temp->next=NULL;
Temp-> prev= NULL;
}
44 06/10/2025
Example
Else
{
Temp->next= head;
Head->prev = temp;
Head = temp;
Temp->prev= NULL;
}
45 06/10/2025
Inserting a node at the end:
Node * temp = new Node;
// store the information in new node
If (! tail)
{
Head = temp;
Tail = temp;
Temp-> next = NULL;
Temp->prev= NULL;
}
Else{
tail->next = temp;
Temp->prev= tail;
Temp->next= NULL;
Tail = temp;
}
46 06/10/2025
Inserting at end
47 06/10/2025
Deleting a node at the beginning:
The following steps are followed, to delete a
node at the beginning of the list:
• If list is empty then display ‘Empty List’
message.
• If the list is not empty, follow the steps given
below:
temp = start;
start = start -> right;
start -> left = NULL;
free(temp);
48 06/10/2025
Deleting at beginning
49 06/10/2025
Deleting a node at the end:
The following steps are followed to delete a node
at the end of the list:
• If list is empty then display ‘Empty List’ message
• If the list is not empty, follow the steps given
below:
temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);
50 06/10/2025
Deleting at end
51 06/10/2025
Traversal and displaying a list (Left to Right):
To display the information, you have to traverse the
list, node by node from the first node, until the end of
the list is reached.
The following steps are followed, to traverse a list from
left to right:
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
temp = start;
while(temp != NULL)
{
print temp-> data;
temp = temp -> right;
}
52 06/10/2025
Traversal and displaying a list (Right to Left):
To display the information from right to left, you have to traverse
the list, node by node from the first node, until the end of the list is
reached
If list is empty then display ‘Empty List’ message.
If the list is not empty, follow the steps given below:
temp = start;
while(temp -> right != NULL)
temp = temp -> right;
while(temp != NULL)
{
print temp -> data;
temp = temp -> left;
}
53 06/10/2025
Counting the Number of Nodes:
The following code will count the number of
nodes exist in the list (using recursion).
int countno de(no de *start)
{
if(start == NULL)
return 0;
else
return(1 + countno de(start ->right ));
}
54 06/10/2025
Lab exercise
Write A Complete Source Code for the
Implementation of Double Linked List
operation:
55 06/10/2025
Circular Linked List
In linear linked lists if a list is traversed (all the elements
56 06/10/2025
Linear linked List
57 06/10/2025
Circular linked list
58 06/10/2025
Cont…
list.
The header node can be separated from the others by either
struct node{
int info;
};
60 06/10/2025