Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
(energetic music) 2
2
(keyboard clicks) 3
3
Alright, this shouldn't have been too difficult. 4
4
I included this challenge 5
5
because I wanted you to write the code 6
6
to wire up the next and previous fields 7
7
when you're adding a node into the list. 8
8
So I'm in the employee doubly link list class 9
9
and I'm gonna go down to the add before method. 10
10
Now there was a comment here 11
11
saying return true if you are able 12
12
to successfully add the employee into the list 13
13
before the existing employee, 14
14
and return false if the existing employee 15
15
doesn't exist in the list. 16
16
Because obviously if the existing employee 17
17
doesn't exist, then you don't know where to add 18
18
the new employee, so you can't do anything. 19
19
Okay, so I'm gonna remove this comment here, 20
20
and let's implement our method. 21
21
So the first thing we're gonna do is check 22
22
whether the list is empty. 23
23
Because if it's empty, then we just need to return false, 24
24
because obviously the employee that we want to add 25
25
the new employee in front of doesn't exist in the list, 26
26
so we can't do anything. 27
27
So I'm just gonna say, if head equals null, return false. 28
28
I could also use if is empty here. 29
29
I'm just gonna say if head equals null, 30
30
but you could also have said if is empty. 31
31
Okay, so if it's not empty, then we have at least 32
32
one employee in the list, and so the first thing 33
33
we need to do is find the existing employee. 34
34
So I'll say find the existing employee. 35
35
So I'll say employee node current equals head, 36
36
cause we're gonna start looking at the head of the list. 37
37
And then while current is not equal to null, 38
38
and current isn't the node that we're looking for, 39
39
the employee that we're looking for, 40
40
so current dot get employee dot equals 41
41
the existing employee. 42
42
So while current isn't null, 43
43
and while current isn't equal to the existing employee, 44
44
we're gonna move to the next node. 45
45
Current equals current dot get next. 46
46
Now when we drop out of this loop, 47
47
two things are possible, either current is null, 48
48
meaning that the existing employee isn't in the list, 49
49
so the employee doesn't actually exist in the list, 50
50
or we have found the employee that was passed in 51
51
as the existing employee. 52
52
So we'll handle the if current equals null case first, 53
53
because if current equals null, 54
54
we just want to return false. 55
55
Cause obviously, if the existing employee, 56
56
the one that we want to insert in front of 57
57
isn't in the list, we can't do anything. 58
58
Alright, so if we get down to this point, 59
59
we know that we have found 60
60
the existing employee in the list. 61
61
So the employee exists in the list 62
62
and so we want to insert the new employee 63
63
in front of the existing employee. 64
64
So what we'll have to do, if we're gonna plop 65
65
this new employee in front of an existing employee, 66
66
we're gonna have to change four fields. 67
67
We're gonna have to obviously set the previous and next 68
68
fields of the new employee. 69
69
We're gonna have to change the previous field 70
70
of the current node, because we're inserting 71
71
the new employee in front of the current node, 72
72
so we're gonna want the current node's previous field 73
73
to point to the new employee, 74
74
and then the employee that's currently in front of current 75
75
is now going to be in front of the new employee, 76
76
so its next field needs to be changed. 77
77
So lets start doing that. 78
78
So we'll start by setting the new node's 79
79
next and previous fields. 80
80
So the first we'll do is create a node 81
81
for the employee, so we'll say employee node. 82
82
New node, equals new employee node, 83
83
and that's for the new employee. 84
84
And then we'll set the new node's next and previous fields, 85
85
so we'll say new node dot set previous, 86
86
and this wants to point to whatever the current node's 87
87
previous field is pointing to. 88
88
Let's say we have Mary Smith and John Doe. 89
89
So Mary Smith is in front of John Doe, 90
90
and then we come along and we want to insert Bill End 91
91
in between Mary Smith and John Doe. 92
92
So right now John Doe's previous field 93
93
is pointing to Mary Smith. 94
94
That's what we want Bill End's previous field 95
95
to point to once he's inserted. 96
96
And so we want to say new node dot set previous to 97
97
whatever the current node is currently pointing to 98
98
in its previous field. 99
99
And then because we're inserting the new employee 100
100
in front of the existing employee, 101
101
we want the new employee's next field 102
102
to point to the existing employee. 103
103
So we're going to say new node dot set next, current. 104
104
And that's taken care of setting the new node's 105
105
previous and next fields. 106
106
Now we also need to set the previous field 107
107
of the current node, because in the example I gave 108
108
John Doe is no longer going to be pointing to Mary Smith 109
109
as previous, he wants to point to Bill End. 110
110
So we're gonna say current dot set previous, 111
111
and he wants to point to the new node. 112
112
And then finally we need to take care 113
113
of Mary Smith's next field, 114
114
but we need to be careful here. 115
115
Because it's possible that John Doe, 116
116
if we use him as an example of the existing employee, 117
117
let's suppose that the employee that we want to insert 118
118
before, that employee might be at the head of the list. 119
119
And so we have to handle that special case. 120
120
Let's say we say we want to insert Bill End 121
121
before Mike Wilson, and Mike is at the head of the list, 122
122
we don't have to set the next field 123
123
of the node in front of Mike, 124
124
because there is no next field. 125
125
But what we would have to do is change the head field 126
126
to the new employee. 127
127
Because if Mike is at the head of the list, 128
128
and we're inserting a new employee before Mike, 129
129
then the new employee becomes the head of the list. 130
130
So we're gonna say, if head equals current, 131
131
then what we wanna do is set head to the new node. 132
132
We don't have to handle the next field 133
133
in whatever was in front of Mike, 134
134
because nothing was in front of Mike. 135
135
But if Mike isn't the head, then we do have to handle 136
136
the next field of the node that was in front 137
137
of the current node, and so we'll say, 138
138
new node dot get previous dot set next, 139
139
and we want to set it to the new node. 140
140
So up here we've already set the new node's previous field 141
141
to whatever was in front of the employee 142
142
that we're inserting in front of. 143
143
So in the case of inserting Bill End 144
144
between Mary Smith and John Doe, 145
145
we already set the new node's previous field to Mary Smith. 146
146
So new node dot get previous will be Mary Smith, 147
147
and we want to set her next field to the new node, 148
148
because instead of pointing to John Doe, 149
149
she's now gonna want to point to Bill End. 150
150
And so that's what we have to do 151
151
to wire up the new node properly 152
152
and to change the next field of the node 153
153
that's now in front of the new node, 154
154
and the previous field of the existing employee. 155
155
But we're not quite finished yet, 156
156
we have to increment the size. 157
157
And we also want change this return false, to return true. 158
158
Because if we make it down here, 159
159
we were able to successfully insert the new employee 160
160
into the list in front of the existing employee. 161
161
So let's go over to the main method, 162
162
and here, this is the code 163
163
that was in the starter project, 164
164
so we're adding five employees, 165
165
we create our list, 166
166
we add Jane, John, Mary, and Mike to the list. 167
167
And then we want to add Bill before John Doe. 168
168
So let's run this to make sure it works. 169
169
Here's the list before we add Bill End, 170
170
and we can see that it contains Mike, Mary, John, and Jane. 171
171
And then here's the list after we've added Bill End, 172
172
and we've called the add before method, 173
173
so we want to add him before John Doe, 174
174
and we see Mike, Mary, Bill, and then John Doe. 175
175
And finally Jane Jones. 176
176
So that works. 177
177
Let's add an employee before Mike Wilson, 178
178
to make sure that we're handling this special case 179
179
of the existing employee being the head of the list. 180
180
So we'll say list dot add before, 181
181
and I'll just make a new employee up on the fly. 182
182
(keyboard clicks) 183
183
I'll say Someone Else, and we'll give them an ID of 1111, 184
184
and we want to have them in front of Mike Wilson, 185
185
because he's the front of the list. 186
186
And then we'll say list dot print list. 187
187
So let's do that. 188
188
And we'll see that Someone Else 189
189
has been added at the front of the list. 190
190
And we still have all the rest of our entries in the list. 191
191
So that's it for this challenge, 192
192
I think maybe perhaps this special case 193
193
might have tripped a couple of you up at first, 194
194
but otherwise it's pretty standard stuff. 195
195
And as I said, I included this challenge 196
196
just to have you the opportunity to go through 197
197
having to wire up a node in a doubly linked list. 198
198
So that's it for challenge number one, 199
199
let's move on to challenge number two.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.