SlideShare a Scribd company logo
1 of 21
Doubly Linked Lists
CS 308 – Data Structures
Node data
• info: the user's data
• next, back: the address of the next and
previous node in the list
.back .next
.info
Node data (cont.)
template<class ItemType>
struct NodeType {
ItemType info;
NodeType<ItemType>* next;
NodeType<ItemType>* back;
};
Finding a List Item
• We no longer need to use prevLocation (we can
get the predecessor of a node using its back
member)
Finding a List Item (cont.)
Inserting into a Doubly Linked List
1. newNode->back = location->back; 3. location->back->next=newNode;
2. newNode->next = location 4. location->back = newNode;
FindItem(listData, item, location, found)
• RetrieveItem, InsertItem, and DeleteItem all
require a search !
• Write a general non-member function FindItem
that takes item as a parameter and returns location
and found.
• InsertItem and DeleteItem need location (ignore
found)
• RetrieveItem needs found (ignores location)
Finding a List Item (cont.)
template<class ItemType>
void FindItem(NodeType<ItemType>* listData, ItemType item,
NodeType<ItemType>* &location, bool &found)
{
// precondition: list is not empty
bool moreToSearch = true;
location = listData;
found = false;
while( moreToSearch && !found) {
if(item < location->info)
moreToSearch = false;
else if(item == location->info)
found = true;
else {
if(location->next == NULL)
moreToSearch = false;
else
location = location->next;
}
}
}
How can we distinguish between the
following two cases?
Special case: inserting in the beginning
Inserting into a Doubly Linked List
template<class ItemType>
void SortedType<ItemType>::InsertItem(ItemType item)
{
NodeType<ItemType>* newNode;
NodeType<ItemType>* location;
bool found;
newNode = new NodeType<ItemType>;
newNode->info = item;
if (listData != NULL) {
FindItem(listData, item, location, found);
if (location->info > item) {
newNode->back = location->back;
newNode->next = location;
if (location != listData) // special case
(location->back)->next = newNode;
else
listData = newNode;
location->back = newNode;
}
(1)
(2)
(3)
(4)
(3)
Inserting into a Doubly Linked List
(cont.)
else { // insert at the end
newNode->back = location;
location->next = newNode;
newNode->next = NULL;
}
}
else { // insert into an empty list
listData = newNode;
newNode->next = NULL;
newNode->back = NULL;
}
length++;
}
Deleting from a Doubly Linked List
• Be careful about the end cases!!
Headers and Trailers
• Special cases arise when we are dealing
with the first or last nodes
• How can we simplify the implementation?
– Idea: make sure that we never insert or delete
the ends of the list
– How? Set up dummy nodes with values outside
of the range of possible values
Headers and Trailers (cont.)
• Header Node: contains a value smaller than
any possible list element
• Trailer Node: contains a value larger than
any possible list element
A linked list as
an array of records
• What are the advantages of using linked
lists?
(1) Dynamic memory allocation
(2) Efficient insertion-deletion (for sorted lists)
• Can we implement a linked list without
dynamic memory allocation ?
A linked list
as an array
of records
(cont.)
Case Study: Implementing a
large integer ADT
• The range of integer values varies from one
computer to another
• For long integers, the range is
[-2,147,483,648 to 2,147,483,647]
• How can we manipulate larger integers?
Case Study: Implementing a large
integer ADT (cont.)
- A special list ADT
Exercises
• 1, 6, 8, 10, 12

More Related Content

Similar to LinkedDoublyLists.ppt

Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdfSonaPathak5
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 

Similar to LinkedDoublyLists.ppt (20)

Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Linked list
Linked list Linked list
Linked list
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
linkedLists.ppt
linkedLists.pptlinkedLists.ppt
linkedLists.ppt
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 

More from veenatanmaipatlolla (11)

Introduction To Vector Integrals.pptx
Introduction To Vector Integrals.pptxIntroduction To Vector Integrals.pptx
Introduction To Vector Integrals.pptx
 
227r1a1237.pptx
227r1a1237.pptx227r1a1237.pptx
227r1a1237.pptx
 
DS THEORY 35.pptx
DS THEORY 35.pptxDS THEORY 35.pptx
DS THEORY 35.pptx
 
Mathematics PPT.pptx
Mathematics PPT.pptxMathematics PPT.pptx
Mathematics PPT.pptx
 
EC-PPT.pptx
EC-PPT.pptxEC-PPT.pptx
EC-PPT.pptx
 
G.Harshith%20Ec%20Lab.pptx
G.Harshith%20Ec%20Lab.pptxG.Harshith%20Ec%20Lab.pptx
G.Harshith%20Ec%20Lab.pptx
 
200210mstrs_adler.pdf
200210mstrs_adler.pdf200210mstrs_adler.pdf
200210mstrs_adler.pdf
 
Lecture 2 Engineering curves.pdf
Lecture 2 Engineering curves.pdfLecture 2 Engineering curves.pdf
Lecture 2 Engineering curves.pdf
 
13_fuel_and_combustion_1.ppt
13_fuel_and_combustion_1.ppt13_fuel_and_combustion_1.ppt
13_fuel_and_combustion_1.ppt
 
Matrix_PPT.pptx
Matrix_PPT.pptxMatrix_PPT.pptx
Matrix_PPT.pptx
 
KIRCHOFF’S LAW.pptx
KIRCHOFF’S LAW.pptxKIRCHOFF’S LAW.pptx
KIRCHOFF’S LAW.pptx
 

Recently uploaded

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxpritamlangde
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptxrouholahahmadi9876
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 

Recently uploaded (20)

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 

LinkedDoublyLists.ppt

  • 1. Doubly Linked Lists CS 308 – Data Structures
  • 2. Node data • info: the user's data • next, back: the address of the next and previous node in the list .back .next .info
  • 3. Node data (cont.) template<class ItemType> struct NodeType { ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };
  • 4. Finding a List Item • We no longer need to use prevLocation (we can get the predecessor of a node using its back member)
  • 5. Finding a List Item (cont.)
  • 6. Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;
  • 7. FindItem(listData, item, location, found) • RetrieveItem, InsertItem, and DeleteItem all require a search ! • Write a general non-member function FindItem that takes item as a parameter and returns location and found. • InsertItem and DeleteItem need location (ignore found) • RetrieveItem needs found (ignores location)
  • 8.
  • 9. Finding a List Item (cont.) template<class ItemType> void FindItem(NodeType<ItemType>* listData, ItemType item, NodeType<ItemType>* &location, bool &found) { // precondition: list is not empty bool moreToSearch = true; location = listData; found = false; while( moreToSearch && !found) { if(item < location->info) moreToSearch = false; else if(item == location->info) found = true; else { if(location->next == NULL) moreToSearch = false; else location = location->next; } } }
  • 10. How can we distinguish between the following two cases?
  • 11. Special case: inserting in the beginning
  • 12. Inserting into a Doubly Linked List template<class ItemType> void SortedType<ItemType>::InsertItem(ItemType item) { NodeType<ItemType>* newNode; NodeType<ItemType>* location; bool found; newNode = new NodeType<ItemType>; newNode->info = item; if (listData != NULL) { FindItem(listData, item, location, found); if (location->info > item) { newNode->back = location->back; newNode->next = location; if (location != listData) // special case (location->back)->next = newNode; else listData = newNode; location->back = newNode; } (1) (2) (3) (4) (3)
  • 13. Inserting into a Doubly Linked List (cont.) else { // insert at the end newNode->back = location; location->next = newNode; newNode->next = NULL; } } else { // insert into an empty list listData = newNode; newNode->next = NULL; newNode->back = NULL; } length++; }
  • 14. Deleting from a Doubly Linked List • Be careful about the end cases!!
  • 15. Headers and Trailers • Special cases arise when we are dealing with the first or last nodes • How can we simplify the implementation? – Idea: make sure that we never insert or delete the ends of the list – How? Set up dummy nodes with values outside of the range of possible values
  • 16. Headers and Trailers (cont.) • Header Node: contains a value smaller than any possible list element • Trailer Node: contains a value larger than any possible list element
  • 17. A linked list as an array of records • What are the advantages of using linked lists? (1) Dynamic memory allocation (2) Efficient insertion-deletion (for sorted lists) • Can we implement a linked list without dynamic memory allocation ?
  • 18. A linked list as an array of records (cont.)
  • 19. Case Study: Implementing a large integer ADT • The range of integer values varies from one computer to another • For long integers, the range is [-2,147,483,648 to 2,147,483,647] • How can we manipulate larger integers?
  • 20. Case Study: Implementing a large integer ADT (cont.) - A special list ADT
  • 21. Exercises • 1, 6, 8, 10, 12