DSA Module 3
DSA Module 3
23EC29 23EC28
Data Structure & Algorithm
Module 3: LinkedList
➢ Linkedlist
• Linked List is Series of Nodes.
• Each node Consist of two Parts viz Data Part & Pointer Part.
• Pointer Part stores the address of the next node.
➢ What is linked list? State its advantage, Explain its types [3M DEC-18]
✓ Advantages of linked list
• Linked List is Dynamic data Structure.
• Linked List can grow and shrink during run time.
• Insertion and Deletion Operations are Easier.
• Efficient Memory Utilization, i.e no need to pre-allocate memory Faster Access
time, can be expanded in constant time without memory overhead.
• Linear Data Structures such as Stack, Queue can be easily implemeted using
Linked list
Linked list is a self referential structure in "c", each node has variable to store data
and next field store the address of next field.
➢ Types of linkedlist
1. Singly Linked List
• In this type of Linked List two successive nodes are linked together in linear
fashion
• Each Node contain address of the next node to be followed.
• In Singly Linked List only Linear or Forward Sequential movement is
possible.
2. Doubly linked list
• In this type of linked list each node holds two pointer field.
• In doubly linked list address of next as well as preceding element are linked
with current node.
Computer Science:
1. Dynamic memory allocation for efficient memory usage.
2. Implementation of data structures like stacks and queues.
3. Collision resolution in hash tables using chaining.
4. Adjacency list representation for graphs in algorithms.
5. Sparse matrix representation to save memory.
6. Undo functionality in text editors and applications.
7. Storing polynomials for arithmetic operations.
8. Managing free blocks in file systems.
Real World:
1. Music playlists for sequential or shuffled playback.
2. Navigation in image viewers with forward and backward options.
3. Dynamic job scheduling in operating systems.
4. Social media feeds updating dynamically in real-time.
5. Navigation in browser tabs for efficient management.
6. Real-time data handling in video games and simulations.
7. Maintaining train routes with dynamic stops.
8. Linked storage of items in inventory systems.
Applications of Circular Linked List
1. Round-robin scheduling for managing CPU processes.
2. Token passing in networks like token ring topology.
3. Implementing circular buffers for streaming data.
4. Cyclic navigation of music or video playlists.
5. Simulation of real-time traffic control systems.
6. Multiplayer games for cyclic player turns.
7. Recurring task scheduling in event systems.
8. Periodic log file rotations in servers.
struct node {
int data ;
struct node *link;
};
temp2->link=head;
head=temp2;
return head;
}
int main(){
head->link=current;
head=insert_begin(head,data);
current=head;
while(current!=NULL)
{
printf("%d",current->data);
current=current->link;
}
return 0;
}
Insert At Between
#include<stdio.h>
#include<stdlib.h>
struct node {
int data ;
struct node *link;
};
pos--;
while(pos!=1){
ptr=ptr->link;
pos--;
}
temp2->link=ptr->link;
ptr->link=temp2;
}
int main(){
insert_between(head,data,position);
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
return 0;
}
Insert At the End
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
temp1=malloc(sizeof(struct node));
temp1->data=data;
temp1->link=NULL;
while(temp->link!=NULL){
temp=temp->link;
}
temp->link=temp1;
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
insert_at_end(head,45);
printf("\n");
print2(head);
return 0;
}
Delete At The Beginning
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
if(head==NULL)
printf("Empty linked list");
else{
struct node *temp;
temp=head;
head= head->link;
free(temp);
temp=NULL;
}
return head;
}
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
struct node *head1;
head1=delete_begin(head);
printf("\n");
print2(head1);
return 0;
}
Delete Between
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
if(head==NULL)
printf("Empty linked list");
else if (pos==1){
head=temp->link;
free(temp);
temp=NULL;
}
else{
while(pos!=1){
temp1=temp; /*here it is already is in this condition*/
temp=temp->link;
pos--;
}
temp1->link=temp->link;
free(temp);
temp=NULL;
}
}
if(head==NULL)
printf("Linked list is empty");
struct node *temp = NULL;
temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
int position= 2;
struct node *head1;
head1= delete_between(head,position);
printf("\n");
print2(head1);
return 0;
}
Delete At the End
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
if(head==NULL)
printf("Empty linked list");
else if (head->link==NULL){
free(head);
head=NULL;
}
else{
struct node *temp=head;
struct node *temp1=head;
while(temp->link!=NULL){
temp1=temp;
temp=temp->link;
}
temp1->link=NULL;
free(temp);
temp=NULL;
}
return head;
}
if(head==NULL)
printf("Linked list is empty");
struct node *temp = NULL;
temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
struct node *head1;
head1=delete_last(head);
printf("\n");
print2(head1);
return 0;
}
Delete The list
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
while(temp!=NULL){
temp=temp->link;
free(head);
head=temp;
}
return head;
}
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
printf("\n");
print2(head1);
return 0;
}
PrintSinglyList
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
return 0;
}
PrintSinglylist by Function
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
return 0;
}
Reverse List
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
while(head != NULL){
next=head->link;
head->link = prev;
prev = head;
head = next;
}
head=prev;
return head;
}
if(head==NULL)
printf("Linked list is empty");
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->link;
}
}
int main()
{
struct node *head = NULL;
head=malloc(sizeof(struct node));
head->data=66;
head->link=NULL;
print(head);
printf("\n");
print2(head1);
return 0;