Data Structure and Algorithm: Sorted Link List Presented by Prabal Kumar Sahu Asst. Prof., IT Dept., BCREC
Data Structure and Algorithm: Sorted Link List Presented by Prabal Kumar Sahu Asst. Prof., IT Dept., BCREC
Data Structure and Algorithm: Sorted Link List Presented by Prabal Kumar Sahu Asst. Prof., IT Dept., BCREC
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
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
50 1000 55 2000
2010 60 2010 75 NULL
Address : 4000 Address : 1000 Address : 2000 Address : 2010
THANK YOU