SlideShare a Scribd company logo
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Link Lists Gone Wild:
Circular and Doubly
Linked Lists
Linked Lists Gone Wild: Doubly-Linked Lists page 2
© Dr. Jonathan (Yahya) Cazalas
Double-Linked Lists
 Limitations of a singly-linked list include:
 Insertion at the front is O(1)
 insertion at other positions is O(n)
 Insertion is convenient only after a referenced node
 Removing a node requires a reference to the previous
node
 We can traverse the list only in the forward direction
 We can overcome these limitations:
 Add a reference in each node to the previous node,
creating a double-linked list
Linked Lists Gone Wild: Doubly-Linked Lists page 3
© Dr. Jonathan (Yahya) Cazalas
Double-Linked Lists (cont.)
Linked Lists Gone Wild: Doubly-Linked Lists page 4
© Dr. Jonathan (Yahya) Cazalas
Linked Lists
 Here’s a doubly linked list node class:
public class DLLnode {
public int data;
public dll_node next;
public dll_node prev;
// Constructor 1
public DLLnode(int i) {
this(i, null, null);
}
// Constructor 2
public DLLnode(int i, dll_node n, dll_node p) {
data = i;
next = n;
prev = p;
}
}
Each ll_node object has
at least 3 fields:
(1) data field, which
stores information
(2) next field, which is
used to link the nodes
together
(3) previous, which links
to previous node
Linked Lists Gone Wild: Doubly-Linked Lists page 5
© Dr. Jonathan (Yahya) Cazalas
Inserting into a Double-Linked
List
next =
= prev
data = "Harry"
DLLnode
next = null
= prev
data = "Sam"
DLLnode
next =
= prev
data = "Sharon"
DLLnode
DLLnode newNode= new DLLnode("Sharon");
newNode.next = helpPtr;
newNode.prev = helpPtr.prev;
helpPtr.prev.next = newNode;
helpPtr.prev = newNode;
from predecessor
to
predecessor
helpPtr
newNode
Linked Lists Gone Wild: Doubly-Linked Lists page 6
© Dr. Jonathan (Yahya) Cazalas
Removing from a Double-Linked
List
next =
= prev
data = "Bob"
Node
next =
= prev
data = "Harry"
Node
next =
= prev
data = "Sharon"
Node
helpPtr
helpPtr.prev.next = helpPtr.next;
helpPtr.next.prev = helpPtr.prev;
Linked Lists Gone Wild: Doubly-Linked Lists page 7
© Dr. Jonathan (Yahya) Cazalas
A Double-Linked List Class
So far we have worked only
with internal nodes
As with the single-linked class,
it is best to access the internal
nodes with a double-linked list
object
A double-linked list object has data fields:
 head (a reference to the first list Node)
 tail (a reference to the last list Node)
Insertion at either end is O(1)
insertion elsewhere is still O(n)
Linked Lists Gone Wild: Doubly-Linked Lists page 8
© Dr. Jonathan (Yahya) Cazalas
Circular Lists
 Circular double-linked list:
 Link last node to the first node, and
 Link first node to the last node
 We can also build singly-linked circular lists:
 Traverse in forward direction only
 Advantages:
 Continue to traverse even after passing the first or last node
 Visit all elements from any starting point
 Never fall off the end of a list
 Disadvantage: Code must avoid an infinite loop!
Linked Lists Gone Wild: Doubly-Linked Lists page 9
© Dr. Jonathan (Yahya) Cazalas
Circular Lists (cont.)
Linked Lists Gone Wild: Doubly-Linked Lists page 10
© Dr. Jonathan (Yahya) Cazalas
Linked Lists Gone Wild
ISN’T
THAT
GREAT ( )!
That we are finished
with Linked Lists!
Linked Lists Gone Wild: Doubly-Linked Lists page 11
© Dr. Jonathan (Yahya) Cazalas
Daily Demotivator
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Link Lists Gone Wild:
Circular and Doubly
Linked Lists

More Related Content

What's hot

Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
SWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mappingSWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mapping
Mariano Rodriguez-Muro
 

What's hot (20)

Sql ppt
Sql pptSql ppt
Sql ppt
 
Trees
TreesTrees
Trees
 
Data struters
Data strutersData struters
Data struters
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
 
Data structures
Data structuresData structures
Data structures
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Data structure
Data structureData structure
Data structure
 
Database structure
Database structureDatabase structure
Database structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
SWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mappingSWT Lecture Session 9 - RDB2RDF direct mapping
SWT Lecture Session 9 - RDB2RDF direct mapping
 
Linear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueLinear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and Queue
 
Advanced Trees
Advanced TreesAdvanced Trees
Advanced Trees
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Lecture1 data structure(introduction)
Lecture1 data structure(introduction)Lecture1 data structure(introduction)
Lecture1 data structure(introduction)
 

Similar to linked_lists4

Similar to linked_lists4 (20)

[Queue , linked list , tree]
[Queue , linked list , tree][Queue , linked list , tree]
[Queue , linked list , tree]
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
Data Structures
Data StructuresData Structures
Data Structures
 
Double link list
Double link listDouble link list
Double link list
 
Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
 
Linked list
Linked listLinked list
Linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST
Unit 3 dsa LINKED LIST
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Linked List Basics
Linked List BasicsLinked List Basics
Linked List Basics
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklist
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
linked list
linked listlinked list
linked list
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
linklisr
linklisrlinklisr
linklisr
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 

More from Mohamed Elsayed

More from Mohamed Elsayed (20)

binary_trees4
binary_trees4binary_trees4
binary_trees4
 
binary_trees3
binary_trees3binary_trees3
binary_trees3
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
queues
queuesqueues
queues
 
stacks2
stacks2stacks2
stacks2
 
stacks1
stacks1stacks1
stacks1
 
algorithm_analysis2
algorithm_analysis2algorithm_analysis2
algorithm_analysis2
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
recursion3
recursion3recursion3
recursion3
 
recursion2
recursion2recursion2
recursion2
 
recursion1
recursion1recursion1
recursion1
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
binary_search
binary_searchbinary_search
binary_search
 
arrays
arraysarrays
arrays
 
introduction
introductionintroduction
introduction
 
heaps2
heaps2heaps2
heaps2
 
heaps
heapsheaps
heaps
 
hash_tables
hash_tableshash_tables
hash_tables
 
graphs
graphsgraphs
graphs
 
quick_sort
quick_sortquick_sort
quick_sort
 

Recently uploaded

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 

Recently uploaded (20)

50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 

linked_lists4

  • 1. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Link Lists Gone Wild: Circular and Doubly Linked Lists
  • 2. Linked Lists Gone Wild: Doubly-Linked Lists page 2 © Dr. Jonathan (Yahya) Cazalas Double-Linked Lists  Limitations of a singly-linked list include:  Insertion at the front is O(1)  insertion at other positions is O(n)  Insertion is convenient only after a referenced node  Removing a node requires a reference to the previous node  We can traverse the list only in the forward direction  We can overcome these limitations:  Add a reference in each node to the previous node, creating a double-linked list
  • 3. Linked Lists Gone Wild: Doubly-Linked Lists page 3 © Dr. Jonathan (Yahya) Cazalas Double-Linked Lists (cont.)
  • 4. Linked Lists Gone Wild: Doubly-Linked Lists page 4 © Dr. Jonathan (Yahya) Cazalas Linked Lists  Here’s a doubly linked list node class: public class DLLnode { public int data; public dll_node next; public dll_node prev; // Constructor 1 public DLLnode(int i) { this(i, null, null); } // Constructor 2 public DLLnode(int i, dll_node n, dll_node p) { data = i; next = n; prev = p; } } Each ll_node object has at least 3 fields: (1) data field, which stores information (2) next field, which is used to link the nodes together (3) previous, which links to previous node
  • 5. Linked Lists Gone Wild: Doubly-Linked Lists page 5 © Dr. Jonathan (Yahya) Cazalas Inserting into a Double-Linked List next = = prev data = "Harry" DLLnode next = null = prev data = "Sam" DLLnode next = = prev data = "Sharon" DLLnode DLLnode newNode= new DLLnode("Sharon"); newNode.next = helpPtr; newNode.prev = helpPtr.prev; helpPtr.prev.next = newNode; helpPtr.prev = newNode; from predecessor to predecessor helpPtr newNode
  • 6. Linked Lists Gone Wild: Doubly-Linked Lists page 6 © Dr. Jonathan (Yahya) Cazalas Removing from a Double-Linked List next = = prev data = "Bob" Node next = = prev data = "Harry" Node next = = prev data = "Sharon" Node helpPtr helpPtr.prev.next = helpPtr.next; helpPtr.next.prev = helpPtr.prev;
  • 7. Linked Lists Gone Wild: Doubly-Linked Lists page 7 © Dr. Jonathan (Yahya) Cazalas A Double-Linked List Class So far we have worked only with internal nodes As with the single-linked class, it is best to access the internal nodes with a double-linked list object A double-linked list object has data fields:  head (a reference to the first list Node)  tail (a reference to the last list Node) Insertion at either end is O(1) insertion elsewhere is still O(n)
  • 8. Linked Lists Gone Wild: Doubly-Linked Lists page 8 © Dr. Jonathan (Yahya) Cazalas Circular Lists  Circular double-linked list:  Link last node to the first node, and  Link first node to the last node  We can also build singly-linked circular lists:  Traverse in forward direction only  Advantages:  Continue to traverse even after passing the first or last node  Visit all elements from any starting point  Never fall off the end of a list  Disadvantage: Code must avoid an infinite loop!
  • 9. Linked Lists Gone Wild: Doubly-Linked Lists page 9 © Dr. Jonathan (Yahya) Cazalas Circular Lists (cont.)
  • 10. Linked Lists Gone Wild: Doubly-Linked Lists page 10 © Dr. Jonathan (Yahya) Cazalas Linked Lists Gone Wild ISN’T THAT GREAT ( )! That we are finished with Linked Lists!
  • 11. Linked Lists Gone Wild: Doubly-Linked Lists page 11 © Dr. Jonathan (Yahya) Cazalas Daily Demotivator
  • 12. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Link Lists Gone Wild: Circular and Doubly Linked Lists