Module 2 DSA T4 List SLL DLL CLL
Module 2 DSA T4 List SLL DLL CLL
BCSE202L -
COURSE INSTRUCTOR: DATA STRUCTURES AND
DR. OM KUMAR C.U. ALGORITHMS
ASSISTANT PROFESSOR,
SCOPE,VIT.
• Linked lists in C
A B C NULL
• There is a pointer (called header) points the first element (also called node)
allocation
a
15 10 15 20 22 25 30
b f k b d
45
c
25 35 40 45 50
d
50
e
22 h
f 10 a 15 i 20 e 22 c 25 g 30 f
35
g
30
h 35 k 40 b 45 d 50
10
i
20 h
j
10 15 20 22 25 30
k
40
DR. OM KUMAR C.U.
35 40 45 50 20/01/2025 8
Array versus Linked Lists
In Linked lists
or pointers
It is essentially a dynamic data structure
struct node
node
{
int data; /* Data */ Data
struct node *next; /* pointer*/ next
} ;
Note:
Such structures which contain a member field pointing to the same structure type are
called self-referential structures.
DR. OM KUMAR C.U. 20/01/2025 11
TYPES OF LISTS: SINGLE LINKED LIST
Depending on the way in which the links are used to maintain
adjacency, several different types of linked lists are possible.
head
A B C NULL
• A circular linked list is basically a linear linked list that may be single- or double-
linked.
• The only difference is that there is no any NULL value terminating the list.
• In fact in the list every node points to the next node and last node points to the
first node, thus forming a circle. Since it forms a circle with no end to stop it is
called as circular linked list.
• In circular linked list there can be no starting or ending node, whole node can
be traversed from any node.
• In order to traverse the circular linked list, only once we need to traverse entire
list until the starting node is not traversed again.
• A circular linked list can be implemented using both singly linked list and
doubly
DR. linked list.
OM KUMAR C.U. 20/01/2025 18
EXAMPLE 1: CREATING A SINGLE LINKED LIST
Linked list to store and print roll number, name and age of 3 students.
#include <stdio.h>
struct stud
{
int roll;
char name[30];
int age;
struct stud *next;
};
main()
{
struct stud n1, n2, n3;
struct stud *p;
scanf (“%d %s %d”, &n1.roll, n1.name, &n1.age);
scanf (“%d %s %d”, &n2.roll,n2.name, &n2.age);
scanf (“%d %s %d”, &n3.roll,n3.name, &n3.age);
DR. OM KUMAR C.U. 20/01/2025 19
EXAMPLE 1: CREATING A SINGLE LINKED
LIST
n1.next = &n2 ;
n2.next = &n3 ;
n3.next = NULL ;
/* Now traverse the list and print the elements */
p = &n1 ; /* point to 1st element */
while (p != NULL)
{
printf (“\n %d %s %d”, p->roll, p->name, p->age);
p = p->next;
}
}
struct stud
{
int roll;
char name[30];
int age;
struct stud *next;
};
Also assume the list with three nodes n1, n2 and n3 for 3 students.
n1.next = &n2 ;
n2.next = &n3 ;
n3.next = NULL ; /* No more nodes follow */
rol
naml
eag
e
nex NULL
t n1 n2 n3
C-program to store 10 values on a linked list reading the data from keyboard.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; //Data part
struct node *next; //Address part
}*header;
int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
DR. OM KUMAR C.U. createList(n); 20/01/2025 23
return 0;
}
EXAMPLE 2: CREATING A SINGLE LINKED LIST
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
header
100 NULL
It creates a single node. For example, if the data entered is 100 then the list
look like
DR. OM KUMAR C.U. 20/01/2025 26
Creating a single linked list
If we need n number of nodes in the linked list:
• Allocate n newNodes, one by one.
• Read in the data for the newNodes.
• Modify the links of the newNodes so that the chain is formed.
It creates n number of nodes . For e.g. if the data entered is 200, 50, 30 then
the list look like
hea
d 10 20 50 30 NULL
0 0
DR. OM KUMAR C.U. 20/01/2025 27
EXAMPLE 3: CREATING A SINGLE LINKED LIST
C-program to copy an array to a single linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; //Data part
struct node *next; //Address part
};
int main()
{
struct node *header, *newNode, *temp;
int data, i, n, a[100];
printf("Enter the total number of data: ");
scanf("%d", &n);
// Write code here to initialize the array a with n
elements //
DR. OM KUMAR C.U. 20/01/2025 28
...
EXAMPLE 2: CREATING A SINGLE LINKED LIST
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
newNode->data = a[i]; //Links the data field of newNode with a[i]
newNode->next = NULL; //Links the address field of newNode with
NULL
}
}
OPERATIONS ON LINKED LISTS
Once the linked list has been constructed and header points to the first node of the
list,
• Follow the pointers.
• Display the contents of the nodes as they are traversed.
• Stop when the next pointer points to null.
Insertion steps:
• Create a new node
• Start from the header node
• Manage links to
• Insert at front
• Insert at end
• Insert at any position
DR. OM KUMAR C.U. 20/01/2025 37
INSERTION AT FRONT
Step 3: Make the new node as the head node, i.e. now head node will point to newNode.
/*Create a new node and insert at the beginning of the linked list.*/
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = head; //Links the address part
Step 2: Traverse to the last node of the linked list and connect the last node of the list with
the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).
/* Create a new node and insert at the end of the linked list. */
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
Step 2: Traverse to the n-1th position of the linked list and connect the new node with the
n+1th node. (newNode->next = temp->next) where temp is the n-1th node.
Step 3: Now at last connect the n-1th node with the new node i.e. the n-1th node will now
point to new node. (temp->next = newNode) where temp is the n-1th node.
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
if(temp == NULL)
break;
}
if(temp != NULL)
{
/* Links the address part of new node */
newNode->next = temp->next;
Step 2: If the last node is the head node then make the head node as NULL else disconnect
the second last node with the last node i.e. secondLastNode->next = NULL
Step 2: Reconnect n-1th node with the n+1th node i.e. prevNode->next = toDelete->next
(Where prevNode is n-1th node and toDelete node is the nth node and toDelete->next is the n+1th
node).
Step 3: Free the memory occupied by the nth node i.e. toDelete node.
if(toDelete == NULL)
break;
}
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
• The pointer from the last element in the list points back to the first
element. head
A B C
• The only difference is that there is no any NULL value terminating the list.
• In fact in the list every node points to the next node and last node points to the
first node, thus forming a circle. Since it forms a circle with no end to stop it is
called as circular linked list.
• In circular linked list there can be no starting or ending node, whole node can
be traversed from any node.
• In order to traverse the circular linked list, only once we need to traverse
entire list until the starting node is not traversed again.
• A circular linked list can be implemented using both singly linked list and
DR. OM KUMAR C.U. 20/01/2025 56
accessing of elements.
OPERATIONS ON CIRCULAR LINKED LIST
• Creation of list
• Traversal of list
• Insertion of node
• At the beginning of list
• At any position in the list
• Deletion of node
• Deletion of first node
• Deletion of node from middle of the list
• Deletion of last node
• Counting total number of nodes
DR. OM KUMAR C.U. 20/01/2025 59
• Reversing of list
DR. OM KUMAR C.U. 20/01/2025 60
CREATION AND TRAVERSAL OF A CIRCULAR LIST
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
}*head;
int main()
{
int n, data;
head = NULL;
head->data = data;
head->next = NULL;
prevNode = head;
for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode; //Links the previous node with newly created node
prevNode = newNode; //Moves the previous node ahead
DR. OM KUMAR C.U. 20/01/2025 62
}
prevNode->next = head; //Links the last node with first node
printf("\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n"); }}
CIRCULAR LINKED LIST: TRAVERSAL OF LIST
void displayList()
{
struct node *current;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
printf("DATA IN THE LIST:\n");
do {
printf("Data %d = %d\n", n, current->data);
current = current->next;
n++;
DR. OM KUMAR C.U.
}while(current != head); 20/01/2025 63
}
}
Insertion
A node can be added in three ways:
•Insertion in an empty list struct Node *addToEmpty(struct Node *last,
•Insertion at the end of the list int data)
•Insertion in between the nodes { // This function is only for empty list
if (last != NULL)
return last;
Insertion in an empty List // Creating a node dynamically.
Initially when the list is empty, last pointer will be struct Node *last =
(struct Node*)malloc(sizeof(struct
NULL.
Node));
// Assigning the data.
•Insertion at the beginning of the list last -> data = data;
// Note : list was empty. We link single
node
// to itself.
last -> next = last;
return last; }
After insertion,
Insertion at the end of the list
struct node
{
int data;
struct node *next; // Pointer to
next node in DLL
struct node *prev; // Pointer to
previous node in DLL
DR. OM KUMAR C.U. 20/01/2025 70
};
DOUBLE LINKED LIST
• Doubly linked list is a collection of nodes linked together in a sequential way.
• Doubly linked list is almost similar to singly linked list except it contains two address or
reference fields, where one of the address field contains reference of the next node and other
contains reference of the previous node.
• First and last node of a linked list contains a terminator generally a NULL value, that determines
the start and end of the list.
• Doubly linked list is sometimes also referred as bi-directional linked list since it allows traversal
of nodes in both direction.
• Since doubly linked list allows the traversal of nodes in both direction, we can keep track of both
first and last nodes.
Step 1: Traverse to N-1 node in the list, where N is the position to insert. Say
temp now points to N-1th node.
Step 3: Connect the next address field of newNode with the node
pointed by next address field of temp node.
Step 5: Check if temp.next is not NULL then, connect the previous address
field of node pointed by temp.next to newNode.
int main()
{
int n, data;
head = NULL;
last = NULL;
displayList();
return 0;}
DOUBLY LINKED LIST: INSERTION AT ANY POSITION
void createList(int n)
{
int i, data;
struct node *newNode;
if(n >= 1){ /* Creates and links the head node */
head = (struct node *)malloc(sizeof(struct node));
printf("Enter data of 1 node: ");
scanf("%d", &data);
head->data = data;
head->prev = NULL;
head->next = NULL;
last = head;
for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last; //Links new node with the previous node
newNode->next = NULL;
newNode->data = data;
newNode->next = temp->next; //Connects new node with n+1th node
newNode->prev = temp; //Connects new node with n-1th node
if(temp->next != NULL)
{
temp->next->prev = newNode; /* Connects n+1th node with new node */
}
temp->next = newNode; /* Connects n-1th node with new node */
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
DR. OM KUMAR C.U. else{ 20/01/2025 79
void displayList()
{
struct node * temp;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;
• Two Linked Lists are identical when they have same data and
arrangement of data is also same.
int main()
{
struct node *a, *b;
a = createList(5); // e.g: a: 5->4->3->2->1
b = createList(5); // e.g: b: 5->4->3->2->1
areIdentical(a, b)? printf("Identical"): printf("Not identical");
return
DR. OM KUMAR C.U. 0; 20/01/2025 83
}
FEW EXERCISES TO TRY OUT
• Compare two given list with same data but different arrangement.
e.g: a: 5->4->3->2->1
b: 1->2->3->4->5
• Count the number of nodes in the given list using iterative method and
recursive method.
DR. OM KUMAR C.U. 20/01/2025 84
ORDERING LINKED LIST
Step 1: Create two more pointers other than head namely prevNode and curNode that
will hold the reference of previous node and current node respectively.
• Make sure that prevNode points to first node i.e. prevNode = head.
• head should now point to its next node i.e. head = head->next.
• curNode should also points to the second node i.e. curNode = head.
DR. OM KUMAR C.U. 20/01/2025 86
REVERSING A LIST
Step 2: Now, disconnect the first node from others. We will make sure
that it points to none. As this node is going to be our last node.
Perform operation prevNode->next = NULL.
Step 3: Move the head node to its next node i.e. head = head->next.
Step 5: Point the previous node to current node and current node to head
node. Means they should now point to prevNode = curNode; and
curNode = head.
Step 7: Now, after all nodes has been re-connected in the reverse order. Make the
last node as the first node. Means the head pointer should point to prevNode
pointer.
• Perform head = prevNode; And finally you end up with a reversed linked
list of its original.
while(head != NULL)
{
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
int main()
{
int n = 10;
createList(n); // creates 10 nodes in the linked list
Recursive_Reverse(head);
return 0;
}
DR. OM KUMAR C.U. 20/01/2025 91
THANK YOU