際際滷

際際滷Share a Scribd company logo
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)
LinkedDoublyLists.ppt
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 (20)

Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
Linked list
Linked listLinked list
Linked list
Hoang Nguyen
Linked list
Linked listLinked list
Linked list
Fraboni Ec
Linked list
Linked listLinked list
Linked list
Luis Goldster
Linked list
Linked listLinked list
Linked list
Tony Nguyen
Linked list
Linked listLinked list
Linked list
Harry Potter
Linked list
Linked listLinked list
Linked list
James Wong
Linked list
Linked listLinked list
Linked list
Young Alista
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
Hanif Durad
LL-converted.pptx Advanced Data Structures
LL-converted.pptx Advanced Data StructuresLL-converted.pptx Advanced Data Structures
LL-converted.pptx Advanced Data Structures
snehalkulkarni78
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Lifna C.S
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
Linked list
Linked list Linked list
Linked list
Arbind Mandal
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
Introduction to Data structures and Trees.ppt
Introduction to Data structures and Trees.pptIntroduction to Data structures and Trees.ppt
Introduction to Data structures and Trees.ppt
Vivekananda Gn
Introduction to linked Lists in data structure.ppt
Introduction to linked Lists in data structure.pptIntroduction to linked Lists in data structure.ppt
Introduction to linked Lists in data structure.ppt
princydwn
linkedLists.ppt
linkedLists.pptlinkedLists.ppt
linkedLists.ppt
Sachin266673
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
Linked list
Linked listLinked list
Linked list
Fraboni Ec
Linked list
Linked listLinked list
Linked list
James Wong
LL-converted.pptx Advanced Data Structures
LL-converted.pptx Advanced Data StructuresLL-converted.pptx Advanced Data Structures
LL-converted.pptx Advanced Data Structures
snehalkulkarni78
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Lifna C.S
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
Introduction to Data structures and Trees.ppt
Introduction to Data structures and Trees.pptIntroduction to Data structures and Trees.ppt
Introduction to Data structures and Trees.ppt
Vivekananda Gn
Introduction to linked Lists in data structure.ppt
Introduction to linked Lists in data structure.pptIntroduction to linked Lists in data structure.ppt
Introduction to linked Lists in data structure.ppt
princydwn
linkedLists.ppt
linkedLists.pptlinkedLists.ppt
linkedLists.ppt
Sachin266673

More from veenatanmaipatlolla (11)

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

Recently uploaded (20)

Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
only history of java.pptx real bihind the name java
only history of java.pptx real bihind the name javaonly history of java.pptx real bihind the name java
only history of java.pptx real bihind the name java
mushtaqsaliq9
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
US Patented ReGenX Generator, ReGen-X Quatum Motor EV Regenerative Accelerati...
Thane Heins NOBEL PRIZE WINNING ENERGY RESEARCHER
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVName.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
MerijimArsedelPalmad1
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
G8 mini project for alcohol detection and engine lock system with GPS tracki...
G8 mini project for  alcohol detection and engine lock system with GPS tracki...G8 mini project for  alcohol detection and engine lock system with GPS tracki...
G8 mini project for alcohol detection and engine lock system with GPS tracki...
sahillanjewar294
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptxUNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
KesavanT10
Engineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdfEngineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdf
Sona
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
Indian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical EngineeringIndian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical Engineering
Rajani Vyawahare
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415
Mathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptxMathematics_behind_machine_learning_INT255.pptx
Mathematics_behind_machine_learning_INT255.pptx
ppkmurthy2006
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Equipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding ProcessEquipment for Gas Metal Arc Welding Process
Equipment for Gas Metal Arc Welding Process
AhmadKamil87
only history of java.pptx real bihind the name java
only history of java.pptx real bihind the name javaonly history of java.pptx real bihind the name java
only history of java.pptx real bihind the name java
mushtaqsaliq9
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVName.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Name.docxVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
MerijimArsedelPalmad1
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
G8 mini project for alcohol detection and engine lock system with GPS tracki...
G8 mini project for  alcohol detection and engine lock system with GPS tracki...G8 mini project for  alcohol detection and engine lock system with GPS tracki...
G8 mini project for alcohol detection and engine lock system with GPS tracki...
sahillanjewar294
health safety and environment presentation
health safety and environment presentationhealth safety and environment presentation
health safety and environment presentation
ssuserc606c7
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptxUNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
UNIT 1FUNDAMENTALS OF OPERATING SYSTEMS.pptx
KesavanT10
Engineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdfEngineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdf
Sona
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2Best KNow  Hydrogen Fuel Production in the World The cost in USD kwh for H2
Best KNow Hydrogen Fuel Production in the World The cost in USD kwh for H2
Daniel Donatelli
Industrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products ProfileIndustrial Valves, Instruments Products Profile
Industrial Valves, Instruments Products Profile
zebcoeng
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...Air pollution is contamination of the indoor or outdoor environment by any ch...
Air pollution is contamination of the indoor or outdoor environment by any ch...
dhanashree78
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
Integration of Additive Manufacturing (AM) with IoT : A Smart Manufacturing A...
ASHISHDESAI85
Indian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical EngineeringIndian Soil Classification System in Geotechnical Engineering
Indian Soil Classification System in Geotechnical Engineering
Rajani Vyawahare
Power Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.pptPower Point Presentation for Electrical Engineering 3-phase.ppt
Power Point Presentation for Electrical Engineering 3-phase.ppt
Aniket_1415

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)
  • 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