SlideShare a Scribd company logo
Doubly-Linked Lists
15-121 Fall 2020
Margaret Reid-Miller
Today
• Exam 1: mean 86, high 98
Amazing!!
• Hw6 will be posted tonight
• Change Hw due dates?
Plan for Today
• Singly-linked list removed at index method
• Java's LinkedList class
• Doubly-linked lists
Fall 2020 15-121 (Reid-Miller) 2
Singly-linked list
• Add at index 3
• Insert node between nodes at index 2 and 3.
• Think add “after 2,” not “before 3,” as you can
always add AFTER a node.
• When do you need to consider a special case?
When you add the first node (after head).
• Remove at index 3
• Remove returns the value of the node removed.
• Like add, need to remove the node “after 2”
• Are there any special cases to consider?
Fall 2020 15-121 (Reid-Miller) 3
Exercise: Remove at given index
// Removes the object at the given index.
// Returns the object removed
public E remove(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
Fall 2020 15-121 (Reid-Miller) 4
Remove at index 3 goal
Fall 2020 15-121 (Reid-Miller) 5
first
remove here
(index = 3)
null
2
obj
Remove at given index
// Removes the object at the given index.
// Returns the object removed
public E remove(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
if (index == 0) {
return removeFirst();
}
…
// cont’d
Fall 2020 15-121 (Reid-Miller) 6
Remove at given index (cont’d)
…
Node nodeBefore = first;
for (int i = 0; ; i++) {
nodeBefore = nodeBefore.next;
}
// remove the node
…
size--;
return obj;
}
Fall 2020 15-121 (Reid-Miller) 7
Remove at index 3
Fall 2020 15-121 (Reid-Miller) 8
first
remove here
(index = 3)
current
WRONG
1. current.next = current.next.next;
2. E obj = current.next.data;
null
2
2
obj
1
Remove at index 3
Fall 2020 15-121 (Reid-Miller) 9
first
remove here
(index = 3)
current
Correct
1. E obj = current.next.data;
2. current.next = current.next.next;
null
2
1
obj
2
Remove at given index (cont’d)
…
Node nodeBefore = first;
for (int i = 0; i < index-1; i++) {
nodeBefore = nodeBefore.next;
}
// remove the node
E obj = nodeBefore.next.data;
nodeBefore.next = nodeBefore.next.next;
size--;
return obj;
}
Fall 2020 15-121 (Reid-Miller) 10
Remove at given index (cont’d alternate)
…
Node nodeBefore = first;
for (int i = 0; i < index-1; i++) {
nodeBefore = nodeBefore.next;
}
// remove the node
Node nodeToRemove = nodeBefore.next;
nodeBefore.next = nodeToRemove.next;
size--;
return nodeToRemove.data;
}
Fall 2020 15-121 (Reid-Miller) 11
Complexity
On a singly linked list with n nodes:
Worst Average Best
addFirst __________________
removeFirst __________________
add(index, obj) __________________
remove(index) __________________
Fall 2020 15-121 (Reid-Miller) 12
O(n) O(n) O(1)
O(n) O(n) O(1)
O(1)
O(1)
Disadvantages of
Singly-Linked Lists
• Insertion into a list is generally linear.
• In order to insert a node at an index greater
than 0, we need a reference to the previous
node.
• In order to remove a node that is not the first
node, we need a reference to the previous
node.
• We can only traverse the list in one direction.
Fall 2020 15-121 (Reid-Miller) 13
Java's LinkedList class
Java's LinkedList add(E obj) method runtime is O(1).
How?
LinkedList has a field that refers to the
last node in the list.
Java's LinkedList remove(int index) method runtime
is O(1). How is it possible even if your have the last
node?
The nodes inside LinkedList are doubly linked in
order to remove from the end of the list in O(1) time.
Fall 2020 15-121 (Reid-Miller) 14
Doubly-Linked Lists
Fall 2020 15-121 (Reid-Miller) 15
prev data next
null
last
null
first
Doubly-Linked Lists
• Each data entry is stored in its own node
• Each node as two references: one reference is to the
next node and one is to the previous node (prev).
• A reference, often referred to as head, points to the
first node in the list and one reference, often referred
to as tail, points the the last node in the list.
• The head node’s prev reference is null and the tail
node’s next reference is null.
• An empty list would have head and tail references
equal to null
Fall 2020 15-121 (Reid-Miller) 16
Node class
private class Node {
private E data;
private Node prev;
private Node next;
private Node(E obj, Node previous, Node next){
data = obj;
prev = previous;
this.next = next;
}
Fall 2020 15-121 (Reid-Miller) 17
1. To add a node at a given index
find the node at index-1
Fall 2020 15-121 (Reid-Miller) 18
null
last
null
first
null
null
before
newNode
4. Result of adding at given index
Fall 2020 15-121 (Reid-Miller) 21
null
last
null
first
How would you implement addFirst?
Fall 2020 15-121 (Reid-Miller) 22
null
last
null
first
How would you implement addLast?
How would you remove the node
referenced by current?
Fall 2020 15-121 (Reid-Miller) 23
null
last
null
first current
How would you remove the node if
current == first?
Fall 2020 15-121 (Reid-Miller) 24
null
last
null
first current
How would you remove the node if
current == last?
Fall 2020 15-121 (Reid-Miller) 25
null
last
null
first current
How would you remove the node
current if first == tail?
Fall 2020 15-121 (Reid-Miller) 26
tail
null
head
null
current
Doubly-linked list complexity
int size()
boolean add(E obj)
void add(int index, E obj)
E get(int index)
E set(int index, E obj)
boolean contains(Object obj)
int indexOf(Object obj)
E remove(int index)
boolean remove(Object obj)
Fall 2020 15-121 (Reid-Miller) 27
Worst Average Best
O(1)
O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)
O(n) O(n) O(1)

More Related Content

Similar to 13-Doubly Linked List data structure.pdf

Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
MrsSHEEBAM
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
duraimurugan alagarsamy
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
3.linked list
3.linked list3.linked list
3.linked list
Chandan Singh
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
DikkySuryadiSKomMKom
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
Zia Ush Shamszaman
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
DoublyLinkedList.pptx
DoublyLinkedList.pptxDoublyLinkedList.pptx
DoublyLinkedList.pptx
Anum Zehra
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
raghavbirla63
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 

Similar to 13-Doubly Linked List data structure.pdf (20)

Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
3.linked list
3.linked list3.linked list
3.linked list
 
Data structures
Data structuresData structures
Data structures
 
linked-list.ppt
linked-list.pptlinked-list.ppt
linked-list.ppt
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
DoublyLinkedList.pptx
DoublyLinkedList.pptxDoublyLinkedList.pptx
DoublyLinkedList.pptx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 

More from ssusere3b1a2

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
ssusere3b1a2
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
ssusere3b1a2
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
ssusere3b1a2
 
stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
ssusere3b1a2
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
ssusere3b1a2
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
ssusere3b1a2
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
ssusere3b1a2
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
ssusere3b1a2
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
ssusere3b1a2
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
ssusere3b1a2
 

More from ssusere3b1a2 (10)

week9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptxweek9-prefixinfixandpostfixnotations-191013065821.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
 
Hashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.pptHashing data structure in genaral ss.ppt
Hashing data structure in genaral ss.ppt
 
stack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.pptstack in java ds - muhammed sdasdasdasdasdas.ppt
stack in java ds - muhammed sdasdasdasdasdas.ppt
 
stack in java data structure - muhammed .ppt
stack in java data structure - muhammed .pptstack in java data structure - muhammed .ppt
stack in java data structure - muhammed .ppt
 
slide6.pptx
slide6.pptxslide6.pptx
slide6.pptx
 
Chapter 6 Methods.pptx
Chapter 6 Methods.pptxChapter 6 Methods.pptx
Chapter 6 Methods.pptx
 
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptxChapter 4 Mathematical Functions, Characters, and Strings.pptx
Chapter 4 Mathematical Functions, Characters, and Strings.pptx
 
information retrieval project
information retrieval projectinformation retrieval project
information retrieval project
 
information retrieval --> dictionary.ppt
information retrieval --> dictionary.pptinformation retrieval --> dictionary.ppt
information retrieval --> dictionary.ppt
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
 

Recently uploaded

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
Sourabh Kumar
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
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
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
SachinKumar945617
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
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
ricssacare
 
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
Celine George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
ssuserbdd3e8
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
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...
Sayali Powar
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Denish Jangid
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
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À ĐÁ...
Nguyen Thanh Tu Collection
 
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
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
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
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
Extraction Of Natural Dye From Beetroot (Beta Vulgaris) And Preparation Of He...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
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
 
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
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
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...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
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À ĐÁ...
 
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
 

13-Doubly Linked List data structure.pdf

  • 1. Doubly-Linked Lists 15-121 Fall 2020 Margaret Reid-Miller
  • 2. Today • Exam 1: mean 86, high 98 Amazing!! • Hw6 will be posted tonight • Change Hw due dates? Plan for Today • Singly-linked list removed at index method • Java's LinkedList class • Doubly-linked lists Fall 2020 15-121 (Reid-Miller) 2
  • 3. Singly-linked list • Add at index 3 • Insert node between nodes at index 2 and 3. • Think add “after 2,” not “before 3,” as you can always add AFTER a node. • When do you need to consider a special case? When you add the first node (after head). • Remove at index 3 • Remove returns the value of the node removed. • Like add, need to remove the node “after 2” • Are there any special cases to consider? Fall 2020 15-121 (Reid-Miller) 3
  • 4. Exercise: Remove at given index // Removes the object at the given index. // Returns the object removed public E remove(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException(); Fall 2020 15-121 (Reid-Miller) 4
  • 5. Remove at index 3 goal Fall 2020 15-121 (Reid-Miller) 5 first remove here (index = 3) null 2 obj
  • 6. Remove at given index // Removes the object at the given index. // Returns the object removed public E remove(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException(); if (index == 0) { return removeFirst(); } … // cont’d Fall 2020 15-121 (Reid-Miller) 6
  • 7. Remove at given index (cont’d) … Node nodeBefore = first; for (int i = 0; ; i++) { nodeBefore = nodeBefore.next; } // remove the node … size--; return obj; } Fall 2020 15-121 (Reid-Miller) 7
  • 8. Remove at index 3 Fall 2020 15-121 (Reid-Miller) 8 first remove here (index = 3) current WRONG 1. current.next = current.next.next; 2. E obj = current.next.data; null 2 2 obj 1
  • 9. Remove at index 3 Fall 2020 15-121 (Reid-Miller) 9 first remove here (index = 3) current Correct 1. E obj = current.next.data; 2. current.next = current.next.next; null 2 1 obj 2
  • 10. Remove at given index (cont’d) … Node nodeBefore = first; for (int i = 0; i < index-1; i++) { nodeBefore = nodeBefore.next; } // remove the node E obj = nodeBefore.next.data; nodeBefore.next = nodeBefore.next.next; size--; return obj; } Fall 2020 15-121 (Reid-Miller) 10
  • 11. Remove at given index (cont’d alternate) … Node nodeBefore = first; for (int i = 0; i < index-1; i++) { nodeBefore = nodeBefore.next; } // remove the node Node nodeToRemove = nodeBefore.next; nodeBefore.next = nodeToRemove.next; size--; return nodeToRemove.data; } Fall 2020 15-121 (Reid-Miller) 11
  • 12. Complexity On a singly linked list with n nodes: Worst Average Best addFirst __________________ removeFirst __________________ add(index, obj) __________________ remove(index) __________________ Fall 2020 15-121 (Reid-Miller) 12 O(n) O(n) O(1) O(n) O(n) O(1) O(1) O(1)
  • 13. Disadvantages of Singly-Linked Lists • Insertion into a list is generally linear. • In order to insert a node at an index greater than 0, we need a reference to the previous node. • In order to remove a node that is not the first node, we need a reference to the previous node. • We can only traverse the list in one direction. Fall 2020 15-121 (Reid-Miller) 13
  • 14. Java's LinkedList class Java's LinkedList add(E obj) method runtime is O(1). How? LinkedList has a field that refers to the last node in the list. Java's LinkedList remove(int index) method runtime is O(1). How is it possible even if your have the last node? The nodes inside LinkedList are doubly linked in order to remove from the end of the list in O(1) time. Fall 2020 15-121 (Reid-Miller) 14
  • 15. Doubly-Linked Lists Fall 2020 15-121 (Reid-Miller) 15 prev data next null last null first
  • 16. Doubly-Linked Lists • Each data entry is stored in its own node • Each node as two references: one reference is to the next node and one is to the previous node (prev). • A reference, often referred to as head, points to the first node in the list and one reference, often referred to as tail, points the the last node in the list. • The head node’s prev reference is null and the tail node’s next reference is null. • An empty list would have head and tail references equal to null Fall 2020 15-121 (Reid-Miller) 16
  • 17. Node class private class Node { private E data; private Node prev; private Node next; private Node(E obj, Node previous, Node next){ data = obj; prev = previous; this.next = next; } Fall 2020 15-121 (Reid-Miller) 17
  • 18. 1. To add a node at a given index find the node at index-1 Fall 2020 15-121 (Reid-Miller) 18 null last null first null null before newNode
  • 19. 4. Result of adding at given index Fall 2020 15-121 (Reid-Miller) 21 null last null first
  • 20. How would you implement addFirst? Fall 2020 15-121 (Reid-Miller) 22 null last null first How would you implement addLast?
  • 21. How would you remove the node referenced by current? Fall 2020 15-121 (Reid-Miller) 23 null last null first current
  • 22. How would you remove the node if current == first? Fall 2020 15-121 (Reid-Miller) 24 null last null first current
  • 23. How would you remove the node if current == last? Fall 2020 15-121 (Reid-Miller) 25 null last null first current
  • 24. How would you remove the node current if first == tail? Fall 2020 15-121 (Reid-Miller) 26 tail null head null current
  • 25. Doubly-linked list complexity int size() boolean add(E obj) void add(int index, E obj) E get(int index) E set(int index, E obj) boolean contains(Object obj) int indexOf(Object obj) E remove(int index) boolean remove(Object obj) Fall 2020 15-121 (Reid-Miller) 27 Worst Average Best O(1) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(1)