SlideShare a Scribd company logo
1
Topic 11
Linked Lists
"All the kids who did great in high school writing
pong games in BASIC for their Apple II would get to
college, take CompSci 101, a data structures
course, and when they hit the pointers business their
brains would just totally explode, and the next thing
you knew, they were majoring in Political Science
because law school seemed like a better idea."
-Joel Spolsky
Thanks to Don Slater of CMU for use of his slides.
Clicker 1
What is output by the following code?
ArrayList<Integer> a1 = new ArrayList<>();
ArrayList<Integer> a2 = new ArrayList<>();
a1.add(12);
a2.add(12);
System.out.println(a1 == a2);
A. false
B. true
C. No output due to syntax error
D. No output due to runtime error
E. Varies from one run of the program to the next
CS314
Linked Lists
2
CS314
Linked Lists
3
Dynamic Data Structures
Dynamic data structures
– They grow and shrink one element at a time,
normally without some of the inefficiencies of
arrays
– as opposed to a static container such as an array
Big O of Array Manipulations
– Access the kth element
– Add or delete an element in the middle of the
array while maintaining relative order
– adding element at the end of array? space
avail? no space avail?
– add element at beginning of an array
Linked Lists
4
Object References
Recall that an object reference is a variable
that stores the address of an object
A reference can also be called a pointer
They are often depicted graphically:
student
John Smith
40725
3.57
CS314
Linked Lists
5
References as Links
Object references can be used to create
links between objects
Suppose a Student class contained a
reference to another Student object
John Smith
40725
3.57
Jane Jones
58821
3.72
CS314
Linked Lists
6
References as Links
References can be used to create a variety
of linked structures, such as a linked list:
studentList
CS314
CS314
Linked Lists
7
Linked Lists
A linear collection of self-referential objects, called
nodes, connected by other links
– linear: for every node in the list, there is one and only one node
that precedes it (except for possibly the first node, which may
have no predecessor,) and there is one and only one node that
succeeds it, (except for possibly the last node, which may have
no successor)
– self-referential: a node that has the ability to refer to another
node of the same type, or even to refer to itself
– node: contains data of any type, including a reference to another
node of the same data type, or to nodes of different data types
– Usually a list will have a beginning and an end; the first element
in the list is accessed by a reference to that class, and the last
node in the list will have a reference that is set to null
CS314
Linked Lists
8
Linked lists are dynamic, they can grow or shrink
as necessary
Linked lists are non-contiguous; the logical
sequence of items in the structure is decoupled
from any physical ordering in memory
Advantages of linked lists
CS314
Linked Lists
9
Nodes and Lists
A different way of implementing a list
Each element of a Linked List is a separate
Node object.
Each Node tracks a single piece of data plus
a reference (pointer) to the next
Create a new Node very time we add
something to the List
Remove nodes when item removed from list
and allow garbage collector to reclaim that
memory
CS314
Linked Lists
10
A Node Class
public class Node<E> {
private E myData;
private Node<E> myNext;
public Node()
{ myData = null; myNext = null; }
public Node(E data, Node<E> next)
{ myData = data; myNext = next; }
public E getData()
{ return myData; }
public Node<E> getNext()
{ return myNext; }
public void setData(E data)
{ myData = data; }
public void setNext(Node<E> next)
{ myNext = next; }
}
CS314
Linked Lists
11
One Implementation of a Linked List
The Nodes show on the previous slide are
singly linked
– a node refers only to the next node in the
structure
– it is also possible to have doubly linked nodes.
– The node has a reference to the next node in the
structure and the previous node in the structure
as well
How is the end of the list indicated
– myNext = null for last node
– a separate dummy node class / object
CS314
Linked Lists
12
A Linked List Implementation
public class LinkedList<E> implements IList<E>
private Node<E> head;
private Node<E> tail;
private int size;
public LinkedList(){
head = null;
tail = null;
size = 0;
}
}
LinkedList<String> list = new LinkedList<String>();
LinkedList
myHead iMySize
myTail
null
null
0
CS314
Linked Lists
13
Writing Methods
When trying to code methods for Linked
Lists draw pictures!
– If you don't draw pictures of what you are trying
to do it is very easy to make mistakes!
CS314
Linked Lists
14
add method
add to the end of list
special case if empty
steps on following slides
public void add(E obj)
CS314
Linked Lists
15
Add Element - List Empty (Before)
head tail size
null null 0
Object
item
CS314
Linked Lists
16
Add Element - List Empty (After)
head tail size
1
String
Node
myData myNext
null
CS314
Linked Lists
17
Add Element - List Not Empty (Before)
1
String
Node
myData myNext
null
head tail size
String
item
CS314
Linked Lists
18
Add Element - List Not Empty (After)
2
String
Node
myData myNext
head tail size
String
Node
myData myNext
null
CS314
Linked Lists
19
Code for default add
public void add(E obj)
Clicker 2
What is the worst case Big O for adding to
the end of an array based list and our
LinkedList314 class? The lists already
contain N items.
Array based Linked
A. O(1) O(1)
B. O(N) O(N)
C. O(logN) O(1)
D. O(1) O(N)
E. O(N) O(1)
20
Contains method
Implement a contains method for our Linked
List class
public boolean contains(E val) // val != null
CS314
Linked Lists
21
CS314
Linked Lists
22
Code for addFront
add to front of list
public void addFront(E obj)
How does this compare to adding at the front
of an array based list?
Clicker 3
What is the Big O for adding to the front of
an array based list and a linked list? The lists
already contain N items.
Array based Linked
A. O(1) O(1)
B. O(N) O(1)
C. O(logN) O(1)
D. O(1) O(N)
E. O(N) O(N)
CS314
Linked Lists
23
CS314
Linked Lists
24
Code for Insert
public void insert(int pos, E obj)
Must be careful not to break the chain!
Where do we need to go?
Special cases?
Clicker 4
What is the Big O for inserting an element
into the middle of an array based list and into
the middle of a linked list? Each list already
contains N items.
Array based Linked
A. O(1) O(1)
B. O(1) O(N)
C. O(N) O(1)
D. O(N) O(N)
E. O(N) O(logN)
CS314
Linked Lists
25
Clicker Question 5
What is the Big O for getting an element
based on position from an array based list
and from a linked list? Each list contains N
items. In other words E get(int pos)
Array based Linked
A. O(1) O(1)
B. O(1) O(N)
C. O(N) O(1)
D. O(logN) O(N)
E. O(N) O(N)
Linked Lists
26
CS314
Linked Lists
27
Code for get
public E get(int pos)
The downside of Linked Lists
CS314
Linked Lists
28
Code for remove
public E remove(int pos)
Clicker 6
What is the order to remove the last element
of a singly linked list with references to the
first and last nodes of the linked structure of
nodes?
The list contains N elements
A. O(1)
B. O(logN)
C. O(N^0.5)
D. O(N)
E. O(NlogN))
CS314
Linked Lists
29
CS314
Linked Lists
30
Why Use Linked List
What operations with a Linked List faster
than the version from ArrayList?
CS314
Linked Lists
31
Clicker 7 - Getting All Elements in
Order From a Linked List
What is the Order (Big O) of the following code?
LinkedList314<Integer> list;
list = new LinkedList314<Integer>();
// code to fill list with N elements
int total = 0;
//Big O of following code?
for(int i = 0; i < list.size(); i++)
total += list.get(i);
A. O(N) B. O(2N) C. O(NlogN)
D. O(N2) E. O(N3)
Iterators to the Rescue
CS314
Linked Lists
32
CS314
Linked Lists
33
Other Possible Features of
Linked Lists
Doubly Linked
Circular
Dummy Nodes for first and last node in list
public class DLNode<E> {
private E myData;
private DLNode<E> myNext;
private DLNode<E> myPrevious;
}
CS314
Linked Lists
34
Dummy Nodes
Use of Dummy Nodes for a Doubly Linked
List removes most special cases
Also could make the Double Linked List
circular
CS314
Linked Lists
35
Doubly Linked List add
public void add(E obj)
CS314
Linked Lists
36
Insert for Doubly Linked List
public void insert(int pos, E obj)

More Related Content

Similar to topic11LinkedLists.ppt

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
3.ppt
3.ppt3.ppt
3.ppt
3.ppt3.ppt
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
Hanif Durad
 
C++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdfC++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdf
aashkaahm
 
Linked List Basics
Linked List BasicsLinked List Basics
Linked List Basics
KaustavRoy40
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
nikhilcse1
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
Poojith Chowdhary
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
HammadTariq51
 
In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdf
fms12345
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)
SURBHI SAROHA
 
Data structure
 Data structure Data structure
Data structure
Shahariar limon
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
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
 
linked_lists
linked_listslinked_lists
linked_lists
Mohamed Elsayed
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 

Similar to topic11LinkedLists.ppt (20)

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
3.ppt
3.ppt3.ppt
3.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
C++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdfC++ Please make sure that your answer is fully submitted .pdf
C++ Please make sure that your answer is fully submitted .pdf
 
Linked List Basics
Linked List BasicsLinked List Basics
Linked List Basics
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdf
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)
 
Data structure
 Data structure Data structure
Data structure
 
Linked List
Linked ListLinked List
Linked 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
 
linked_lists
linked_listslinked_lists
linked_lists
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 

More from KamranAli649587

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
KamranAli649587
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
KamranAli649587
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link list
KamranAli649587
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
KamranAli649587
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
KamranAli649587
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
KamranAli649587
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
KamranAli649587
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
KamranAli649587
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
KamranAli649587
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
KamranAli649587
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
KamranAli649587
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
KamranAli649587
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
KamranAli649587
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
KamranAli649587
 
Reaches
ReachesReaches
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
KamranAli649587
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
KamranAli649587
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
KamranAli649587
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
KamranAli649587
 
ch7.ppt
ch7.pptch7.ppt

More from KamranAli649587 (20)

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link list
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
 
Reaches
ReachesReaches
Reaches
 
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 

Recently uploaded

Drownings spike from May to August in children
Drownings spike from May to August in childrenDrownings spike from May to August in children
Drownings spike from May to August in children
Bisnar Chase Personal Injury Attorneys
 
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
9gr6pty
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
hqfek
 
Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)
GeorgiiSteshenko
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
Alireza Kamrani
 
Digital Marketing Performance Marketing Sample .pdf
Digital Marketing Performance Marketing  Sample .pdfDigital Marketing Performance Marketing  Sample .pdf
Digital Marketing Performance Marketing Sample .pdf
Vineet
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
zsafxbf
 
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdfreading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
perranet1
 
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
osoyvvf
 
Econ3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdfEcon3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdf
blueshagoo1
 
Sample Devops SRE Product Companies .pdf
Sample Devops SRE  Product Companies .pdfSample Devops SRE  Product Companies .pdf
Sample Devops SRE Product Companies .pdf
Vineet
 
Template xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptxTemplate xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptx
TeukuEriSyahputra
 
A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
dataschool1
 
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
Marlon Dumas
 
saps4hanaandsapanalyticswheretodowhat1565272000538.pdf
saps4hanaandsapanalyticswheretodowhat1565272000538.pdfsaps4hanaandsapanalyticswheretodowhat1565272000538.pdf
saps4hanaandsapanalyticswheretodowhat1565272000538.pdf
newdirectionconsulta
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
bmucuha
 
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
Rebecca Bilbro
 
Salesforce AI + Data Community Tour Slides - Canarias
Salesforce AI + Data Community Tour Slides - CanariasSalesforce AI + Data Community Tour Slides - Canarias
Salesforce AI + Data Community Tour Slides - Canarias
davidpietrzykowski1
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
agdhot
 
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
zoykygu
 

Recently uploaded (20)

Drownings spike from May to August in children
Drownings spike from May to August in childrenDrownings spike from May to August in children
Drownings spike from May to August in children
 
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
一比一原版(uob毕业证书)伯明翰大学毕业证如何办理
 
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
一比一原版爱尔兰都柏林大学毕业证(本硕)ucd学位证书如何办理
 
Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)
 
How To Control IO Usage using Resource Manager
How To Control IO Usage using Resource ManagerHow To Control IO Usage using Resource Manager
How To Control IO Usage using Resource Manager
 
Digital Marketing Performance Marketing Sample .pdf
Digital Marketing Performance Marketing  Sample .pdfDigital Marketing Performance Marketing  Sample .pdf
Digital Marketing Performance Marketing Sample .pdf
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
 
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdfreading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
reading_sample_sap_press_operational_data_provisioning_with_sap_bw4hana (1).pdf
 
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
一比一原版(uom毕业证书)曼彻斯特大学毕业证如何办理
 
Econ3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdfEcon3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdf
 
Sample Devops SRE Product Companies .pdf
Sample Devops SRE  Product Companies .pdfSample Devops SRE  Product Companies .pdf
Sample Devops SRE Product Companies .pdf
 
Template xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptxTemplate xxxxxxxx ssssssssssss Sertifikat.pptx
Template xxxxxxxx ssssssssssss Sertifikat.pptx
 
A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
 
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
Discovering Digital Process Twins for What-if Analysis: a Process Mining Appr...
 
saps4hanaandsapanalyticswheretodowhat1565272000538.pdf
saps4hanaandsapanalyticswheretodowhat1565272000538.pdfsaps4hanaandsapanalyticswheretodowhat1565272000538.pdf
saps4hanaandsapanalyticswheretodowhat1565272000538.pdf
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
PyData London 2024: Mistakes were made (Dr. Rebecca Bilbro)
 
Salesforce AI + Data Community Tour Slides - Canarias
Salesforce AI + Data Community Tour Slides - CanariasSalesforce AI + Data Community Tour Slides - Canarias
Salesforce AI + Data Community Tour Slides - Canarias
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
 
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
一比一原版(heriotwatt学位证书)英国赫瑞瓦特大学毕业证如何办理
 

topic11LinkedLists.ppt

  • 1. 1 Topic 11 Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101, a data structures course, and when they hit the pointers business their brains would just totally explode, and the next thing you knew, they were majoring in Political Science because law school seemed like a better idea." -Joel Spolsky Thanks to Don Slater of CMU for use of his slides.
  • 2. Clicker 1 What is output by the following code? ArrayList<Integer> a1 = new ArrayList<>(); ArrayList<Integer> a2 = new ArrayList<>(); a1.add(12); a2.add(12); System.out.println(a1 == a2); A. false B. true C. No output due to syntax error D. No output due to runtime error E. Varies from one run of the program to the next CS314 Linked Lists 2
  • 3. CS314 Linked Lists 3 Dynamic Data Structures Dynamic data structures – They grow and shrink one element at a time, normally without some of the inefficiencies of arrays – as opposed to a static container such as an array Big O of Array Manipulations – Access the kth element – Add or delete an element in the middle of the array while maintaining relative order – adding element at the end of array? space avail? no space avail? – add element at beginning of an array
  • 4. Linked Lists 4 Object References Recall that an object reference is a variable that stores the address of an object A reference can also be called a pointer They are often depicted graphically: student John Smith 40725 3.57 CS314
  • 5. Linked Lists 5 References as Links Object references can be used to create links between objects Suppose a Student class contained a reference to another Student object John Smith 40725 3.57 Jane Jones 58821 3.72 CS314
  • 6. Linked Lists 6 References as Links References can be used to create a variety of linked structures, such as a linked list: studentList CS314
  • 7. CS314 Linked Lists 7 Linked Lists A linear collection of self-referential objects, called nodes, connected by other links – linear: for every node in the list, there is one and only one node that precedes it (except for possibly the first node, which may have no predecessor,) and there is one and only one node that succeeds it, (except for possibly the last node, which may have no successor) – self-referential: a node that has the ability to refer to another node of the same type, or even to refer to itself – node: contains data of any type, including a reference to another node of the same data type, or to nodes of different data types – Usually a list will have a beginning and an end; the first element in the list is accessed by a reference to that class, and the last node in the list will have a reference that is set to null
  • 8. CS314 Linked Lists 8 Linked lists are dynamic, they can grow or shrink as necessary Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory Advantages of linked lists
  • 9. CS314 Linked Lists 9 Nodes and Lists A different way of implementing a list Each element of a Linked List is a separate Node object. Each Node tracks a single piece of data plus a reference (pointer) to the next Create a new Node very time we add something to the List Remove nodes when item removed from list and allow garbage collector to reclaim that memory
  • 10. CS314 Linked Lists 10 A Node Class public class Node<E> { private E myData; private Node<E> myNext; public Node() { myData = null; myNext = null; } public Node(E data, Node<E> next) { myData = data; myNext = next; } public E getData() { return myData; } public Node<E> getNext() { return myNext; } public void setData(E data) { myData = data; } public void setNext(Node<E> next) { myNext = next; } }
  • 11. CS314 Linked Lists 11 One Implementation of a Linked List The Nodes show on the previous slide are singly linked – a node refers only to the next node in the structure – it is also possible to have doubly linked nodes. – The node has a reference to the next node in the structure and the previous node in the structure as well How is the end of the list indicated – myNext = null for last node – a separate dummy node class / object
  • 12. CS314 Linked Lists 12 A Linked List Implementation public class LinkedList<E> implements IList<E> private Node<E> head; private Node<E> tail; private int size; public LinkedList(){ head = null; tail = null; size = 0; } } LinkedList<String> list = new LinkedList<String>(); LinkedList myHead iMySize myTail null null 0
  • 13. CS314 Linked Lists 13 Writing Methods When trying to code methods for Linked Lists draw pictures! – If you don't draw pictures of what you are trying to do it is very easy to make mistakes!
  • 14. CS314 Linked Lists 14 add method add to the end of list special case if empty steps on following slides public void add(E obj)
  • 15. CS314 Linked Lists 15 Add Element - List Empty (Before) head tail size null null 0 Object item
  • 16. CS314 Linked Lists 16 Add Element - List Empty (After) head tail size 1 String Node myData myNext null
  • 17. CS314 Linked Lists 17 Add Element - List Not Empty (Before) 1 String Node myData myNext null head tail size String item
  • 18. CS314 Linked Lists 18 Add Element - List Not Empty (After) 2 String Node myData myNext head tail size String Node myData myNext null
  • 19. CS314 Linked Lists 19 Code for default add public void add(E obj)
  • 20. Clicker 2 What is the worst case Big O for adding to the end of an array based list and our LinkedList314 class? The lists already contain N items. Array based Linked A. O(1) O(1) B. O(N) O(N) C. O(logN) O(1) D. O(1) O(N) E. O(N) O(1) 20
  • 21. Contains method Implement a contains method for our Linked List class public boolean contains(E val) // val != null CS314 Linked Lists 21
  • 22. CS314 Linked Lists 22 Code for addFront add to front of list public void addFront(E obj) How does this compare to adding at the front of an array based list?
  • 23. Clicker 3 What is the Big O for adding to the front of an array based list and a linked list? The lists already contain N items. Array based Linked A. O(1) O(1) B. O(N) O(1) C. O(logN) O(1) D. O(1) O(N) E. O(N) O(N) CS314 Linked Lists 23
  • 24. CS314 Linked Lists 24 Code for Insert public void insert(int pos, E obj) Must be careful not to break the chain! Where do we need to go? Special cases?
  • 25. Clicker 4 What is the Big O for inserting an element into the middle of an array based list and into the middle of a linked list? Each list already contains N items. Array based Linked A. O(1) O(1) B. O(1) O(N) C. O(N) O(1) D. O(N) O(N) E. O(N) O(logN) CS314 Linked Lists 25
  • 26. Clicker Question 5 What is the Big O for getting an element based on position from an array based list and from a linked list? Each list contains N items. In other words E get(int pos) Array based Linked A. O(1) O(1) B. O(1) O(N) C. O(N) O(1) D. O(logN) O(N) E. O(N) O(N) Linked Lists 26
  • 27. CS314 Linked Lists 27 Code for get public E get(int pos) The downside of Linked Lists
  • 28. CS314 Linked Lists 28 Code for remove public E remove(int pos)
  • 29. Clicker 6 What is the order to remove the last element of a singly linked list with references to the first and last nodes of the linked structure of nodes? The list contains N elements A. O(1) B. O(logN) C. O(N^0.5) D. O(N) E. O(NlogN)) CS314 Linked Lists 29
  • 30. CS314 Linked Lists 30 Why Use Linked List What operations with a Linked List faster than the version from ArrayList?
  • 31. CS314 Linked Lists 31 Clicker 7 - Getting All Elements in Order From a Linked List What is the Order (Big O) of the following code? LinkedList314<Integer> list; list = new LinkedList314<Integer>(); // code to fill list with N elements int total = 0; //Big O of following code? for(int i = 0; i < list.size(); i++) total += list.get(i); A. O(N) B. O(2N) C. O(NlogN) D. O(N2) E. O(N3)
  • 32. Iterators to the Rescue CS314 Linked Lists 32
  • 33. CS314 Linked Lists 33 Other Possible Features of Linked Lists Doubly Linked Circular Dummy Nodes for first and last node in list public class DLNode<E> { private E myData; private DLNode<E> myNext; private DLNode<E> myPrevious; }
  • 34. CS314 Linked Lists 34 Dummy Nodes Use of Dummy Nodes for a Doubly Linked List removes most special cases Also could make the Double Linked List circular
  • 35. CS314 Linked Lists 35 Doubly Linked List add public void add(E obj)
  • 36. CS314 Linked Lists 36 Insert for Doubly Linked List public void insert(int pos, E obj)