SlideShare une entreprise Scribd logo
1  sur  50
UNIT IV
Linked list
• Each node in a linked list contains one or more members
that represent data.
• In addition to the data, each node contains a pointer,
which can point to another node.
Example
Data / Info link / next
Advantages of Linked list over arrays:
• Efficient memory utilization
• Operations such as insertion and deletion are easy and
efficient.
• Size can be changed.
• Arbitrary memory locations
Disadvantages of linked lists:
• Each node requires an extra pointer in addition to
information, requiring more space.
• Insertion or deletion of a node takes a bit longer
because it involves more pointer operations.
• Linked lists do not allow random access.
• Traversing and changing of pointers consumes a lot of
time.
• Programming is typically trickier with pointers.
Memory Representation of Linear Linked List:
It needs two linear arrays for memory representation.
Let these linear arrays are INFO and LINK.
INFO[K] contains the information part and LINK[K]
contains the next pointer field of node K.
A variable START is used to store the location of the
beginning of the LIST
NULL is used as next pointer sentinel which indicates
the end of LIST.
TYPES OF LINKED LIST
Singly-linked list
• The simplest kind of linked list is a singly-linked
list which has one link per node. This link points to the
next node in the list, or to a null value or empty list if it
is the last node.
Doubly-linked list
• A more sophisticated kind of linked list is a doubly-
linked list. Each node has two links, one to the previous
node and one to the next node.
Doubly-linked list
• A more sophisticated kind of linked list is a doubly-
linked list. Each node has two links, one to the previous
node and one to the next node.
Info − Each link of a linked list can store a data
called an element.
Next − Each link of a linked list contains a link to
the next link called Next.
Prev − Each link of a linked list contains a link to
the previous link called Prev.
Advantages
1) It can be traversed in forward or backward
direction.
2) Given a particular nodes address, one can have
both successor nodes and predecessor nodes
address that makes inserting and deleting much
easier
3) It is used to represent the trees effectively.
Disadvantage
Extra memory is required to store the back pointer.
Circularly-linked list
• In a circularly-linked list, the first and last nodes
are linked together. This can be done for both
singly and doubly linked lists. To traverse a
circular linked list, you begin at any node and
follow the list in either direction until you return to
the original node. Viewed another way, circularly-
linked lists can be seen as having no beginning or
end.
Singly –circularly linked list
• A singly linked circular list is a linked list where the last
node in the list points to the first node in the list. A
circular list does not contain NULL pointers
Doubly Linked List
In this type of liked list each node holds two-
pointer field. Pointers exist between adjacent
nodes in both directions. The list can be traversed
either forward or backward.
Insertion at the Beginning of the list:
• Single linked List
• Operations on Singly linked list:
– Traversing a linked list
– Creation of linked list
– Displaying elements of linked list
– counting number of nodes
– searching an element
– insertion of node
– deletion of node
void create()
{ char choice='y';
q=null; while(choice=='y')
{ p=((struct node*)malloc(sizeof(struct node)));
printf("nEnter the rollno & name :");
scanf("%d %s",&p->rollno,p->name);
p->prev=null;
p->next=null;
if(start==null) { start=p; q=p; }
else
{ q->next=p; p->prev=q; p->next=null; q=p; }
printf("nDo you want to create another node y/n :");
choice=getche();
} }
void display()
{
printf("n The nodes in the list arenSTART->");
p=start;
while(p!=null)
{
printf("%d %s ->",p->rollno,p->name);
p=p->next;
}
printf("NULL");
return;
}
void addatbeg()
{
p=((struct node*)malloc(sizeof(struct node)));
printf("nEnter register number:");
scanf("%d",&p->rollno);
printf("nEnter name of student:");
scanf("%s",&p->name);
(*start)->prev=p;
p->next=start;
p->prev=null;
start=p;
}
void addafter(int regno)
{ p=((struct node*)malloc(sizeof(struct node)));
printf("nEnter the rollno & name :");
scanf("%d %s",&p->rollno,p->name);
t=start;
while((t->next!=null)||(t->rollno==regno))
{ if(t->rollno==regno) break;
else t=t->next; }
if(t->next==null)
{ p->next=null; p->prev=t; t->next=p; }
else
{ p->prev=t; p->next=t->next; t->next=p;
}
}
void del(int regno)
{ p=start; prev=null;
if(start==null) {
printf("nLinked list is emptyn"); return; }
if(start->rollno==regno) {
start =start->link; free(p); return; }
while((p->rollno!=regno)&&(p!=null))
{ prev=p; p=p->link; }
if(p==null)
{ printf("n Reg number %d is not found in the LLn", regno); }
else { prev->link=p->link;
free(p);
}
}
void search(int regno)
{
int i=0; p=start;
while(p!=null)
{ if(regno==p->rollno)
{ i++;
printf("nRoll no %d is found at %d",p->rollno,i);
printf("n Name:%s",p->name);
return; }
else
{ p=p->link; i++; }
}
printf("nNode with register number %d does not exit",regno);
}
Stack Implementation using linked list:
/*Program to implement a Stack using Linked List */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define NULL 0
void push(int);
int pop();
void display();
struct node
{
int info;
struct node *link;
}*top,*p;
void main()
{
int ch,i,item,pos;
clrscr();
while(1)
{ printf("n1->Pushn");
printf("2->Popn");
printf("3->Displayn");
printf("4->Exitn");
printf("Enter your choicen");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter an elementn");
scanf("%d",&item);
push(item);
break;
case 2:item=pop();
if(item==-1)
printf("Empty Stackn");
else
printf("Popped element=%dn",item); break;
case 3:display();
break;
case 4:exit(0);
} } }
void push(int item)
{
p=(struct node *)malloc(sizeof(struct node));
p->info=item;
p->link=NULL;
if(top==NULL)
top=p;
else
{ p->link=top;
top=p; } }
int pop()
{
int delinfo;
p=top;
if(p==NULL)
return(-1);
top=top->link;
delinfo=p->info;
free(p);
return(delinfo);
}
void display()
{
p=top;
if(p==NULL)
printf("empty Stackn");
else
printf("nStackn");
while(p!=NULL) {
printf("%dt",p->info);
p=p->link;
} }
Circular Linked list
Circular Linked List is a variation of Linked list in which the
first element points to the last element and the last element
points to the first element. Both Singly Linked List and
Doubly Linked List can be made into a circular linked list.
Circular Linked list
void insert(int item)
{
p=(struct node *)malloc(sizeof(struct node));
p->info=item;
p->link=NULL;
if(front==NULL)
front=p;
else
rear->link=p;
rear=p;
rear->link=front;
}
int delet()
{
int delinfo;
if(front==NULL)
return(-1);
else
{
if(front==rear)
{
delinfo=front->info;
free(front);
front=NULL;
rear=NULL;
}
else
{
p=front;
delinfo=p->info;
front=front->link;
rear->link=front;
free(p);
}
return(delinfo);
} }
void display()
{
p=front;
if(p==NULL)
printf("empty Listn");
else
printf("Circular Linked Listn");
while(p!=rear)
{
printf("%dt",p->info);
p=p->link;
}
printf("%dn",p->info);
}
Stack Implementation using linked list:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define NULL 0
void push(int);
int pop();
void display();
struct node
{
int info;
struct node *link;
}*top,*p;
void main()
{
int ch,i,item,pos;
clrscr();
while(1)
{
printf("n1->Pushn");
printf("2->Popn");
printf("3->Displayn");
printf("4->Exitn");
printf("Enter your choicen");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter an elementn");
scanf("%d",&item);
push(item);
case 2:item=pop();
if(item==-1)
printf("Empty Stackn");
else
printf("Popped element=%dn",item);
break;
case 3:display();
break;
case 4:exit(0);
}
}
}
void push(int item)
{
p=(struct node *)malloc(sizeof(struct node));
p->info=item;
p->link=NULL;
if(top==NULL)
top=p;
else
{
p->link=top;
top=p;
}
}
int pop()
{
int delinfo;
p=top;
if(p==NULL)
return(-1);
top=top->link;
delinfo=p->info;
free(p);
return(delinfo);
}
void display()
{
p=top;
if(p==NULL)
printf("empty Stackn");
else
printf("nStackn");
while(p!=NULL)
{
printf("%dt",p->info);
p=p->link;
}
}

Contenu connexe

Similaire à Linked List.pptx

circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
MeghaKulkarni27
 

Similaire à Linked List.pptx (20)

Linked list
Linked listLinked list
Linked list
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptxData Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptx
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Linked List
Linked ListLinked List
Linked List
 
Linked list
Linked listLinked list
Linked list
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Introduction to linked lists
Introduction to linked listsIntroduction to linked lists
Introduction to linked lists
 
Linked list
Linked listLinked list
Linked list
 
Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 

Plus de SherinRappai

A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA students
SherinRappai
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
SherinRappai
 

Plus de SherinRappai (20)

Extensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet TechnologyExtensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet Technology
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technology
 
Building Competency and Career in the Open Source World
Building Competency and Career in the Open Source WorldBuilding Competency and Career in the Open Source World
Building Competency and Career in the Open Source World
 
How to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptxHow to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptx
 
Issues in Knowledge representation for students
Issues in Knowledge representation for studentsIssues in Knowledge representation for students
Issues in Knowledge representation for students
 
A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA students
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
 
Rendering Algorithms.pptx
Rendering Algorithms.pptxRendering Algorithms.pptx
Rendering Algorithms.pptx
 
Introduction to Multimedia.pptx
Introduction to Multimedia.pptxIntroduction to Multimedia.pptx
Introduction to Multimedia.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
system model.pptx
system model.pptxsystem model.pptx
system model.pptx
 
SE UNIT-1.pptx
SE UNIT-1.pptxSE UNIT-1.pptx
SE UNIT-1.pptx
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Introduction to DBMS.pptx
Introduction to DBMS.pptxIntroduction to DBMS.pptx
Introduction to DBMS.pptx
 
Input_Output_Organization.pptx
Input_Output_Organization.pptxInput_Output_Organization.pptx
Input_Output_Organization.pptx
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Linked List.pptx