SlideShare a Scribd company logo
1 of 24
LINKED LIST
Presented By
MANOJKUMAR
N I
LINKED LIST
 Linked list is linear data structure which is collection of
zero or more nodes where each node has two fields.
root
1000 2000 3000
Node
10 2000 20 3000
30
NULL
Information Link
1000
REPRESENTATION OF SINGLY
LINKED LIST IN C
 A structure is a collection of one or more fields which
are of same or different types.
 The nodes in a linked list are self referential structures.
 Self referential structure is a structure which has at least
one field which is pointer to same structure
Ex:
struct node
{
int data;
struct node *link;
};
struct node *root=NULL;
TYPES OF LINKED LIST
1.Singly linked list.
2.Doubly linked list.
3.Circular singly linked lists.
4.Circular doubly linked lists.
SINGLY LINKED LIST
It is a collection of zero or more nodes where each
node has two or more fields and only one link field
which contains address of the next node.
NULL
root
5412 10 NULL
5412
1000 20 NULL
1000
8120 40 NULL
2748
30 NULL
8120
2748
Pictorial representation of singly linked list
Operations performed on singly linked
list are :
1.Inserting a node into the list
2.Deleting node from the list
3.Searching a key in a list
1.INSERTING A NODE INTO THE LIST
A. Inserting a node at rear end.
void insert rear-end()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“ Enter Node Datan”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp; 1000 2000 3000
}
else
{
struct node *p; root
p=root;
while(p->link!=NULL) p
4000
{
p=p->link;
}
p->link=temp;
temp
}
10 2000 20 3000
40 NULL
30 NULL
1000
4000
1000
B. Inserting a node at front end.
void insert rear-end()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“ Enter Node Datan”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(root==NULL)
{ 1000 2000 3000
root=temp;
}
else root
{ 500
temp->link=root;
root=temp;
} temp
}
10 2000 20 3000 30 NULL
1000
500
5 NULL
C. FINDING THE LENGTH OF THE LIST
1000 2000 3000 4000
int length()
{
int count=0; temp
struct node *temp;
temp=root;
while (temp!=NULL)
{
count++;
temp=temp->link;
}
return count;
}
NULL
root
1000 10 NULL
2000 20 NULL
3000 40 NULL
30 NULL
4000
1000
D. Inserting a node at specific position after a node
void after-add ()
{
struct node *temp, *p;
int loc, i=1,len;
printf(“Enter Location”);
scanf(“%d”, &loc); 1000 2000 3000 4000
len=length();
if (loc>len)
{
printf(“Invalid location”); root
printf(“Currently list is having a %d nodes”,len); 4500
} p
else
{
p=root;
while(i<loc) temp
{
p=p->link;
i++;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->link=p->link;
p->link=temp;
}
}
10 2000 20 3000 30 4000
40
NULL
35
NULL
1000
1000
4500
2. DELETING A NODE IN THE LIST
A. Deleting a node in middle or end of a node
void delete()
{
struct node *temp;
int loc;
printf(“enter a location to delete”);
scanf(“%d”,&loc);
if(loc>length())
{
printf(“Invalid location”);
} 1000 2000 3000 4000
else
{
if(loc==1)
{
temp=root;
root=temp->link; root
temp->link=NULL; P q
free (temp);
}
else
{
struct node *p=root,*q; temp
int i=1;
while(i<loc-1)
{
p=p->link;
i=i+1;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);
}
}
}
10 2000 10 3000 10 4000
40
NULL
1000
1000 3000
1000
3. SEARCHING A NODE IN THE LIST
Void search()
{
struct node *temp;
temp=root;
if(temp==NULL)
{ 1000 2000 3000 4000
printf(“list is empty”);
}
else
{ root
while(temp!=NULL) temp
{
if (key==temp->info)
break; key
temp=temp->link;
}
if(temp==NULL) Not a key
{
printf(“ search is unsuccessful”);
return;
}
printf(“ search is successful”);
}
}
10 2000 40 NULL
30 4000
20 3000
1000
1000
10
50
4. DISPLAYING ALL ELEMENTS IN THE LIST
1000 2000 3000 4000
void display()
{
struct node *temp;
temp=root;
if(temp==NULL)
{ temp
printf(“list is empty”);
}
else
{
while(temp!=NULL)
{
printf(“%d”, temp->data);
temp=temp->link;
}
}
}
NULL
root
1000 10 NULL
2000 20 NULL
3000 40 NULL
30 NULL
4000
1000
DISADVANTAGES OF SINGLY LINKED LIST
 Using singly linked list, it is not possible to traverse
the list backwards
i.e two way traversing is not possible
 Insertion or deletion to the left of a designated node
X is difficult, this requires the finding the predecessor
of X which takes more time.
 Given a Node X , It is difficult to find the
predecessor of X . Since it is required to traverse the
list from the first node
DOUBLY LINKED LIST
 It is one of the most powerful variations of linked
list.
 It is a linear collection of nodes where each node is
divided into three fields.
 Using such list , it is possible to traverse the list in
forward and backward direction. Such list is known
as Two way list
*left data *right
REPRESENTATION OF DOUBLY LINKED LIST IN C
 The nodes in a linked list are self referential
structures.
 A structure is a collection of one or more fields which
are of same or different types. self referential
structure is a structure which has at least one field
which is pointer to same structure
Ex:
struct node{
int data;
struct node *left;
struct node *right;
};
 struct node *root=NULL;

root
Pictorial representation of doubly linked list
 Operations performed on doubly linked linked list are :
1.Inserting a node into the list
2.Deleting node from the list
3.Search in a list
4.Display the contents of list
NULL 10
2000
1000 20 3000
3000 40
NULL
2000 30 4000
1000
1000 2000 3000 4000
1.INSERTING A NODE INTO THE LIST
A. Inserting the node at rear end
void append()
{
struct node *temp;
temp=(struct node *)malloc (size of(struct node));
printf(“enter node data);
scanf(“%d”,&temp->data);
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
} 1000 2000 3000 4000
else
{
struct node *p; root
p=root;
while(p->right!=NULL) p
{ temp
p=p->link;
}
p->right=temp;
temp->left=p;
}
NULL 10
2000
1000 20 3000
NULL 40
NULL
2000 30
NULL
1000
1000
4000
B. Inserting the node at beginning end
void addbeg()
{
struct node *temp;
temp=(struct node *)malloc (size of(struct node));
printf(“enter node data);
scanf(“%d”,&temp->data);
temp->NULL;
temp->NULL;
if(root==NULL)
{
root=temp; 1000 2000 3000
}
else
{ root
temp->right=root;
root->left=temp; 500
root=temp;
}
}
temp
NULL 10
2000
1000
1000 10 3000 2000 30 NULL
NULL 40
NULL
500
FINDING THE LENGTH OF THE LIST
1000 2000 3000 4000
void length()
{
struct node * temp;
int count=0; temp
while(temp!=NULL)
{
count++;
temp=temp->right;
}
return count;
}
NULL
root
1000 10 NULL
2000 20 NULL
3000 40 NULL
30 NULL
4000
1000
INSERTING A NODE AT SPECIFIC POSITION AFTER A NODE
void addafter()
{
strcut node *temp;
int loc,len,i=1:
printf(“enter loc to add:”);
scanf(“%d”,&loc); 1000 2000 3000 4000
len=length();
if(loc>len)
{ root
printf(“Invalid location:”);
printf(“List contains only %d nodes”,len); p 5000
}
else
{
temp=(struct node * temp) malloc(size of(struct node)); temp
printf(“enter node data:”);
scanf(“%d”,&temp->data);
temp->left=NULL;
temp->right=NULL;
p=root;
NULL 10
2000
3000 40
NULL
2000 30 4000
1000 20 3000
NULL 10
2000
1000
1000
5000
while(i<loc)
{
p=p->right;
i++;
}
temp->right=p->right;
p->right->left=temp;
temp->left=p;
p->right=temp;
}
}
4. Displaying all elements in the list
void display()
{
struct node *temp=root;
if(temp==NULL)
{
printf(‘list is emptyn”);
}
else
while(temp!=null)
{
printf(“%d”,temp-.data);
temp=temp->right;
}
}
Singly linked list doubly linked list
1. Since there is only one link , so
traversing is done only in one direction
1. Since there are two links, so traversing
can be done in both directions
2. While deleting a node, its predecessor is
required and can be found only after
traversing from the beginning of list.
2.While deleting a node X,it predecessor
can be obtained using *left of node X,No
need to traverse the list.
3.Occupy less memory 3.Occupy more memory
4.Prgrams will be lengthy and need more
time to design
4.Using circular linked list with header,
efficient and small programs can be written
and hence design is easier
5.Care is taken to modify only one link of a
node
5.Care is taken to modify both links of a
node
SINGLY LINKED LIST VS DOUBLY LINKED LIST
ADVANTAGES OF LINKED LIST
 Linked list are a dynamic data structure since it
allocating and deallocating the memory while the
programing is running.
 Insertion and deletion node operations are easily
implemented in a linked list.
 Dynamic data structures such as stacks and queues can
be implemented using linked list.
LINKED LIST.pptx

More Related Content

Similar to LINKED LIST.pptx

Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 
dokumen.tips_linked-list-ppt-5584a44be6115.pptx
dokumen.tips_linked-list-ppt-5584a44be6115.pptxdokumen.tips_linked-list-ppt-5584a44be6115.pptx
dokumen.tips_linked-list-ppt-5584a44be6115.pptx
MrMudassir
 

Similar to LINKED LIST.pptx (20)

Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.ppt
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Linked list
Linked listLinked list
Linked list
 
dokumen.tips_linked-list-ppt-5584a44be6115.pptx
dokumen.tips_linked-list-ppt-5584a44be6115.pptxdokumen.tips_linked-list-ppt-5584a44be6115.pptx
dokumen.tips_linked-list-ppt-5584a44be6115.pptx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
List
ListList
List
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSLINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 

LINKED LIST.pptx

  • 2. LINKED LIST  Linked list is linear data structure which is collection of zero or more nodes where each node has two fields. root 1000 2000 3000 Node 10 2000 20 3000 30 NULL Information Link 1000
  • 3. REPRESENTATION OF SINGLY LINKED LIST IN C  A structure is a collection of one or more fields which are of same or different types.  The nodes in a linked list are self referential structures.  Self referential structure is a structure which has at least one field which is pointer to same structure Ex: struct node { int data; struct node *link; }; struct node *root=NULL;
  • 4. TYPES OF LINKED LIST 1.Singly linked list. 2.Doubly linked list. 3.Circular singly linked lists. 4.Circular doubly linked lists.
  • 5. SINGLY LINKED LIST It is a collection of zero or more nodes where each node has two or more fields and only one link field which contains address of the next node. NULL root 5412 10 NULL 5412 1000 20 NULL 1000 8120 40 NULL 2748 30 NULL 8120 2748 Pictorial representation of singly linked list Operations performed on singly linked list are : 1.Inserting a node into the list 2.Deleting node from the list 3.Searching a key in a list
  • 6. 1.INSERTING A NODE INTO THE LIST A. Inserting a node at rear end. void insert rear-end() { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); printf(“ Enter Node Datan”); scanf(“%d”,&temp->data); temp->link=NULL; if(root==NULL) { root=temp; 1000 2000 3000 } else { struct node *p; root p=root; while(p->link!=NULL) p 4000 { p=p->link; } p->link=temp; temp } 10 2000 20 3000 40 NULL 30 NULL 1000 4000 1000
  • 7. B. Inserting a node at front end. void insert rear-end() { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); printf(“ Enter Node Datan”); scanf(“%d”,&temp->data); temp->link=NULL; if(root==NULL) { 1000 2000 3000 root=temp; } else root { 500 temp->link=root; root=temp; } temp } 10 2000 20 3000 30 NULL 1000 500 5 NULL
  • 8. C. FINDING THE LENGTH OF THE LIST 1000 2000 3000 4000 int length() { int count=0; temp struct node *temp; temp=root; while (temp!=NULL) { count++; temp=temp->link; } return count; } NULL root 1000 10 NULL 2000 20 NULL 3000 40 NULL 30 NULL 4000 1000
  • 9. D. Inserting a node at specific position after a node void after-add () { struct node *temp, *p; int loc, i=1,len; printf(“Enter Location”); scanf(“%d”, &loc); 1000 2000 3000 4000 len=length(); if (loc>len) { printf(“Invalid location”); root printf(“Currently list is having a %d nodes”,len); 4500 } p else { p=root; while(i<loc) temp { p=p->link; i++; } temp=(struct node*)malloc(sizeof(struct node)); temp->link=p->link; p->link=temp; } } 10 2000 20 3000 30 4000 40 NULL 35 NULL 1000 1000 4500
  • 10. 2. DELETING A NODE IN THE LIST A. Deleting a node in middle or end of a node void delete() { struct node *temp; int loc; printf(“enter a location to delete”); scanf(“%d”,&loc); if(loc>length()) { printf(“Invalid location”); } 1000 2000 3000 4000 else { if(loc==1) { temp=root; root=temp->link; root temp->link=NULL; P q free (temp); } else { struct node *p=root,*q; temp int i=1; while(i<loc-1) { p=p->link; i=i+1; } q=p->link; p->link=q->link; q->link=NULL; free(q); } } } 10 2000 10 3000 10 4000 40 NULL 1000 1000 3000 1000
  • 11. 3. SEARCHING A NODE IN THE LIST Void search() { struct node *temp; temp=root; if(temp==NULL) { 1000 2000 3000 4000 printf(“list is empty”); } else { root while(temp!=NULL) temp { if (key==temp->info) break; key temp=temp->link; } if(temp==NULL) Not a key { printf(“ search is unsuccessful”); return; } printf(“ search is successful”); } } 10 2000 40 NULL 30 4000 20 3000 1000 1000 10 50
  • 12. 4. DISPLAYING ALL ELEMENTS IN THE LIST 1000 2000 3000 4000 void display() { struct node *temp; temp=root; if(temp==NULL) { temp printf(“list is empty”); } else { while(temp!=NULL) { printf(“%d”, temp->data); temp=temp->link; } } } NULL root 1000 10 NULL 2000 20 NULL 3000 40 NULL 30 NULL 4000 1000
  • 13. DISADVANTAGES OF SINGLY LINKED LIST  Using singly linked list, it is not possible to traverse the list backwards i.e two way traversing is not possible  Insertion or deletion to the left of a designated node X is difficult, this requires the finding the predecessor of X which takes more time.  Given a Node X , It is difficult to find the predecessor of X . Since it is required to traverse the list from the first node
  • 14. DOUBLY LINKED LIST  It is one of the most powerful variations of linked list.  It is a linear collection of nodes where each node is divided into three fields.  Using such list , it is possible to traverse the list in forward and backward direction. Such list is known as Two way list *left data *right
  • 15. REPRESENTATION OF DOUBLY LINKED LIST IN C  The nodes in a linked list are self referential structures.  A structure is a collection of one or more fields which are of same or different types. self referential structure is a structure which has at least one field which is pointer to same structure Ex: struct node{ int data; struct node *left; struct node *right; };  struct node *root=NULL;
  • 16.  root Pictorial representation of doubly linked list  Operations performed on doubly linked linked list are : 1.Inserting a node into the list 2.Deleting node from the list 3.Search in a list 4.Display the contents of list NULL 10 2000 1000 20 3000 3000 40 NULL 2000 30 4000 1000 1000 2000 3000 4000
  • 17. 1.INSERTING A NODE INTO THE LIST A. Inserting the node at rear end void append() { struct node *temp; temp=(struct node *)malloc (size of(struct node)); printf(“enter node data); scanf(“%d”,&temp->data); temp->left=NULL; temp->right=NULL; if(root==NULL) { root=temp; } 1000 2000 3000 4000 else { struct node *p; root p=root; while(p->right!=NULL) p { temp p=p->link; } p->right=temp; temp->left=p; } NULL 10 2000 1000 20 3000 NULL 40 NULL 2000 30 NULL 1000 1000 4000
  • 18. B. Inserting the node at beginning end void addbeg() { struct node *temp; temp=(struct node *)malloc (size of(struct node)); printf(“enter node data); scanf(“%d”,&temp->data); temp->NULL; temp->NULL; if(root==NULL) { root=temp; 1000 2000 3000 } else { root temp->right=root; root->left=temp; 500 root=temp; } } temp NULL 10 2000 1000 1000 10 3000 2000 30 NULL NULL 40 NULL 500
  • 19. FINDING THE LENGTH OF THE LIST 1000 2000 3000 4000 void length() { struct node * temp; int count=0; temp while(temp!=NULL) { count++; temp=temp->right; } return count; } NULL root 1000 10 NULL 2000 20 NULL 3000 40 NULL 30 NULL 4000 1000
  • 20. INSERTING A NODE AT SPECIFIC POSITION AFTER A NODE void addafter() { strcut node *temp; int loc,len,i=1: printf(“enter loc to add:”); scanf(“%d”,&loc); 1000 2000 3000 4000 len=length(); if(loc>len) { root printf(“Invalid location:”); printf(“List contains only %d nodes”,len); p 5000 } else { temp=(struct node * temp) malloc(size of(struct node)); temp printf(“enter node data:”); scanf(“%d”,&temp->data); temp->left=NULL; temp->right=NULL; p=root; NULL 10 2000 3000 40 NULL 2000 30 4000 1000 20 3000 NULL 10 2000 1000 1000 5000
  • 21. while(i<loc) { p=p->right; i++; } temp->right=p->right; p->right->left=temp; temp->left=p; p->right=temp; } } 4. Displaying all elements in the list void display() { struct node *temp=root; if(temp==NULL) { printf(‘list is emptyn”); } else while(temp!=null) { printf(“%d”,temp-.data); temp=temp->right; } }
  • 22. Singly linked list doubly linked list 1. Since there is only one link , so traversing is done only in one direction 1. Since there are two links, so traversing can be done in both directions 2. While deleting a node, its predecessor is required and can be found only after traversing from the beginning of list. 2.While deleting a node X,it predecessor can be obtained using *left of node X,No need to traverse the list. 3.Occupy less memory 3.Occupy more memory 4.Prgrams will be lengthy and need more time to design 4.Using circular linked list with header, efficient and small programs can be written and hence design is easier 5.Care is taken to modify only one link of a node 5.Care is taken to modify both links of a node SINGLY LINKED LIST VS DOUBLY LINKED LIST
  • 23. ADVANTAGES OF LINKED LIST  Linked list are a dynamic data structure since it allocating and deallocating the memory while the programing is running.  Insertion and deletion node operations are easily implemented in a linked list.  Dynamic data structures such as stacks and queues can be implemented using linked list.