You are on page 1of 67

Data Structures (Module 1) – 1.

2 Singly Linked List


Presented by
Prof.A.Ramesh, &
Dr.N.Shanmugasundaram, HoD/ECE.

Dept. of ECE 1
Data Structures (Module 1) – Single Linked List

Topics Covered

Module 1 – Linked List


Arrays vs Linked List - Types of Linked List : Singly Linked list - Doubly Linked List - Singly Circular
Linked list - Doubly Circular Linked List – Operations on Linked List : Insertion-Deletion- Find-Reverse
– Modifying Linked List - Floyds cycle finding algorithm (Slow pointer and Fast pointer)

Text & Reference Books:


1. Seymour Lipschutz,” Data Structures using C”, First Edition, McGraw Hill Education, 2017.
2. Narasimha Karumanchi “Data Structures and Algorithms Made Easy” Fifth Edition, Career Monk Publications,2017.
3. Salaria R S, “Data Structures and Algorithms using C”, Fifth Edition, Khanna Book Publishing, New Delhi, 2012
4. Karumanchi Narasimha, ”Data Structures and Algorithms Made Easy”, Fifth Edition, Career Monk Publication, 2016
5. Reema Thareja, “Data Structures Using C”, Second Edition, Oxford University Press, 2019.

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.

 Linked lists provide a simple and flexible representation of dynamic sets.

09/04/2024 Dr.NSS/ECE 3
Data Structures – Arrays and Linked List

Linked List

Structure of a Singly Linked List

 Elements in a linked list are known as nodes.


 Each node contains a Data and a pointer to its successor node, known as next.
 The attribute named head points to the first element of the linked list.
 The last element of the linked list is known as the tail.

09/04/2024 Dr.NSS/ECE 4
Data Structures – Arrays and Linked List

Linked List
Structure of a Node Operations on Linked List

#include<stdio.h> 1. void begin_insert();


#include<stdlib.h> 2. void last_insert();
struct node 3. void random_insert();
{ 4. void begin_delete();
int data;
struct node *next; 5. void last_delete();
}; 6. void random_delete();
7. void display();
8. void search();
struct node *head;

09/04/2024 Dr.NSS/ECE 5
Data Structures – Arrays and Linked List

Singly 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

Implementation of Single Linked List


Creating
pointers
int main() for two
#include <stdio.h>
{ Nodes
#include <stdlib.h> Allocating
struct node *a, *b; memory
struct node a = malloc(sizeof(struct node)); for
{ b = malloc(sizeof(struct node)); Nodes
int data;
struct node *next; a->data = 10;
}; b->data = 20;
Assigning
a->next = b; Nodes
Declaring
b->next = NULL;
a Node
printf("%d\n%d\n",a->data, a->next->data);
return 0;
}

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

Implementation of Single Linked List – Insert node at the beginning


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtBeginning(&head, 3);
{ insertAtBeginning(&head, 2);
int data;
struct Node* next; display(head);
};
insertAtBeginning(&head, 1);
// Functions used in the linked list
void insertAtBeginning(struct Node** head1, int d); display(head);
struct Node* createNode(int d);
return 0;
void display(struct Node* temp); }

09/04/2024 Dr.NSS/ECE 9
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the beginning

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 10
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the beginning

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 11
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the beginning

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Insert node at the end


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtEnd(&head, 3);
{ insertAtEnd(&head, 2);
int data;
struct Node* next; display(head);
};
insertAtEnd(&head, 1);
// Functions used in the linked list
void insertAtEnd(struct Node** head1, int d); display(head);
struct Node* createNode(int d);
return 0;
void display(struct Node* temp); }

09/04/2024 Dr.NSS/ECE 14
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the end


// Function to insert a new node at the end of the linked list
void insertAtEnd(struct Node** head1, int d)
{
struct Node* newNodeptr1 = createNode(d);
if (*head1 == NULL)
{
*head1 = newNodeptr1;
}
else
{
struct Node* temp = *head1;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNodeptr1;
}
printf("Node with data %d inserted at the end.\n", d);
} 09/04/2024 Dr.NSS/ECE 15
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the end

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 16
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the end

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Insert node at the specific position


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtPos(&head, 1, 10);
{ insertAtPos(&head, 2, 20);
int data; display(head);
struct Node* next;
}; insertAtPos(&head, 2, 15);
// Functions used in the linked list display(head);
void insertAtPos(struct Node** head1, int pos, int d);
struct Node* createNode(int d); return 0;
}
void display(struct Node* temp);

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

Implementation of Single Linked List – Insert node at the specific position

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 21
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Insert node at the specific position

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Delete node at the beginning


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtBeginning(&head, 3);
{ insertAtBeginning(&head, 2);
int data; display(head);
struct Node* next;
}; deleteFromBeginning(&head);
display(head);
// Functions used in the linked list
void insertAtBeginning(struct Node** head 1, int d); insertAtBeginning(&head, 1);
struct Node* createNode(int d); display(head);
void deleteFromBeginning(struct Node** head1);
void display(struct Node* temp); deleteFromBeginning(&head);
display(head);

return 0;
09/04/2024 }
Dr.NSS/ECE 24
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the beginning

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 25
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the beginning

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 26
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the beginning

// Function to delete a node from the beginning of the linked list


void deleteFromBeginning(struct Node** head1)
{
if (*head1 == NULL)
{
printf("List is empty. Cannot delete.\n");
return;
}
struct Node* temp= *head1;
*head1 = temp->next;
free(temp);
printf("Node deleted from the beginning.\n");
}

09/04/2024 Dr.NSS/ECE 27
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the beginning

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Delete node at the end


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtBeginning(&head, 3);
{ insertAtBeginning(&head, 2);
int data; deleteFromEnd(&head);
struct Node* next;
}; display(head);
// Functions used in the linked list insertAtBeginning(&head, 1);
void insertAtBeginning(struct Node** head1, int d); deleteFromEnd(&head);
struct Node* createNode(int d);
void deleteFromEnd(struct Node** head1); display(head);
void display(struct Node* temp);
return 0;
}

09/04/2024 Dr.NSS/ECE 30
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the end

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 31
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the end

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

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;
}

struct Node* temp= *head1;


while (temp>next->next != NULL)
{
temp= temp->next;
09/04/2024 Dr.NSS/ECE 33
}
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at the end

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Delete node at specific position


#include <stdio.h> int main()
#include <stdlib.h> {
struct Node* head = NULL;
// Structure to represent a node in the linked list
struct Node insertAtBeginning(&head, 3);
{ insertAtBeginning(&head, 2);
int data; deleteAtPos(&head, 2);
struct Node* next;
}; display(head);
// Functions used in the linked list insertAtBeginning(&head, 1);
void insertAtBeginning(struct Node** head1, int d); deleteAtPos(&head, 1);
struct Node* createNode(int d);
void deleteAtPos(struct Node** head1, int pos); display(head);
void display(struct Node* temp);
return 0;
}

09/04/2024 Dr.NSS/ECE 36
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at specific position

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 37
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Delete node at specific position

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

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

Implementation of Single Linked List – Delete node at specific position

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Find position of the node


int main()
#include <stdio.h>
{
#include <stdlib.h>
struct Node* head = NULL;
insertAtBeginning(&head, 3);
// Structure to represent a node in the linked list
insertAtBeginning(&head, 2);
struct Node
insertAtBeginning(&head, 1);
{
display(head);
int data;
struct Node* next;
int pos = Find(head, 2);
};
if (pos == -1)
{
// Functions used in the linked list
printf("Value 2 not found in the list\n");
void insertAtBeginning(struct Node** head1, int d);
}
struct Node* createNode(int d);
else
int Find(struct Node *temp, int value);
{
void display(struct Node* temp);
printf("Value 2 found at position %d\n", pos);
}
return 0;
}
09/04/2024 Dr.NSS/ECE 42
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Find position of the node

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 43
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Find position of the node

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 44
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Find position of the node

// Function to find a node's position in the linked list

int Find(struct Node *temp, int value)


{
int pos = 1;
while (temp != NULL)
{
if (temp->data == value)
{
return pos;
}
temp = temp->next;
pos++;
}
return -1; // Return -1 if value not found
}

09/04/2024 Dr.NSS/ECE 45
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Find position of the node

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

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

Implementation of Single Linked List – Count number of nodes


int main()
#include <stdio.h> {
#include <stdlib.h> struct Node* head = NULL;

// Structure to represent a node in the linked list insertAtBeginning(&head, 3);


struct Node insertAtBeginning(&head, 2); //&head represents address of
{ head pointer
int data; display(head);
struct Node* next; int c = count_nodes(head); //head represents address of
}; head node
printf("No. of nodes in the linked list: %d\n", c);
// Functions used in the linked list insertAtBeginning(&head, 1);
void insertAtBeginning(struct Node** head1, int d); display(head);
struct Node* createNode(int d); c = count_nodes(head);
int count_nodes(struct Node *temp); printf("No. of nodes in the linked list: %d", c);
void display(struct Node* temp);
return 0;
}

09/04/2024 Dr.NSS/ECE 48
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Count number of nodes

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 49
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Count number of nodes

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 50
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Count number of nodes

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

printf("\n");
}

09/04/2024 Dr.NSS/ECE 51
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Count number of nodes

// Function to count number of nodes in linked list

int count_nodes(struct Node *temp)


{
int count = 0;
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}

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

Implementation of Single Linked List – Reversing the linked list


int main()
#include <stdio.h> {
#include <stdlib.h> struct Node* head = NULL;
// Structure to represent a node in the linked list // Inserting some elements at the beginning of the linked list
struct Node insertAtBeginning(&head, 5);
{ insertAtBeginning(&head, 4);
int data; insertAtBeginning(&head, 3);
struct Node* next; insertAtBeginning(&head, 2);
}; insertAtBeginning(&head, 1);
// Functions used in the linked list printf("Original linked list: ");
void insertAtBeginning(struct Node** head1, int d); display(head);
struct Node* createNode(int d);
void reverse(struct Node ** head1); reverse(&head); // Reversing the linked list
void display(struct Node* temp);
printf("Reversed linked list: ");
display(head);
return 0;
09/04/2024 } Dr.NSS/ECE 54
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Reversing the linked list

// Function to insert a new node at the beginning of the linked list

void insertAtBeginning(struct Node** head1, int d)


{
struct Node* newNodeptr1 = createNode(d);

newNodeptr1->next = *head1;
*head1 = newNodeptr1;

printf("Node with data %d inserted at the beginning.\n", d);


}

09/04/2024 Dr.NSS/ECE 55
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Reversing the linked list

// Function to create a new node with given data

struct Node* createNode(int d)


{
struct Node* newNodeptr = (struct Node*)malloc(sizeof(struct Node));
if (newNodeptr == NULL)
{
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}

newNodeptr->data = d;
newNodeptr->next = NULL;

return newNodeptr;
}

09/04/2024 Dr.NSS/ECE 56
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Reversing the linked list

// Function to display/traverse and print the content in linked list

void display(struct Node* temp)


{
printf(“\nLinked List: ");

while (temp != NULL)


{
printf("%d ", temp->data);
temp = temp->next;
}

printf("\n");
}

09/04/2024 Dr.NSS/ECE 57
Data Structures (Module 1) – Single Linked List

Implementation of Single Linked List – Reversing the linked list

// Function to reverse a linked list


void reverse(struct Node ** head1)
{
struct Node *before= NULL;
struct Node *now = *head1;
struct Node *after = NULL;

while (now != NULL)


{
after = now->next; // Store next node
now->next = before; // Reverse current node's pointer
before = now; // Move pointers one position ahead
now = after;
}

*head1 = before; // Set the new head after reversing


}
09/04/2024 Dr.NSS/ECE 58
Session Handler Details
Prof.A.Ramesh,
AP/ECE
and
Dr.N.Shanmugasundaram,
Professor & Head – ECE,
Sri Eshwar College Of Engg.,
09/04/2024 Dr.NSS/ECE 59
Data Structures (Module 1) – Single Linked List
Day 1 Assignment: Task 1
Given an integer array and another integer element. The task is to find if the given element is present in array or not.

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.

int search(int arr[], int N, int X)


{ }
09/04/2024 Dr.NSS/ECE 60
Data Structures (Module 1) – Single Linked List
Day 1 Assignment: Task 1
Given an integer array and another integer element. The task is to find if the given element is present in array or not.

int search(int arr[], int N, int X)


{
for (int i = 0; i < N; i++)
{
if (arr[i] == X)
{
return i; // Element found at index i
}
}
return -1; // Element not found
}

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.

void sort012(int a[], int n)


{ }

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

// Node temp->next is the node to be deleted


struct Node *nextNode = position->next->next;

// Unlink the node from linked list


free(position->next); // Free memory

position->next = nextNode; // Unlink the deleted node from list


}

09/04/2024 Dr.NSS/ECE 67

You might also like