150 likes | 247 Views
Data Structure . Ali Abdul Karem Habib MSc.IT. Doubly Linked List . A doubly linked list is a collection of data elements, called nodes, where each node N is divided into 3 parts. INFO field which contains the data NEXT field which contains the location of the next node
E N D
Data Structure Ali Abdul KaremHabib MSc.IT
Doubly Linked List • A doubly linked list is a collection of data elements, called nodes, where each node N is divided into 3 parts. • INFO field which contains the data • NEXT field which contains the location of the next node • PREV field which contains the location of previous node in the list. • The pointer PTR[PREV] of the first node and PTR[NEXT] of the last node is NULL • In DLL can traverse from front and back and this reduce time to insert and delete . Data Next Prev
Operation On Doubly Linked List • 1- Create DLL • 2- Insertion • At First • At Last • At Any Position • 3- Deletion • At First • At Last • At Any Position 4- Traversing :- • From Start • From Last 5- Searching
Create DLL Node Using C++ • structDnode • { • Data type Value ; • Dnode * next , *prev ; • }
Algorithm Of Create First NODE • 1- Start • 2- If Start=Null then • 3- Prepare DNode • 4- Read data • 5- DNode ->data= data • 6- DNode ->next=Null7-Dnode->prev=Null • 8- Start = DNode • 9- Cur = DNode • 10- End .
Algorithm Insertion At First • Start • 2- Prepare DNode • 3- Read Data • 4- DNode ->Data=Data • 5- DNode->next=Start • 6- Dnode->Prev=Null • 7- start->prev=node • 8- Start=DNode • 9-End.
Insertion At Last • 1- Start • 2- 1- Start • 2- Prepare DNode • 3- Read Data • 4- DNode ->Data=Data • 5- Dnode->Prev=Cur • 6- Cur->next=DNode • 7- Cur=node • 8- Cur ->next=Null • 9- End
Insert At Any Position • 1- Start • 2- Prepare DNode • 3- Read Data • 4- Read Pos • 5- DNode -> Data=Data • 6- temp =start • 7- Let Cn=1 • 8- while( Cn < pos-1) • 9- temp=temp-> next • 10- Cn=Cn+1 • 11- End While • 12- DNode -> next=temp ->next • 13- temp ->next->Prev=DNode • 14- temp->Next=Dnode • 15- Node->Prev=temp • 14- End
Delete First • 1-Start • 2- temp=Start • 3- Start= Start -> next • 4- Start->Prev=Null • 5- Delete ( temp ) • 6- temp=Null • 7- End .
Delete At Last • 1- Start • 2- Let Temp=Curr • 3- Cur=Cur->Prev • 4- Cur->Next=Null • 5- Delete(temp) • 6- Temp=Null • 7- End
Deletion From Any Position 1- Start 2- Let Temp=start 3- Read Pos 4- Cn=1 5- while ( Cn< pos) 6- temp=temp -> next 7- Cn=Cn+1 8- End While 9- P= temp 10- temp->next->Prev =temp->Prev 11-temp->Prev->Next=temp->Next 11- Delete (p) 12- p=null 13- end
Traversing From Start • 1- Start • 2- temp=start • 3- While ( temp <> null ) • 4- print (temp -> Data ) • 5- temp = temp -> next • 6- End while • 7- End .
Travers From Last • 1- Start • 2- temp=Cur • 3- While ( temp <> null ) • 4- print (temp -> Data ) • 5- temp = temp -> Prev • 6- End while • 7- End .
Searching 1- Start 2- let temp=start 3- Read key 4- While ( temp <> null) 5- If ( temp -> Data = key ) then 6- print ( “ Found “) 7- else Print ( “Not Found “ ) 8- temp = temp -> next ) 9- End While 10 - End
Assignment • Implement Queue Using DLL