Data Structure and Algorithm: Sorted Link List Presented by Prabal Kumar Sahu Asst. Prof., IT Dept., BCREC

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

DATA STRUCTURE AND

ALGORITHM
SORTED LINK LIST
Presented by
Prabal Kumar Sahu
Asst. Prof., IT Dept., BCREC
SORTED LINKED LIST
• STRUCTURE OF SINGLY LINKED LIST

struct SLL
{
int iData;
struct SLL *Next;
};
SORTED LINKED LIST
• TO CREATE SINGLY LINKED LIST NODE
struct SLL *Start=NULL, *New;
fnCreate()
{
New=(struct SLL*)malloc(sizeof(struct SLL));
printf(“\nEnter the data:”);
scanf(“%d”,&New->iData);
New->Next=NULL; New
}
60 NULL

Address : 2000
SORTED LINKED LIST
• TO INSERT THE NEW NODE
struct SLL *Start=NULL, *New;
fnInsert()
{
fnCreate();
if(Start==NULL)
{
Start=New;
}
else
{ New
Start
fnAddNode();
} 60 NULL
}
Address : 2000
SORTED LINKED LIST
• TO INSERT THE NEW NODE
fnAddNode()
{
// insert node at the beginning
if(New->iData < Start->iData)
{
New->Next=Start; New
Start=New;
return; 50 NULL
2000
}
Address : 4000
Start

60 NULL
Address : 2000
SORTED LINKED LIST
• TO INSERT THE NEW NODE
fnAddNode() Cont…
struct SLL *Prev, *Ptr;
// insert node in between two nodes
for(Prev=Start, Ptr=Start ->Next; Ptr; Prev=Ptr, Ptr=Ptr->Next)
{
if(New->iData < Ptr->iData)
{
Prev->Next=New; New
New->Next=Ptr;
return;
55 NULL
2000
}
} Start Address : 1000

Prev Ptr
50
50 2000
1000 60 NULL
Address : 4000 Address : 2000
SORTED LINKED LIST
• TO INSERT THE NEW NODE
fnAddNode() Cont…
// insert node at the end
if(Ptr==NULL)
{ New
Prev->Next=New;
} 75 NULL

} //end of fnAddNode function Address : 2010


Prev
Start Ptr

50 1000 55 2000 60 NULL


2010

Address : 4000 Address : 1000 Address : 2000


SORTED LINKED LIST
• TO TRAVERSE LINKED LIST
fnTraverse()
{
if(Start==NULL)
{
printf(“Link List is Empty”);
return;
}
for(Ptr=Start; Ptr; Ptr=Ptr->Next)
{
printf(“%d\t”,Ptr->iData)
}
}
Start Ptr Ptr Ptr Ptr Ptr

50 1000 55 2000 60 2010 75 NULL


Address : 4000 Address : 1000 Address : 2000 Address : 2010
OUTPUT : 50 55 60 75
SORTED LINKED LIST
• TO DELETE NODE FROM LINKED LIST
fnDelete()
{
struct SLL *Prev, *Ptr, *Temp;
int iDel;
printf(“\nEnter the data to be deleted”);
scanf(“%d”,&iDel);
// Delete First Node
50
if(Start->iData==iDel)
{ iDel
Temp=Start;
Start=Start->Next;
free(Temp);
return;
}
Start Temp Start

50 1000 55 2000 60 2010 75 NULL


Address : 4000 Address : 1000 Address : 2000 Address : 2010
SORTED LINKED LIST
• TO DELETE NODE FROM LINKED LIST
fnDelete() Cont…
//Delete from Middle or Last
for(Prev=Start, Ptr=Start->Next; Ptr; Prev=Ptr,Ptr=Ptr->Next)
{
if(Ptr->iData==iDel) 60
{
Prev-Next=Ptr->Next; iDel
free(Ptr);
break;
}
}
Start Prev PtrPrev Ptr

50 1000 55 2000
2010 60 2010 75 NULL
Address : 4000 Address : 1000 Address : 2000 Address : 2010
SORTED LINKED LIST
• TO DELETE NODE FROM LINKED LIST
fnDelete() Cont…
//Data Not Found
if(Ptr==NULL)
{
88
printf(“\nData Not Found…”);
} iDel
} // end of fnDelete function

Start Prev PtrPrev Ptr Prev Prev


Ptr Ptr

50 1000 55 2000
2010 60 2010 75 NULL
Address : 4000 Address : 1000 Address : 2000 Address : 2010
THANK YOU

You might also like