Linked List Singly LL
Linked List Singly LL
Linked List
Subject Coordinator
Dr. Sonal Chandel
Department of Computer Science & Engineering
E-Mail ID: [email protected]
Introduction
A linked list is a linear data structure.
It consists of nodes where each node contains data and a reference (link) to the next node in the sequence.
This allows for dynamic memory allocation and efficient insertion and deletion operations compared to
arrays.
•The list is not required to be contiguously present in the memory. The node can reside any where in the
memory and linked together to make a list. This achieves optimized utilization of space.
•list size is limited to the memory size and doesn't need to be declared in advance.
•Empty node can not be present in the linked list.
•We can store values of primitive types or objects in the singly linked list.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Why use linked list over array?
Array contains following limitations:
• The size of array must be known in advance before using it in the
program.
• Increasing size of the array is a time taking process. It is almost impossible to
expand the size of the array at run time.
• All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.
• Sizing is no longer a problem since we do not need to define its size at the time of declaration.
List grows as per the program's demand and limited to the available memory space.
Types of Linked List
There are three common types of Linked List.
1.Singly Linked List
2.Doubly Linked List
3.Circular Linked List
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Linked List Implementation in C
12 300 17 400 24 0
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Insertion at the beginning
struct node
500
500 {
int data;
struct node *next;
500 10 200
};
New node struct node *head, *newnode,*temp;
500
newnode =(structnode*)malloc(sizeof(struct node))
printf(“enter data”);
scanf(“%d”, &newnode ->data);
newnode->next=head;
head=newnode;
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Insertion at the end
400
300
200
temp 500
struct node
{
int data; 500 10 0
struct node *next;
New node
}; 500
struct node *head, *newnode,*temp;
newnode =(structnode*)malloc(sizeof(struct node)) while temp->next!=0
printf(“enter data”);
{
scanf(“%d”, &newnode ->data);
temp=temp->next;
newnode->next=null; }
temp=head; temp->next=newnode;
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Insertion after a given
300
position
i=2
200
temp 500
Pos=2 i=1
500 5 400
int Pos, i=1; new node 500
struct node
{ if Pos>count
newnode->next=temp->next;
int data; {
printf(“invalid Position”); temp->next=newnode;
struct node *next;
}; }
struct node *head, *newnode,*temp; else
{ temp=head;
newnode =(structnode*)malloc(sizeof(struct node))
printf(“enter data”); while(i<Pos)
scanf(“%d”, &newnode ->data); {
printf(“enter position”); temp=temp->next;
scanf(“%d”, &Pos); i++;}
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Deletion in Linked
List
The Deletion of a node from a singly linked list can be performed at different positions. Based on
the position of the node being deleted, the operation is categorized into the following categories.
2. Deletion at the end Removing the node from end of the list.
3. Deletion after specified node It involves deleting the node after the
specified node in the list.
Deletion from beginning
200 free
temp
struct node
300 {
int data;
struct node *next;
};
struct node *head, *temp;
temp=head;
head=head->next;
free(temp);
Deletion
temp
from end
300 300 400
200
0
200
ptr
struct node
{ if (head==temp)
int data; {
struct node *next; head=0;}
}; else
struct node *head, *temp, *ptr; {
temp=head; ptr->next=0;
while( temp->next!=0) }
{ free(temp);
ptr=temp;
temp=temp->next;
}
Deletion from specified Position
200 i=1
temp