Professional Documents
Culture Documents
Dept. of ECE 1
Data Structures (Module 1) – Single Linked List
Topics Covered
09/04/2024 Dr.NSS/ECE 2
Data Structures – Arrays and Linked List
Linked List
A linked list is a sequential structure that consists of a sequence of items in linear order
which are linked to each other.
Hence, you have to access data sequentially and random access is not possible.
09/04/2024 Dr.NSS/ECE 3
Data Structures – Arrays and Linked List
Linked List
09/04/2024 Dr.NSS/ECE 4
Data Structures – Arrays and Linked List
Linked List
Structure of a Node Operations on Linked List
09/04/2024 Dr.NSS/ECE 5
Data Structures – Arrays and Linked List
A singly linked list is a unidirectional linked list. So, you can only traverse it in one direction,
i.e., from head node to tail node.
A singly linked list can be used to store a list of tasks that need to be completed, with the
head node representing the first task to be completed and the tail node representing the
last task to be completed.
09/04/2024 Dr.NSS/ECE 6
Data Structures (Module 1) – Singly Linked List
09/04/2024 Dr.NSS/ECE 7
Implementation of Single Linked List –
Insert node at the beginning
Dept. of ECE 8
Data Structures (Module 1) – Singly Linked List
09/04/2024 Dr.NSS/ECE 9
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 10
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 11
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 12
Implementation of Single Linked List –
Insert node at the end
Dept. of ECE 13
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 14
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 16
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 17
Implementation of Single Linked List –
Insert node at the specific position
Dept. of ECE 18
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 19
Data Structures (Module 1) – Single Linked List
Implementation of Single Linked List – Insert node at the specific position
// Function to insert a new node at the specific position of the linked list
void insertAtPos(struct Node** head1, int pos, int d)
{
struct Node* newNodeptr1 = createNode(d);
if (pos == 1)
{
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
}
else
{
struct Node *temp = *head1;
for (int i = 1; i < pos - 1 ; i++)
{
temp = temp ->next;
}
newNodeptr1->next = temp ->next;
temp ->next = newNodeptr1;
}
printf("Node with data %d inserted at pos: %d\n",
09/04/2024 d, pos);
Dr.NSS/ECE 20
}
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 21
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 22
Implementation of Single Linked List –
Delete node at the beginning
Dept. of ECE 23
Data Structures (Module 1) – Single Linked List
return 0;
09/04/2024 }
Dr.NSS/ECE 24
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 25
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 26
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 27
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 28
Implementation of Single Linked List –
Delete node at the end
Dept. of ECE 29
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 30
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 31
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 32
Data Structures (Module 1) – Single Linked List
Implementation of Single Linked List – Delete node at the end
// Function to delete a node from the end of the linked list
void deleteFromEnd(struct Node** head1) free(temp->next);
{ temp->next = NULL;
if (*head1== NULL)
{ printf("Node deleted from the end.\n");
printf("List is empty. Cannot delete.\n"); }
return;
}
if ((*head1)->next == NULL)
{
free(*head1);
*head1= NULL;
printf("Node deleted from the end.\n");
return;
}
printf("\n");
}
09/04/2024 Dr.NSS/ECE 34
Implementation of Single Linked List –
Delete node at specific position
Dept. of ECE 35
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 36
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 37
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 38
Data Structures (Module 1) – Single Linked List
Implementation of Single
// Function to delete a node Linked
at specificList – Delete
position node
in the linked list at specific position
void deleteAtPos(struct Node ** head1, int pos)
{
if (*head1 == NULL)
{
printf("List is empty. Deletion not possible.\n");
return;
}
struct Node *temp = *head1;
if (pos == 1) // If the position is the head node
{
*head1 = temp->next; // Change head
free(temp); // Free old head
return;
}
for (int i = 1; i < pos - 1; i++) // Find the previous node of the node to be deleted
{
temp = temp->next;
}
struct Node *delete = temp->next; //Node to be deleted
temp->next = temp->next->next; // Connecting linked list
free(delete); // Free memory
09/04/2024 Dr.NSS/ECE 39
}
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 40
Implementation of Single Linked List –
Find position of the node
Dept. of ECE 41
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 43
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 44
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 45
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 46
Implementation of Single Linked List –
Count number of nodes
Dept. of ECE 47
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 48
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 49
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 50
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 51
Data Structures (Module 1) – Single Linked List
09/04/2024 Dr.NSS/ECE 52
Implementation of Single Linked List –
Reversing the linked list
Dept. of ECE 53
Data Structures (Module 1) – Single Linked List
newNodeptr1->next = *head1;
*head1 = newNodeptr1;
09/04/2024 Dr.NSS/ECE 55
Data Structures (Module 1) – Single Linked List
newNodeptr->data = d;
newNodeptr->next = NULL;
return newNodeptr;
}
09/04/2024 Dr.NSS/ECE 56
Data Structures (Module 1) – Single Linked List
printf("\n");
}
09/04/2024 Dr.NSS/ECE 57
Data Structures (Module 1) – Single Linked List
Example 1:
Input: n = 4
arr[] = {1,2,3,4}
x=3
Output:
2
Explanation: There is one test case with array as {1, 2, 3 4} and element to be searched as 3. Since 3 is present at index 2,
output is 2.
Example 2:
Input: n = 5
arr[] = {1,2,3,4,5}
x=5
Output: 4
Explanation: For array elements {1,2,3,4,5} element to be searched is 5 and it is at index 4. So, the output is 4.
09/04/2024 Dr.NSS/ECE 61
Data Structures (Module 1) – Single Linked List
Day 1 Assignment: Task 2
Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.
Example 1:
Input:
N=5
arr[]= {0 2 1 2 0}
Output: 0 0 1 2 2
Explanation: 0s 1s and 2s are segregated into ascending order.
Example 2:
Input:
N=3
arr[] = {0 1 0}
Output: 0 0 1
Explanation: 0s 1s and 2s are segregated into ascending order.
09/04/2024 Dr.NSS/ECE 62
Data Structures (Module 1) – Single Linked List Day 1 Assignment: Task 2
Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void sort012(int a[], int n)
{
int low = 0, mid = 0, high = n - 1;
while (mid <= high)
{
switch (a[mid])
{
case 0:
swap(&a[low++], &a[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(&a[mid], &a[high--]);
09/04/2024 Dr.NSS/ECE 63
break; } } }
Dept. of ECE 64
Data Structures (Module 1) – Single Linked List
Implementation of Single Linked List – Insert node at the specific position
void insertAtPos(struct Node** temp, int pos, int data) // Function to insert a new node at the specific position of the linked list
{
struct Node* newNode = createNode(data);
if (pos == 1)
{
newNode->next = *temp;
*temp = newNode;
}
else
{
for (int i = 1; i < pos - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
} 09/04/2024 Dr.NSS/ECE 65
}
Data Structures (Module 1) – Single Linked List
Implementation of Single Linked List – Delete node at the specific position
void deleteAtPos(struct Node **temp, int pos) // Function to delete a node at specific position in the linked list
{
if (*temp == NULL)
{
printf("List is empty. Deletion not possible.\n");
return;
}
struct Node *position = *temp;
if (pos == 1) // If the position is the head node
{
*temp = position->next; // Change head
free(position); // Free old head
return;
}
for (int i = 1; position != NULL && i < pos - 1; i++) // Find the previous node of the node to be deleted
position = position->next;
if (position == NULL || position->next == NULL) // If position is more than number of nodes
{
printf("Position is out of range. Deletion not possible.\n");
return;
} 09/04/2024 Dr.NSS/ECE 66
Data Structures (Module 1) – Single Linked List
Implementation of Single Linked List – Delete node at the specific position
// Function to delete a node at specific position in the linked list
09/04/2024 Dr.NSS/ECE 67