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 Doubly Linked Lists in Under 40

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 Doubly Linked Lists in Under 40 (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

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

Doubly Linked Lists in Under 40

  • 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