SlideShare a Scribd company logo
Linked Lists 
Christopher Simpkins 
chris.simpkins@gatech.edu 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 1 / 12
Linked Lists 
Dynamic data structures 
Singly linked lists 
Generic linked lists 
Doubly linked lists 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 2 / 12
Dynamic Data Structures 
ArrayList was our first dynamically-allocated data structure. 
ArrayList is a hybrid static and dynamically allocated data 
structure. 
ArrayList automatically allocates a larger backing (static) array 
when the capacity of its current backing array is exceeded. 
In a purely dynamic data structure, storage for each new element 
is allocated when the element is added. 
A purely dynamically allocated data structure is (slightly) more 
space efficient, but slower because heap allocation occurs every 
time an element is added. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 3 / 12
Linked Data Structures 
The core concept in a linked data structures is the node. 
A data structure is a collection of nodes linked in a particular way. 
A node holds a data item and links to other nodes. 
The nature of the links determines the kind of data structure, e.g., 
singly linked list, doubly linked list, binary tree, etc. 
Here is a depiction of a node for a singly linked list and code that 
implements such a node (public members for brevity) 
private class Node { 
public Object data; 
public Node next; 
public Node(Object data, Node next) { 
this.data = data; 
this.next = next; 
} 
} 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 4 / 12
Singly Linked Lists 
A singly linked list 
Contains a pointer to a “head” node (null if empty). 
The head node’s next points to the second node, the second 
node’s next points to the third node, and so on. 
The next reference of the last node is null 
Here’s a depiction of the nodes in a singly linked list with three 
elements: 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 5 / 12
Adding Elements to a Singly Linked List 
1. Create a new Node. 
2. Set the next reference of the new Node to the current head. 
3. Set the head reference to the new Node 
See LinkedList.java for the code. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 6 / 12
Finding an Item in a Linked List 
An algorithm for finding an item in a linked list: 
foundNode: Node := null 
currentNode: Node := LinkedList.head 
while currentNode != null && foundNode = null 
if currentNode.data = queryItem 
foundNode := currentNode 
currentNode := currentNode.next 
The postcondition of this algorithm is that foundNode will be: 
The node containing the query item, or 
null if the query item is not in the list. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 7 / 12
Inserting Elements into a Linked List 
1. Find the existing Node to insert new element after. 
2. Create a new Node. 
3. Set the next reference of the new Node to the next reference of 
the existing node. 
4. Set the next reference of the existing node to the new Node. 
See LinkedList.java for the code. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 8 / 12
Computing the Length of a Linked List 
An algorithm for computing the length a linked list: 
length: int := 0 
currentNode: Node := LinkedList.head 
while currentNode != null 
length := length + 1 
currentNode := currentNode.next 
The postcondition of this algorithm is that length will be equal to the 
number of nodes in the list. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 9 / 12
Generic Linked Lists 
To make our LinkedList generic we only need to add a type parameter 
to the declaration: 
public class GenericLinkedList<E> { ... 
and replace Object with E in the body of the class. 
See GenericLinkedList.java 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 10 / 12
Doubly Linked Lists 
A doubly linked list simply adds a previous reference to the Node 
class so that the list can be traversed forwards or backwards. 
private class Node<E> { 
E data; 
Node<E> next; 
Node<E> previous; 
public Node(E data, Node<E> next, Node<E> previous) { 
this.data = data; 
this.next = next; 
this.previous = previous; 
} 
} 
Doubly linked lists work the same, except that the algorithms for 
inserting and removing items requires a bit more link fiddling (have to 
set previous links as well). 
See DoublyLinkedList.java. 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 11 / 12
Programming Exercises 
Programming Exercise 1 
Add a get(int index) method to GenericLinkedList. 
Add a remove(T item) method to GenericLinkedList. 
Programming Exercise 2 
Implement public static int binarySearch(int[] a, 
int v). Return -1 if v is not in a. 
Bonus: implement public static <T> int 
binarySearch(T[] a, T v, Comparator<? super T> 
c). Return -1 if v is not in a. 
Bonus: for either of the options above, implement your method 
using a recursive helper method. 
Bonus question: if we wanted to implement a similar method for a 
Collection, how would we do it? Could we define such a binary 
search method for any Collection? 
Bonus question 2: what is the running time (Big-O) of binary 
search? 
Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 12 / 12

More Related Content

What's hot

C programming
C programmingC programming
C programming
Karthikeyan A K
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
Data structure
 Data structure Data structure
Data structure
Shahariar limon
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
Teksify
 
linked list
linked list linked list
linked list
Mohaimin Rahat
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
Sukhdeep Kaur
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5Kumar
 
Ds06 linked list- insert a node after a given node
Ds06   linked list-  insert a node after a given nodeDs06   linked list-  insert a node after a given node
Ds06 linked list- insert a node after a given node
jyoti_lakhani
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memoryecomputernotes
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
ecomputernotes
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists MrDavinderSingh
 
Linked list
Linked listLinked list
Linked listVONI
 

What's hot (20)

C programming
C programmingC programming
C programming
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Data structure
 Data structure Data structure
Data structure
 
Linked List
Linked ListLinked List
Linked List
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
linked list
linked list linked list
linked list
 
Ppt of operations on one way link list
Ppt of operations on one way  link listPpt of operations on one way  link list
Ppt of operations on one way link list
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Ds06 linked list- insert a node after a given node
Ds06   linked list-  insert a node after a given nodeDs06   linked list-  insert a node after a given node
Ds06 linked list- insert a node after a given node
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memory
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Insertion into linked lists
Insertion into linked lists Insertion into linked lists
Insertion into linked lists
 
Linked list
Linked listLinked list
Linked list
 

Similar to L7

L3
L3L3
L3
lksoo
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
ArifKamal36
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
KamranAli649587
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
Gokiladeepa
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
3.ppt
3.ppt3.ppt
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
Hanif Durad
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
edgar6wallace88877
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
kumarkaushal17
 
Datastructures using c++
Datastructures using c++Datastructures using c++
Datastructures using c++
Gopi Nath
 
Link list
Link listLink list
Link list
zzzubair
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
General Data structures
General Data structuresGeneral Data structures
General Data structures
Youssef Elsalhawy
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
ASADAHMAD811380
 
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
widespreadpromotion
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptx
UsriDevi1
 
Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
DrSudeshna
 

Similar to L7 (20)

L3
L3L3
L3
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
 
topic11LinkedLists.ppt
topic11LinkedLists.ppttopic11LinkedLists.ppt
topic11LinkedLists.ppt
 
Linked List
Linked ListLinked List
Linked List
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docxSIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
SIT221 Data Structures and Algorithms     Trimester 2, 2019 .docx
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
Datastructures using c++
Datastructures using c++Datastructures using c++
Datastructures using c++
 
Link list
Link listLink list
Link list
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
General Data structures
General Data structuresGeneral Data structures
General Data structures
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
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
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptx
 
Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
 

More from lksoo

Lo48
Lo48Lo48
Lo48
lksoo
 
Lo43
Lo43Lo43
Lo43
lksoo
 
Lo39
Lo39Lo39
Lo39lksoo
 
Lo37
Lo37Lo37
Lo37
lksoo
 
Lo27
Lo27Lo27
Lo27
lksoo
 
Lo17
Lo17Lo17
Lo17
lksoo
 
Lo12
Lo12Lo12
Lo12
lksoo
 
T3
T3T3
T3
lksoo
 
T2
T2T2
T2
lksoo
 
T1
T1T1
T1
lksoo
 
T4
T4T4
T4
lksoo
 
P5
P5P5
P5
lksoo
 
P4
P4P4
P4
lksoo
 
P3
P3P3
P3
lksoo
 
P1
P1P1
P1
lksoo
 
P2
P2P2
P2
lksoo
 
L10
L10L10
L10
lksoo
 
L9
L9L9
L9
lksoo
 
L8
L8L8
L8
lksoo
 
L6
L6L6
L6
lksoo
 

More from lksoo (20)

Lo48
Lo48Lo48
Lo48
 
Lo43
Lo43Lo43
Lo43
 
Lo39
Lo39Lo39
Lo39
 
Lo37
Lo37Lo37
Lo37
 
Lo27
Lo27Lo27
Lo27
 
Lo17
Lo17Lo17
Lo17
 
Lo12
Lo12Lo12
Lo12
 
T3
T3T3
T3
 
T2
T2T2
T2
 
T1
T1T1
T1
 
T4
T4T4
T4
 
P5
P5P5
P5
 
P4
P4P4
P4
 
P3
P3P3
P3
 
P1
P1P1
P1
 
P2
P2P2
P2
 
L10
L10L10
L10
 
L9
L9L9
L9
 
L8
L8L8
L8
 
L6
L6L6
L6
 

Recently uploaded

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
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
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
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
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
 
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
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
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
Tamralipta Mahavidyalaya
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
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)

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
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
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
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
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
 
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
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
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
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
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
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
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
 

L7

  • 1. Linked Lists Christopher Simpkins chris.simpkins@gatech.edu Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 1 / 12
  • 2. Linked Lists Dynamic data structures Singly linked lists Generic linked lists Doubly linked lists Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 2 / 12
  • 3. Dynamic Data Structures ArrayList was our first dynamically-allocated data structure. ArrayList is a hybrid static and dynamically allocated data structure. ArrayList automatically allocates a larger backing (static) array when the capacity of its current backing array is exceeded. In a purely dynamic data structure, storage for each new element is allocated when the element is added. A purely dynamically allocated data structure is (slightly) more space efficient, but slower because heap allocation occurs every time an element is added. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 3 / 12
  • 4. Linked Data Structures The core concept in a linked data structures is the node. A data structure is a collection of nodes linked in a particular way. A node holds a data item and links to other nodes. The nature of the links determines the kind of data structure, e.g., singly linked list, doubly linked list, binary tree, etc. Here is a depiction of a node for a singly linked list and code that implements such a node (public members for brevity) private class Node { public Object data; public Node next; public Node(Object data, Node next) { this.data = data; this.next = next; } } Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 4 / 12
  • 5. Singly Linked Lists A singly linked list Contains a pointer to a “head” node (null if empty). The head node’s next points to the second node, the second node’s next points to the third node, and so on. The next reference of the last node is null Here’s a depiction of the nodes in a singly linked list with three elements: Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 5 / 12
  • 6. Adding Elements to a Singly Linked List 1. Create a new Node. 2. Set the next reference of the new Node to the current head. 3. Set the head reference to the new Node See LinkedList.java for the code. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 6 / 12
  • 7. Finding an Item in a Linked List An algorithm for finding an item in a linked list: foundNode: Node := null currentNode: Node := LinkedList.head while currentNode != null && foundNode = null if currentNode.data = queryItem foundNode := currentNode currentNode := currentNode.next The postcondition of this algorithm is that foundNode will be: The node containing the query item, or null if the query item is not in the list. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 7 / 12
  • 8. Inserting Elements into a Linked List 1. Find the existing Node to insert new element after. 2. Create a new Node. 3. Set the next reference of the new Node to the next reference of the existing node. 4. Set the next reference of the existing node to the new Node. See LinkedList.java for the code. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 8 / 12
  • 9. Computing the Length of a Linked List An algorithm for computing the length a linked list: length: int := 0 currentNode: Node := LinkedList.head while currentNode != null length := length + 1 currentNode := currentNode.next The postcondition of this algorithm is that length will be equal to the number of nodes in the list. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 9 / 12
  • 10. Generic Linked Lists To make our LinkedList generic we only need to add a type parameter to the declaration: public class GenericLinkedList<E> { ... and replace Object with E in the body of the class. See GenericLinkedList.java Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 10 / 12
  • 11. Doubly Linked Lists A doubly linked list simply adds a previous reference to the Node class so that the list can be traversed forwards or backwards. private class Node<E> { E data; Node<E> next; Node<E> previous; public Node(E data, Node<E> next, Node<E> previous) { this.data = data; this.next = next; this.previous = previous; } } Doubly linked lists work the same, except that the algorithms for inserting and removing items requires a bit more link fiddling (have to set previous links as well). See DoublyLinkedList.java. Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 11 / 12
  • 12. Programming Exercises Programming Exercise 1 Add a get(int index) method to GenericLinkedList. Add a remove(T item) method to GenericLinkedList. Programming Exercise 2 Implement public static int binarySearch(int[] a, int v). Return -1 if v is not in a. Bonus: implement public static <T> int binarySearch(T[] a, T v, Comparator<? super T> c). Return -1 if v is not in a. Bonus: for either of the options above, implement your method using a recursive helper method. Bonus question: if we wanted to implement a similar method for a Collection, how would we do it? Could we define such a binary search method for any Collection? Bonus question 2: what is the running time (Big-O) of binary search? Chris Simpkins (Georgia Tech) CS 1331 Introduction to Object Oriented Programming CS 1331 12 / 12