module 9: list structures - cs.wustl.educytron/cse131/slides/9.pdf · 9.0 introduction • recall...
Post on 16-Oct-2020
2 Views
Preview:
TRANSCRIPT
Module 9: List Structures
Copyright Ron K. Cytron 2013
Ron K. Cytron *
Prepared for 2u
Semester Online
Department of Computer Science and Engineering Washington University in Saint Louis
Thanks to Alan Waldman for comments that improved these slides
*
9.0 Introduction
• Recall methods – One method can call another – A method can even call itself – this is recursion
• Recall objects – One object can reference (point to) other objects – An object can even reference itself, a form of
recursion – More commonly, an object references another object
of the same time • These are also called recursive data structures
– We study such objects here • Lists, Sets, Maps
– Are built out of such recursive objects
2
Monolog 0
End of Monologue
9.1 List Nodes
• Objects can reference other objects
3
Oyster 1
day time
Appointment
9.1 List Nodes
• Objects can reference other objects
– Which may contain primitive values
4
hr!
3!
min!
45!
isPM!
true!
Time
day time
Appointment
9.1 List Nodes
• Objects can reference other objects
– Which may contain primitive values
– Or references to other objects
5
hr!
3!
min!
45!
isPM!
true!
Time month! day!
19!
yr!2013!
Date
day time
Appointment
9.1 List Nodes
• Objects can reference other objects
– Which may contain primitive values
– Or references to other objects
– Which may reference other objects as well
6
hr!
3!
min!
45!
isPM!
true!
Time month! day!
19!
yr!2013!
Date
String
chars!
day time
Appointment
9.1 List Nodes
• Objects can reference other objects
– Which may contain primitive values
– Or references to other objects
– Which may reference other objects as well
7
hr!
3!
min!
45!
isPM!
true!
Time month! day!
19!
yr!2013!
Date
String
chars!
char[]
'N'! 'o'! 'v'!
day time
Appointment
9.1 List Nodes
• These structures always reach some fixed size
8
hr!
3!
min!
45!
isPM!
true!
Time month! day!
19!
yr!2013!
Date
String
chars!
char[]
'N'! 'o'! 'v'!
day time
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless
9
hr!
3!
min!
45!
isPM!
true!
Time
day time
Appointment
month! day!
19!
yr!2013!
Date
String
chars!
char[]
'N'! 'o'! 'v'!
9.1 List Nodes
• These structures always reach some fixed size
– Unless
10
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen
11
Appointment
prev
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen
12
Appointment
prev
prev
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen
13
Appointment
prev
prev
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen • Which can go on
14
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen • Which can go on
15
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• These structures always reach some fixed size
– Unless • We have a pointer to a
type previously seen • Which can go on • Ends in null
16
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• There could be any number of previous appointments
17
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• There could be any number of previous appointments
• This data structure can grow arbitrarily large
18
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• Trick – An Appointment can
reference an Appointment
19
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• Trick – An Appointment can
reference an Appointment – Recursive data type
20
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• Trick – An Appointment can
reference an Appointment – Recursive data type – Base case is null!
21
Appointment
prev
prev
prev
Appointment
Appointment
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Value of interest in the list
22
ListNode!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Could be an int!
23
ListNode!
131!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Could be a boolean!
24
ListNode!
true!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Could be a String!
25
ListNode!
String!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Could be or any other kind of reference!
26
ListNode!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Let's assume it's an int, named value!
27
ListNode!
131!
value!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Let's assume it's an int, named value!– reference to the next node in the list
28
ListNode!
131!
value!
9.1 List Nodes
• Generic form of this idea – ListNode!
• has-a – payload
» Let's assume it's an int, named value!– reference to the next node in the list
» Let's assume it's named next!
29
ListNode!
131!
ListNode!
240!
value! value!next! next!
9.1 List Nodes
• Generic form of this idea – ListNode!
• to save space we will stack the contents vertically:!
30
ListNode!
131!
ListNode!
240!
value! value!next! next!
value!
next!
value!
next!
9.1 List Nodes
• Generic form of this idea – ListNode!
• to save space we will stack the contents vertically:!
31
ListNode!
240!
value!next!131!
value!
next!
value!
next!
9.1 List Nodes
• Generic form of this idea – ListNode!
• to save space we will stack the contents vertically:!
32
131! 240!
End of Oyster
value!
next!
value!
next!
9.2 Roundtable
• What is the difference between – A person (has-a finger) – A finger pointing to a person – A ListNode object – A ListNode reference
• The only way to make an object – new ListNode(….)!– creates what we show as a circle on these slides – has-a
• int value • ListNode next
• The only way to declare a reference – ListNode ref;!
33
Roundtable 2
9.2 Roundtable
• How many objects do you see below? • How many references?
34
132!
240! 241!131!
9.2 Roundtable
• A reference – is a red box – either points at an object (a circle)
35
132!
240! 241!131!
9.2 Roundtable
• A reference – is a red box – either points at an object (a circle)
36
132!
240! 241!131!
9.2 Roundtable
• A reference – is a red box – either points at an object (a circle) – or is null
37
132!
240! 241!131!
9.2 Roundtable
• A reference – is a red box – it never points to another red box !
38
132!
240! 241!131!No!
9.2 Roundtable
• How many objects do you see below? • How many references?
39
332!132!
240! 241!131!
9.2 Roundtable
• How many objects do you see below?
40
332!132!
240! 241!131!
1
9.2 Roundtable
• How many objects do you see below?
41
332!132!
240! 241!131!
1 2
9.2 Roundtable
• How many objects do you see below?
42
332!132!
240! 241!131!
1 2 3
9.2 Roundtable
• How many objects do you see below?
43
332!132!
240! 241!131!
1 2 3
4
9.2 Roundtable
• How many objects do you see below?
44
332!132!
240! 241!131!
1 2 3
4 5
9.2 Roundtable
• How many objects do you see below? – 5 objects, each is a ListNode!
45
332!132!
240! 241!131!
1 2 3
4 5
9.2 Roundtable
• How many ListNode references do you see?
46
332!132!
240! 241!131!
9.2 Roundtable
• How many ListNode references do you see? – 10 red boxes – each is a ListNode reference
47
332!132!
240! 241!131!
End of Roundtable
9.3 Some ListNode gymnastics
48
Oyster 3
• We will implement List<T> using ListNode!– But first, some exercises using ListNode!
• We will use either – iteration – recursion
• Our methods will be either – static, so they accept an ListNode parameter – nonstatic, so they operate on this ListNode!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! ! !
}!
49
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
50
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
51
• These instance variables are missing the private declaration
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
52
• These instance variables are missing the private declaration • The fields can be set or examined by any class
in the same package as ListNode!
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
53
• These instance variables are missing the private declaration • The fields can be set or examined by any class
in the same package as ListNode!• Avoids the need for getters and setters
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
54
• These instance variables are missing the private declaration • The fields can be set or examined by any class
in the same package as ListNode!• Avoids the need for getters and setters • Makes the resulting code less cluttered!
value!
next!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
55
• Given • ListNode<Integer> p = new ListNode<Integer>();!
0!
null!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
56
• Given • ListNode<Integer> p = new ListNode<Integer>();!
0!
null!
p!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
57
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
0!
null!
0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
58
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value =!
0!
null!
0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
59
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45;!
45!
null!
0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
60
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null
45!
null!
0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
61
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; !
45!
null!
0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
62
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; !
45! 0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
63
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; !
45! 0!
null!
p! q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
64
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; // q could be null here!
45!
p!null!
q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
65
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; // q could be null here!
45!
null!
p!null!
q!
9.3 Some ListNode gymnastics
• Declaration of ListNode!
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!}!
66
• Given • ListNode<Integer> p = new ListNode<Integer>();!• ListNode<Integer> q = new ListNode<Integer>();!
• We can say • p.value = 45; // p cannot be null • p.next = q; // q could be null here!
45!
p! q!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
67
131 132 240 241 this!
this!
System.out.println(!
);!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
68
131 132 240 241 this!
this!
System.out.println(!
);!
The results of this depend on the implementation of
toString() within ListNode!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
69
131 132 240 241 this!
this!
System.out.println(!
);!
The results of this depend on the implementation of
toString() within ListNode!
"A ListNode"!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
70
131 132 240 241 this!
this!
System.out.println(!
);!
The results of this depend on the implementation of
toString() within ListNode!
"A ListNode with 131"!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
71
131 132 240 241 this!
this!
System.out.println(!
);!
The results of this depend on the implementation of
toString() within ListNode!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
72
131 132 240 241 this!
this.value!
System.out.println(!
);!
131!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
73
131 132 240 241 this!
this.next!
System.out.println(!
);!
"A ListNode with 132"!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
74
131 132 240 241 this!
this.next.value!
System.out.println(!
);!
132!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
75
131 132 240 241 this!
this.next.next.next.value!
System.out.println(!
);!
241!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
76
131 132 240 241 this!
this.next.next.next.next!
System.out.println(!
);!
null!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
77
131 132 240 241 this!
Now let's try changing the values and structure of the list!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
78
131 132 240 241 this!
this.value = 100;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
79
131 132 240 241 this!
this.value = 100;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
80
131 132 240 241 this!
this.value = 100;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
81
100 132 240 241 this!
this.value = 100;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
82
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
83
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
84
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
85
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
86
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
87
100 132 240 241 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
88
100 132 240 101 this!
this.next.next.next.value = 101;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
89
100 132 240 101 this!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
90
100 132 240 101 this!
this.next =!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
91
100 132 240 101 this!
this.next =!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
92
100 132 240 101 this!
this.next = this.next.next;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
93
100 132 240 101 this!
this.next = this.next.next;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
94
100 132 240 101 this!
this.next = this.next.next;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
95
100 132 240 101 this!
this.next = this.next.next;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
96
100 132 240 101 this!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
97
100 132 240 101 this!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
98
100 132 240 101 this!
The storage associated with this unreachable node would eventually be recycled by the
garbage collector!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
99
131 132 240 241 this!
Returning to the list in its original form …!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
100
131 132 240 241 this!
this = null;!
9.3 Some ListNode gymnastics
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
}!
101
131 132 240 241 this!
this = null;!
We are not allowed to change this. It is inherent to an
object and cannot be modified.!
XXXXXXXXXXXXX
End of Oyster
9.4 Roundtable
• Interaction of quiz on the following diagram – What is the value of …. – Show me where …. is stored – Each time turning to online student first
102
131 132 240 241 this!
Roundtable 4
End of Roundtable
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
103
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• No use of static here • Method has access to this as
the first ListNode in the list
Oyster 5a
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
104
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
For this problem, we will use recursion!
• No use of static here • Method has access to this as
the first ListNode in the list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
105
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
Recursion will provide the length
of this smaller list
For this problem, we will use recursion!
• No use of static here • Method has access to this as
the first ListNode in the list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
106
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• No use of static here • Method has access to this as
the first ListNode in the list • this.next references the
smaller list
Recursion will provide the length
of this smaller list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
107
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• No use of static here • Method has access to this as
the first ListNode in the list • this.next references the
smaller list • this.next.size() will
compute its size
Recursion will provide the length
of this smaller list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
108
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• Given the length of the smaller list • this list is just one
bigger
Recursion will provide the length
of this smaller list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
109
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• Given the length of the smaller list • this list is just one
bigger
Recursion will provide the length
of this smaller list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
110
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
• Given the length of the smaller list • this list is just one
bigger
Recursion will provide the length
of this smaller list
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
111
this!
Compute the length of a list beginning at this ListNode!
• Base case • Only one node
241
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
112
this!
Compute the length of a list beginning at this ListNode!
• Base case • Only one node
241
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
113
Compute the length of a list beginning at this ListNode!
• Why 1 and not 0? • Because this can never be
null
this! 241
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
114
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
We next compute this.size() for the list below!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
115
this!
Compute the length of a list beginning at this ListNode!
1 + 1 + 1 + 1!
131 132 240 241
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
116
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
1 + 1 + 1 + 1!
this!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
117
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
1 + 1 + 1 + 1!
this!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
118
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
1 + 1 + 1 + 1!
this!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
119
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
1 + 1 + 2!
this!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
120
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
1 + 3!
this!
9.5 ListNode methods
public class ListNode<T> {!! !T value;!! !ListNode<T> next;!
!public int size() {!! !if (this.next == null) {!! ! !return 1;!! !else!! ! !return 1 + this.next.size();!!}!
}!
121
131 132 240 241 this!
Compute the length of a list beginning at this ListNode!
4!End of Oyster
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
122
131 132 240 241 head!
Sum the elements starting at head!
Oyster 5b
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
123
131 132 240 241 head!
Sum the elements starting at head!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
124
131 132 240 241 head!
Sum the elements starting at head!
For this problem, we will use iteration
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
125
131 132 240 241 head!
For this problem, we will use iteration and a static method
Sum the elements starting at head!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
126
131 132 240 241 head!
With no this, the method requires the first node as a parameter
Sum the elements starting at head!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListItem<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
127
131 132 240 241 head!
Sum the elements starting at head!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
128
131 132 240 241 head!
Sum the elements starting at head!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
129
131 132 240 241 head!
Sum the elements starting at head!
p!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
130
131 132 240 241 head!
Sum the elements starting at head!
p!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
131
131 132 240 241 head!
Sum the elements starting at head!
p!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
132
131 132 240 241 head!
Sum the elements starting at head!
p!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !p = p + value;!!}!!return ans;!
}!
133
131 132 240 241 head!
Sum the elements starting at head!
p!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
134
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 0!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
135
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 131!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
136
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 131!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
137
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 263!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
138
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 263!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
139
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 503!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
140
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 503!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
141
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 744!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
142
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 744!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
143
131 132 240 241 head!
Sum the elements starting at head!
p!
ans! 744!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
144
131 132 240 241 head!
Sum the elements starting at head!
ans! 744!
9.5 ListNode methods
public static int sum(ListNode<Integer> head) {!!int ans = 0;!!for (ListNode<Integer> p = head; p != null; p=p.next) {!! !ans = ans + p.value;!!}!!return ans;!
}!
145
131 132 240 241 head!
Sum the elements starting at head!
Thus, sum(head) returns 744!
End of Oyster
9.6 Roundtable
• We will complete the following method – size(), static and recursively
• Different base case!
146
Roundtable 6
End of Roundtable
9.7 Exercises
• Write each of the following methods – size(), static and iteratively – size(), static and recursively
• Write the following method – sum(), static but recursively
• Use a "helper" method if necessary – Why can't we write a nonstatic version of the sum()
method?
147
BLT 7a,b,c,d
End of BLT
9.8 A LinkedList implementation of List!
• Consider a List ADT: – T is a type parameter
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
148
Oyster 8a
9.8 A LinkedList implementation of List!
• Consider a List ADT: – T is a type parameter
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
149
We can use T in here to declare the type of the values contained in the list
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
150
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
151
• Add the item x of type T to the beginning of the list.
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public void remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
152
• Add the item x of type T to the end of the list.
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
153
• Find and remove the first occurrence of x in the list, returning true if successful
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
154
• Return the ith element in the list • First element is at index 0 • Return null if no such element exists
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
155
• Return the size of the list • The number of elements in the list!
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
156
• Is the list empty? • Can use the principle of reduction
9.8 A LinkedList implementation of List!
• Consider a List ADT:
public interface List<T> {!!public void addFirst(T x);!!public void addLast(T x);!!public boolean remove(T x);!!public T get(int i);!!public int size();!!public boolean isEmpty();!}!
157
• Is the list empty? • Can use the principle of reduction • Same as asking if size() == 0!
9.8 A LinkedList implementation of List
• We next implement the List<T> interface – A SinglyLinkedList<T> implementation
• Two kinds of objects involved – ListNode<T> has-a
• T value!• ListNode<T> next!
158
value!
next!
9.8 A LinkedList implementation of List
• We next implement the List<T> interface – A SinglyLinkedList<T> implementation
• Two kinds of objects involved – ListNode<T> has-a
• T value!• ListNode<T> next!• We will use many of these per list
159
value!
next!
9.8 A LinkedList implementation of List
• We next implement the List<T> interface – A SinglyLinkedList<T> implementation
• Two kinds of objects involved – ListNode<T> has-a
• T value!• ListNode<T> next!• We will use many of these per list
– SinglyLinkedList<T> has-a • ListNode<T> head!
160
value!
next!
head!
9.8 A LinkedList implementation of List
• We next implement the List<T> interface – A SinglyLinkedList<T> implementation
• Two kinds of objects involved – ListNode<T> has-a
• T value!• ListNode<T> next!• We will use many of these per list
– SinglyLinkedList<T> has-a • ListNode<T> head!• Only one of these used per list
161
value!
next!
head!
9.8 A LinkedList implementation of List
• To simply things – Let's assume the parametric type T is Integer!– Our implementation will work for any type T!
162
131 132 240 241 head!
9.8 A LinkedList implementation of List
• In our singly-linked list implementation – Each ListNode points to the next element in the list
163
131 132 240 241 head!
9.8 A LinkedList implementation of List
• In our singly-linked list implementation – Each ListNode points to the next element in the list – The head pointer in SinglyLinkedList points to
the beginning of the list
164
131 132 240 241 head!
9.8 A LinkedList implementation of List
• Note – The SinglyLinkedList object implements the
methods of interest
165
131 132 240 241 head!
9.8 A LinkedList implementation of List
• Note – The SinglyLinkedList object implements the
methods of interest
166
131 132 240 241 head!
addFirst(…)!
9.8 A LinkedList implementation of List
• Note – The SinglyLinkedList object implements the
methods of interest
167
131 132 240 241 head!
size()!
9.8 A LinkedList implementation of List
• Note – The SinglyLinkedList object implements the
methods of interest • It does its work by following and processing the ListNode objects
168
131 132 240 241 head!
9.8 A LinkedList implementation of List
• Note – The SinglyLinkedList object implements the
methods of interest – The ListNode objects are just containers
• They offer no special methods at all
169
131 132 240 241 head!
9.8 A LinkedList implementation of List
• We will use the following program and implement methods as needed
170
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
9.8 A LinkedList implementation of List
• Construction
171
head!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!
9.8 A LinkedList implementation of List
• Construction • The only instance variable is head!
– Set to null!– Represents the empty list
172
head!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!
End of Oyster
9.8 A LinkedList implementation of List
173
head!
• Method addFirst(Integer i) – Special case: adding the element to an empty list
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
Oyster 8b
9.8 A LinkedList implementation of List
174
0 head!
• Method addFirst(Integer i) – Special case: adding the element to an empty list
– Create a new ListNode (referenced by p)!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
p!
9.8 A LinkedList implementation of List
175
240 head!
• Method addFirst(Integer i) – Special case: adding the element to an empty list
– Create a new ListNode (referenced by p)!– Set its value to i (its next is null)
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
p!
9.8 A LinkedList implementation of List
176
240 head!
• Method addFirst(Integer i) – Special case: adding the element to an empty list
– Create a new ListNode (referenced by p)!– Set its value to i (its next is null) – Set head to reference p's ListNode!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
p!
9.8 A LinkedList implementation of List
177
240 head!
• Method addFirst(Integer i) – Result shown below
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
9.8 A LinkedList implementation of List
178
240 head!
• Method addFirst(Integer i) – The list is no longer empty!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!
9.8 A LinkedList implementation of List
179
240 head!
• Method addFirst(Integer i) – The list is no longer empty!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!
9.8 A LinkedList implementation of List
180
240 head!
132
• Method addFirst(Integer i) – Adding to a non-empty list
– Create a new ListNode and set its value to 132!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!
p!
9.8 A LinkedList implementation of List
181
240 head!
132
• Method addFirst(Integer i) – Adding to a non-empty list
– Create a new ListNode and set its value to 132 – Its next references head's ListNode!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!
p!
9.8 A LinkedList implementation of List
182
240 head!
132
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!
• Method addFirst(Integer i) – Adding to a non-empty list
– Create a new ListNode and set its value to 132 – Its next references head's ListNode!– head references p's ListNode!
p!
9.8 A LinkedList implementation of List
183
240 head!
132
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!
• Method addFirst(Integer i) – Result shown below
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
184
240 head!
132
• Method addFirst(Integer i) – Result shown below
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
185
240 head!
132
• Method addFirst(Integer i) – Same process as last time
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
186
240 head!
132
• Method addFirst(Integer i) – Same process as last time
131
p!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
187
240 head!
132
• Method addFirst(Integer i) – Same process as last time
131
p!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
188
240 head!
132
• Method addFirst(Integer i) – Same process as last time
131
p!
End of Oyster
9.8 A LinkedList implementation of List
189
240 head!
132 131
• Method addFirst(Integer i) – Result shown below
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!
9.8 A LinkedList implementation of List
190
240 head!
132 131
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – This could happen to an empty list
Oyster 8c
9.8 A LinkedList implementation of List
191
240 head!
132 131
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – This could happen to an empty list – Note: adding to the beginning or end of an empty list
produces the same result – Use the principle of reduction
9.8 A LinkedList implementation of List
192
240 head!
132 131
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – This could happen to an empty list – If so, call addFirst(i)!
9.8 A LinkedList implementation of List
193
240 head!
132 131
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise!
9.8 A LinkedList implementation of List
194
240 head!
132 131 0
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode (call it q)
q!
9.8 A LinkedList implementation of List
195
240 head!
132 131 241
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!
q!
9.8 A LinkedList implementation of List
196
240 head!
132 131 241
q!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– We now search for the end of the list
9.8 A LinkedList implementation of List
197
240 head!
132 131 241
p! q!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– We now search for the end of the list (using p)
9.8 A LinkedList implementation of List
198
240 head!
132 131 241
p! q!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– We now search for the end of the list (using p)
9.8 A LinkedList implementation of List
199
240 head!
132 131 241
p! q!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– We now search for the end of the list (using p)
9.8 A LinkedList implementation of List
200
240 head!
132 131 241
p! q!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– When p references the end
9.8 A LinkedList implementation of List
201
240 head!
132 131 241
p!
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Otherwise
– Make the new ListNode and set its value!– When p references the end – Set p.next to q!
q!
9.8 A LinkedList implementation of List
202
240 head!
132 131 241
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();!list.addFirst(240);!list.addFirst(132);!list.addFirst(131);!list.addLast(241);!
• Method addLast(Integer i) – Result shown below!
End of Oyster
9.8 A LinkedList implementation of List
203
240 head!
132 131 241
• Method remove(Integer i) – Finds and removes the first node that has i as its value!
list.remove(132);!
Oyster 8d
9.8 A LinkedList implementation of List
204
• Method remove(Integer i) – Special case: empty list—nothing to do!
list.remove(132);!
head!
9.8 A LinkedList implementation of List
205
• Method remove(Integer i) – Another special case: item is found first in the list!
list.remove(132);!
240 head!
132 241
9.8 A LinkedList implementation of List
206
• Method remove(Integer i) – Another special case: item is found first in the list – Remove by setting head = head.next!
list.remove(132);!
240 head!
132 241
9.8 A LinkedList implementation of List
207
• Method remove(Integer i) – Another special case: item is found first in the list – Remove by setting head = head.next!
list.remove(132);!
240 head!
132 241
9.8 A LinkedList implementation of List
208
• Method remove(Integer i) – Another special case: item is found first in the list – Remove by setting head = head.next!– The deleted node is no longer part of this list
list.remove(132);!
240 head!
132 241
9.8 A LinkedList implementation of List
209
• Method remove(Integer i) – Another special case: item is found first in the list – Remove by setting head = head.next!– The deleted node is no longer part of this list
list.remove(132);!
240 head!
241
9.8 A LinkedList implementation of List
210
240 head!
132 131 241
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list
list.remove(132);!
9.8 A LinkedList implementation of List
211
240 head!
132 131 241
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list – Suppose the item is found and referenced by p
list.remove(132);!
9.8 A LinkedList implementation of List
212
240 head!
132 131 241
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list – Suppose the item is found and referenced by p
list.remove(132);!
p!
9.8 A LinkedList implementation of List
213
240 head!
132 131 241
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list – Suppose the item is found and referenced by p – We have no reference to the previous node
list.remove(132);!
p!
9.8 A LinkedList implementation of List
214
240 head!
132 131 241
p!
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list – Trick: look at the node just to the right of p!
9.8 A LinkedList implementation of List
215
240 head!
132 131 241
p!
• Method remove(Integer i) – Otherwise we have to look for the item in the middle
of the list – Trick: look at the node just to the right of p!
9.8 A LinkedList implementation of List
• Recall our List ADT: • And recall our singly-linked list implementation • The method remove(int)!
– Technique: look at value one to the right of p!
216
240 head!
132 131 241
p!
9.8 A LinkedList implementation of List
• Recall our List ADT: • And recall our singly-linked list implementation • The method remove(int)!
– Technique: look at value one to the right of p!
217
240 head!
132 131 241
p!
9.8 A LinkedList implementation of List
218
240 head!
132 131 241
p!
• Method remove(Integer i) – Trick: look at the node just to the right of p!– When found, set p.next = p.next.next!
list.remove(132);!
9.8 A LinkedList implementation of List
219
240 head!
132 131 241
p!
• Method remove(Integer i) – Trick: look at the node just to the right of p!– When found, set p.next = p.next.next!
list.remove(132);!
9.8 A LinkedList implementation of List
220
240 head!
131 241
• Method remove(Integer i) – Result is shown below!
list.remove(132);!
End of Oyster
Roundtable
• Develop the SinglyLinkedList implementation – addFirst, addLast, remove!– If time allows: get, size, and isEmpty!
221
Roundtable 9a
End of Roundtable
Roundtable
• How is our code changed by adding a sentinel to the list? – A dummy node at the start of the list – It's there even just after the list is constructed – It never goes away
• Modify the methods to take account of this – And see how things get simpler – Each method – turn to student: how would you do
this? – addFirst, addLast, remove!– If time allows: get, size, and isEmpty!
222
Roundtable 9b
End of Roundtable
top related