All language subtitles for 6. Singly Linked Lists (Implementation)

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:00,211 --> 00:00:05,211 (lively music) (keyboard clacking) 2 2 00:00:05,890 --> 00:00:10,270 So, let's implement a simple singly linked class. 3 3 00:00:10,270 --> 00:00:12,070 I've created a project, 4 4 00:00:12,070 --> 00:00:13,690 I'm putting the code into package 5 5 00:00:13,690 --> 00:00:16,220 academy.learnprogramming.linkedlists. 6 6 00:00:16,220 --> 00:00:19,280 I've created our four employees that we used 7 7 00:00:19,280 --> 00:00:20,750 in the array list video. 8 8 00:00:20,750 --> 00:00:24,050 This time I've assigned them to employee variables 9 9 00:00:24,050 --> 00:00:26,960 and I've also added the employee class 10 10 00:00:26,960 --> 00:00:29,830 and all I did was copied and pasted the class 11 11 00:00:29,830 --> 00:00:32,200 over from the array list project, 12 12 00:00:32,200 --> 00:00:33,630 so it's a straightforward class. 13 13 00:00:33,630 --> 00:00:34,620 We have three fields 14 14 00:00:34,620 --> 00:00:37,660 and we just have the usual boilerplate code. 15 15 00:00:37,660 --> 00:00:38,990 We have our equals method 16 16 00:00:38,990 --> 00:00:41,630 and we have our override toString. 17 17 00:00:41,630 --> 00:00:43,330 So, the first class we'll work on 18 18 00:00:43,330 --> 00:00:45,870 is the node class and this class 19 19 00:00:45,870 --> 00:00:48,720 is going to need two variables, 20 20 00:00:48,720 --> 00:00:50,840 one for the employee instance 21 21 00:00:50,840 --> 00:00:53,660 and one for the next node in the list 22 22 00:00:53,660 --> 00:00:56,360 because remember, when we're working with a linked list, 23 23 00:00:56,360 --> 00:00:59,550 every node knows about the next node in the list 24 24 00:00:59,550 --> 00:01:02,100 and so, we need a field that contains a reference 25 25 00:01:02,100 --> 00:01:03,970 to the next node in the list 26 26 00:01:03,970 --> 00:01:06,670 and that's why the data structure is called a linked list 27 27 00:01:06,670 --> 00:01:09,290 if you hadn't already figured that out. 28 28 00:01:09,290 --> 00:01:11,230 In Java, by contains a link, 29 29 00:01:11,230 --> 00:01:13,410 we mean that it stores the object reference 30 30 00:01:13,410 --> 00:01:15,030 of the next node. 31 31 00:01:15,030 --> 00:01:17,070 So, let's add a node class, 32 32 00:01:17,070 --> 00:01:20,060 so I'll say New, Java Class. 33 33 00:01:20,060 --> 00:01:22,650 And I'm going to call this EmployeeNode 34 34 00:01:22,650 --> 00:01:24,410 rather than just plain old Node 35 35 00:01:24,410 --> 00:01:26,980 because I'm not going to use generics 36 36 00:01:26,980 --> 00:01:28,870 to write this linked list, 37 37 00:01:28,870 --> 00:01:30,400 I want us to just focus 38 38 00:01:30,400 --> 00:01:32,640 on the linked list implementation 39 39 00:01:32,640 --> 00:01:34,590 and on top of that you're not going to write 40 40 00:01:34,590 --> 00:01:36,470 your own linked list, you'll end up using the one 41 41 00:01:36,470 --> 00:01:39,800 in the JDK and even if you did write a linked link class 42 42 00:01:39,800 --> 00:01:41,290 for your application, 43 43 00:01:41,290 --> 00:01:43,420 you'd probably make it specific 44 44 00:01:43,420 --> 00:01:44,980 to the type of data you're dealing with. 45 45 00:01:44,980 --> 00:01:47,320 The only reason that you would need to use generics 46 46 00:01:47,320 --> 00:01:49,140 is if you're going to write a class 47 47 00:01:49,140 --> 00:01:51,000 that's going to be released publicly 48 48 00:01:51,000 --> 00:01:53,270 and so that many, many applications 49 49 00:01:53,270 --> 00:01:54,690 are going to use it 50 50 00:01:54,690 --> 00:01:56,780 and in that case, you'd wanna use generics 51 51 00:01:56,780 --> 00:01:59,000 'cause you'd want it to be usable 52 52 00:01:59,000 --> 00:02:01,850 with a variety of object types. 53 53 00:02:01,850 --> 00:02:04,040 But for our purposes, we're just gonna focus 54 54 00:02:04,040 --> 00:02:06,320 on the implementation of the linked list 55 55 00:02:06,320 --> 00:02:07,670 and so, I'm not gonna use generics 56 56 00:02:07,670 --> 00:02:09,660 and so, I'm calling this EmployeeNode 57 57 00:02:09,660 --> 00:02:11,640 to make it clear that this node 58 58 00:02:11,640 --> 00:02:13,993 will only work with employee instances. 59 59 00:02:15,550 --> 00:02:17,550 So, as I said, we're gonna need two fields, 60 60 00:02:17,550 --> 00:02:19,400 we're gonna need one for the employee 61 61 00:02:22,830 --> 00:02:24,640 and we're going to need a field 62 62 00:02:24,640 --> 00:02:29,150 that stores a reference to the next node in the list. 63 63 00:02:29,150 --> 00:02:30,963 So, we'll call that next. 64 64 00:02:32,680 --> 00:02:36,330 So, for the constructor, we just need the employee, 65 65 00:02:36,330 --> 00:02:38,553 so I'll say public EmployeeNode 66 66 00:02:40,310 --> 00:02:42,350 and we just need the employee for that. 67 67 00:02:44,520 --> 00:02:49,520 And so, we'll just say this.employee equals employee. 68 68 00:02:51,460 --> 00:02:52,910 And to make things quick and easy, 69 69 00:02:52,910 --> 00:02:54,940 I'll have the IDE just create me a bunch 70 70 00:02:54,940 --> 00:02:58,213 of sets and gets, so I'll say Generate, 71 71 00:02:59,240 --> 00:03:03,160 Getter and Setter, I want it for both fields, 72 72 00:03:03,160 --> 00:03:04,960 click OK and there we go. 73 73 00:03:04,960 --> 00:03:06,993 We have our sets and gets. 74 74 00:03:08,100 --> 00:03:10,600 So, the constructor only takes employee 75 75 00:03:10,600 --> 00:03:12,610 because when we construct an instance, 76 76 00:03:12,610 --> 00:03:15,840 we don't know yet what the next node will be. 77 77 00:03:15,840 --> 00:03:17,890 We'll add that later, you'll see when we implement 78 78 00:03:17,890 --> 00:03:20,470 the insert method, we create the node first 79 79 00:03:20,470 --> 00:03:23,360 and then we figure out which node 80 80 00:03:23,360 --> 00:03:25,560 is supposed to be assigned to it next field. 81 81 00:03:25,560 --> 00:03:27,130 Now, as we saw in the slides, 82 82 00:03:27,130 --> 00:03:29,630 if a node is the last node in the list, 83 83 00:03:29,630 --> 00:03:32,070 meaning that there isn't any node following it, 84 84 00:03:32,070 --> 00:03:34,420 then its next field will be set to null 85 85 00:03:34,420 --> 00:03:36,000 and so, when we traverse the list, 86 86 00:03:36,000 --> 00:03:37,910 that's how we'll know we've reached the end. 87 87 00:03:37,910 --> 00:03:39,920 We don't have to set next to null 88 88 00:03:39,920 --> 00:03:42,120 in the constructor because that's the default value 89 89 00:03:42,120 --> 00:03:43,920 for object fields. 90 90 00:03:43,920 --> 00:03:46,250 So, we have a class for the node, 91 91 00:03:46,250 --> 00:03:48,150 what about the linked list itself? 92 92 00:03:48,150 --> 00:03:49,910 Well, as we saw from the slides, 93 93 00:03:49,910 --> 00:03:51,930 all we need to know for the linked list 94 94 00:03:51,930 --> 00:03:54,300 is the head node, that's it. 95 95 00:03:54,300 --> 00:03:56,560 From there we can traverse the entire list 96 96 00:03:56,560 --> 00:03:59,250 because every node in the list contains a link 97 97 00:03:59,250 --> 00:04:00,280 to the next node 98 98 00:04:00,280 --> 00:04:03,763 and so, let's create a class for a linked list. 99 99 00:04:04,940 --> 00:04:09,940 And I'm gonna call this EmployeeLinkedList once again 100 100 00:04:10,170 --> 00:04:14,310 to make it clear that this only works with employee nodes. 101 101 00:04:14,310 --> 00:04:16,110 And this just needs one field. 102 102 00:04:16,110 --> 00:04:18,560 It needs a field for the head node, 103 103 00:04:18,560 --> 00:04:20,523 so private EmployeeNode 104 104 00:04:23,480 --> 00:04:25,610 head, that's it. 105 105 00:04:25,610 --> 00:04:29,510 So, let's say we wanna add an item to the linked list. 106 106 00:04:29,510 --> 00:04:32,490 For the best performance, we should add items 107 107 00:04:32,490 --> 00:04:35,080 to the beginning, so we don't have to traverse the list 108 108 00:04:35,080 --> 00:04:36,880 looking for an insertion point 109 109 00:04:36,880 --> 00:04:40,550 and so, we're going to code an addToFront method. 110 110 00:04:40,550 --> 00:04:44,047 So, we'll say public void addToFront 111 111 00:04:45,868 --> 00:04:48,468 and we need the employee instance that we wanna add, 112 112 00:04:50,610 --> 00:04:55,610 so we'll say EmployeeNode node equals new EmployeeNode 113 113 00:04:55,830 --> 00:04:57,920 and we just have to pass the employee 114 114 00:04:58,910 --> 00:05:02,720 and then we need to set this new node's next field. 115 115 00:05:02,720 --> 00:05:05,240 Its new node's next field is going to point 116 116 00:05:05,240 --> 00:05:07,460 at whatever head it's currently pointing at 117 117 00:05:07,460 --> 00:05:11,430 because when we add a new node to the front of the list, 118 118 00:05:11,430 --> 00:05:13,370 the current head of the list 119 119 00:05:13,370 --> 00:05:17,290 is now going to become the second node in the list 120 120 00:05:17,290 --> 00:05:18,830 and so, this new node 121 121 00:05:18,830 --> 00:05:20,950 is going to point to the current head 122 122 00:05:20,950 --> 00:05:22,430 as we saw in the slides. 123 123 00:05:22,430 --> 00:05:26,113 So, we'll say node.setNext head. 124 124 00:05:27,290 --> 00:05:29,240 And then of course the last thing we wanna do 125 125 00:05:29,240 --> 00:05:32,810 is set head to the new node. 126 126 00:05:32,810 --> 00:05:34,710 So, if you're having trouble understanding this, 127 127 00:05:34,710 --> 00:05:35,950 go back to the slides 128 128 00:05:35,950 --> 00:05:38,100 but essentially, we're inserting the node right 129 129 00:05:38,100 --> 00:05:38,950 at the front of the list, 130 130 00:05:38,950 --> 00:05:42,760 so let's say our list contains employee Jane 131 131 00:05:42,760 --> 00:05:45,190 and we're inserting John. 132 132 00:05:45,190 --> 00:05:46,830 When we come in, the head field 133 133 00:05:46,830 --> 00:05:48,140 will be pointing to Jane. 134 134 00:05:48,140 --> 00:05:50,210 When we've finished inserting John, 135 135 00:05:50,210 --> 00:05:52,290 we want John to be pointing to Jane 136 136 00:05:52,290 --> 00:05:54,840 and the head field to be pointing to John. 137 137 00:05:54,840 --> 00:05:56,970 So, first we create the new node 138 138 00:05:56,970 --> 00:05:59,690 and then we set John which is the new node, 139 139 00:05:59,690 --> 00:06:01,790 John's next field to Jane 140 140 00:06:01,790 --> 00:06:03,400 because that's currently the one 141 141 00:06:03,400 --> 00:06:05,260 being pointed to by head 142 142 00:06:05,260 --> 00:06:07,080 and then we set head to John 143 143 00:06:07,080 --> 00:06:09,190 and so, we end up with a head field 144 144 00:06:09,190 --> 00:06:11,930 that points to John and John's next field points 145 145 00:06:11,930 --> 00:06:14,208 to Jane, I'll just delete this blank line here 146 146 00:06:14,208 --> 00:06:17,180 and now let's go back to our main method 147 147 00:06:17,180 --> 00:06:18,930 and let's create a linked list 148 148 00:06:18,930 --> 00:06:21,147 and add some employees to it. 149 149 00:06:21,147 --> 00:06:24,300 So, we'll say EmployeeLinkedList 150 150 00:06:24,300 --> 00:06:28,483 and I'll just call it list equals new EmployeeLinkedList 151 151 00:06:30,690 --> 00:06:32,390 and then we'll say list.addToFront 152 152 00:06:33,609 --> 00:06:35,609 and we'll add janeJones, 153 153 00:06:37,352 --> 00:06:39,352 list.addToFront johnDoe, 154 154 00:06:41,242 --> 00:06:43,325 list.addTofront marySmith 155 155 00:06:44,180 --> 00:06:48,463 and list.addToFront mikeWilson. 156 156 00:06:49,470 --> 00:06:51,330 And we're gonna need a way to print our list, 157 157 00:06:51,330 --> 00:06:53,410 so let's go back to our LinkedList class 158 158 00:06:53,410 --> 00:06:55,203 and add a printList method. 159 159 00:06:56,090 --> 00:06:59,493 So, we'll say public void printList 160 160 00:07:01,980 --> 00:07:05,620 and we'll say EmployeeNode current 161 161 00:07:05,620 --> 00:07:07,060 equals the head of the list 162 162 00:07:07,060 --> 00:07:08,660 'cause we're gonna start at the head 163 163 00:07:08,660 --> 00:07:11,070 and I'm gonna print out something here 164 164 00:07:11,070 --> 00:07:13,340 that says HEAD 165 165 00:07:17,050 --> 00:07:19,570 and then we wanna keep traversing the list 166 166 00:07:19,570 --> 00:07:21,585 until current hits null, 167 167 00:07:21,585 --> 00:07:24,240 so while current is not equal to null 168 168 00:07:24,240 --> 00:07:27,440 because when it hits null, we've hit the end of the list, 169 169 00:07:27,440 --> 00:07:32,010 we'll say System.out.print current 170 170 00:07:32,010 --> 00:07:33,950 and I'm gonna change this to print as well 171 171 00:07:33,950 --> 00:07:38,083 because I don't want that to be printed on its own line. 172 172 00:07:41,110 --> 00:07:42,770 And then I'll print an arrow 173 173 00:07:46,080 --> 00:07:48,490 and then we want to move to the next node, 174 174 00:07:48,490 --> 00:07:50,933 so current equals current.getNext. 175 175 00:07:52,220 --> 00:07:55,390 This isn't perfect, we're gonna end up with an arrow after 176 176 00:07:55,390 --> 00:07:59,230 but I can say System.out.print line null 177 177 00:07:59,230 --> 00:08:03,896 and so, the last item in the list will be null. 178 178 00:08:03,896 --> 00:08:05,050 This will work fine 179 179 00:08:05,050 --> 00:08:07,930 but what we'll see right now if we were to call this 180 180 00:08:07,930 --> 00:08:09,610 is a bunch of object references 181 181 00:08:09,610 --> 00:08:12,120 because we've overridden employee, 182 182 00:08:12,120 --> 00:08:14,940 we've overridden the toString method and employee 183 183 00:08:14,940 --> 00:08:18,030 but we haven't overridden it in our node class. 184 184 00:08:18,030 --> 00:08:19,623 So, let's do that now. 185 185 00:08:21,160 --> 00:08:23,070 And what I actually wanna print out 186 186 00:08:23,070 --> 00:08:26,870 is the employee information when we print a node, 187 187 00:08:26,870 --> 00:08:30,250 so I'll just call the employees toString method, 188 188 00:08:30,250 --> 00:08:32,700 so I'll say public String toString 189 189 00:08:34,060 --> 00:08:38,720 and I want to return employee.toString 190 190 00:08:38,720 --> 00:08:40,150 and so, when we print a node, 191 191 00:08:40,150 --> 00:08:41,590 what will actually be printing 192 192 00:08:41,590 --> 00:08:44,963 is the result of this toString method. 193 193 00:08:45,830 --> 00:08:50,830 So, let's call our printList method now and let's run. 194 194 00:08:55,140 --> 00:08:56,850 So, we have the head of our list 195 195 00:08:56,850 --> 00:09:00,500 and then you'll see that Mike Wilson comes first. 196 196 00:09:00,500 --> 00:09:02,440 We added Jane first 197 197 00:09:02,440 --> 00:09:05,180 but we're constantly adding new employees 198 198 00:09:05,180 --> 00:09:06,190 to the front of the list, 199 199 00:09:06,190 --> 00:09:08,620 so every time we add a new employee, 200 200 00:09:08,620 --> 00:09:11,450 the existing employees gets bumped down the list. 201 201 00:09:11,450 --> 00:09:14,200 So, we added Mike last, so he's gonna be the head 202 202 00:09:14,200 --> 00:09:17,130 of the list 'cause we're always adding to the front, 203 203 00:09:17,130 --> 00:09:19,280 followed by Mary Smith 204 204 00:09:20,500 --> 00:09:23,190 followed by John Doe 205 205 00:09:23,190 --> 00:09:27,170 and followed by Jane Jones and she'll be pointing to null 206 206 00:09:27,170 --> 00:09:30,100 because she's the last node in the list 207 207 00:09:30,100 --> 00:09:32,900 and so, that's all there is to inserting a node 208 208 00:09:32,900 --> 00:09:35,660 into the linked list at the front of the list. 209 209 00:09:35,660 --> 00:09:36,910 We could write a method 210 210 00:09:36,910 --> 00:09:39,160 that adds employees to the end of the list 211 211 00:09:39,160 --> 00:09:42,530 or that looks very specific employee in the list 212 212 00:09:42,530 --> 00:09:45,480 and adds an employee after that employee 213 213 00:09:45,480 --> 00:09:47,050 or before the employee 214 214 00:09:47,050 --> 00:09:50,960 but those methods will be on the order of O of n, 215 215 00:09:50,960 --> 00:09:53,960 they'll be linear because then we have to traverse the list 216 216 00:09:53,960 --> 00:09:55,330 and the worst case will always be 217 217 00:09:55,330 --> 00:09:57,950 that we have to traverse the entire list. 218 218 00:09:57,950 --> 00:09:59,810 That would be true if we wanted to add a node 219 219 00:09:59,810 --> 00:10:00,643 to the end. 220 220 00:10:00,643 --> 00:10:02,670 Now, some implementations of linked list 221 221 00:10:02,670 --> 00:10:05,806 will have a pointer to the tail of the list, 222 222 00:10:05,806 --> 00:10:07,600 the last node in the list 223 223 00:10:07,600 --> 00:10:11,250 but that's not really a true singly linked list, 224 224 00:10:11,250 --> 00:10:12,610 it's a variation on them 225 225 00:10:12,610 --> 00:10:13,770 but you could do that 226 226 00:10:13,770 --> 00:10:16,380 if you thought you were constantly gonna be wanting 227 227 00:10:16,380 --> 00:10:18,060 to add items to the end of the list, 228 228 00:10:18,060 --> 00:10:22,210 you could keep a reference to the last node in the list 229 229 00:10:22,210 --> 00:10:23,400 which is called the tail 230 230 00:10:23,400 --> 00:10:25,850 but for a linked list implementation 231 231 00:10:25,850 --> 00:10:28,420 that's only keeping reference to the head, 232 232 00:10:28,420 --> 00:10:30,120 if you wanted to insert items at the end, 233 233 00:10:30,120 --> 00:10:32,050 you'd have to traverse the entire list 234 234 00:10:32,050 --> 00:10:35,720 and in either way whether you kept a reference 235 235 00:10:35,720 --> 00:10:36,880 to the tail or not, 236 236 00:10:36,880 --> 00:10:38,440 if you wanted to insert items 237 237 00:10:38,440 --> 00:10:40,530 at a specific point in the list, 238 238 00:10:40,530 --> 00:10:42,697 like before or after a specific employee, 239 239 00:10:42,697 --> 00:10:44,410 you gonna have to traverse the list looking 240 240 00:10:44,410 --> 00:10:47,870 for that employee and so, a singly linked list 241 241 00:10:47,870 --> 00:10:51,630 is best used when you want to insert and remove items 242 242 00:10:51,630 --> 00:10:53,280 from the front of the list. 243 243 00:10:53,280 --> 00:10:55,600 Now, the other things to note is is that a linked list 244 244 00:10:55,600 --> 00:10:57,070 can continue to grow 245 245 00:10:57,070 --> 00:10:59,070 without having to be resized. 246 246 00:10:59,070 --> 00:11:01,720 Remember, with arrays, once the array is full, 247 247 00:11:01,720 --> 00:11:03,200 if we wanna add more items to it, 248 248 00:11:03,200 --> 00:11:04,890 we have to resize the array 249 249 00:11:04,890 --> 00:11:06,330 but that's not true of a linked list. 250 250 00:11:06,330 --> 00:11:08,420 With a linked list, you're pretty much only bounded 251 251 00:11:08,420 --> 00:11:09,930 by the memory you have. 252 252 00:11:09,930 --> 00:11:11,410 Now, talking about memory, 253 253 00:11:11,410 --> 00:11:13,120 one disadvantage to a linked list 254 254 00:11:13,120 --> 00:11:16,700 is you have to store that extra field with every value. 255 255 00:11:16,700 --> 00:11:18,260 You don't have to do that with arrays, 256 256 00:11:18,260 --> 00:11:20,340 so is memory's really tight, 257 257 00:11:20,340 --> 00:11:22,810 that could be one disadvantage to using a linked list 258 258 00:11:22,810 --> 00:11:25,480 even if you will only wanna be adding 259 259 00:11:25,480 --> 00:11:27,290 and deleting items from the front 260 260 00:11:27,290 --> 00:11:30,260 if memory is tight and you're gonna have a lot of items, 261 261 00:11:30,260 --> 00:11:32,440 maybe a linked list wouldn't be your best choice. 262 262 00:11:32,440 --> 00:11:34,990 So, as usual, it's going to depend on your application, 263 263 00:11:34,990 --> 00:11:36,410 the platform you're running on, 264 264 00:11:36,410 --> 00:11:38,020 what the application's gonna wanna do 265 265 00:11:38,020 --> 00:11:39,120 with the data, et cetera. 266 266 00:11:39,120 --> 00:11:42,260 There isn't a one-size-fits-all answer. 267 267 00:11:42,260 --> 00:11:44,240 So, let's say that we wanted to know how many items 268 268 00:11:44,240 --> 00:11:45,490 are in the linked list. 269 269 00:11:45,490 --> 00:11:47,330 We could traverse the list 270 270 00:11:47,330 --> 00:11:49,010 and count how many items there are 271 271 00:11:49,010 --> 00:11:50,390 but another way to do it 272 272 00:11:50,390 --> 00:11:52,170 would be to just keep a running count 273 273 00:11:52,170 --> 00:11:53,910 of how many nodes are in the list 274 274 00:11:53,910 --> 00:11:56,340 and we're gonna do it that way. 275 275 00:11:56,340 --> 00:11:58,430 So, let's go back to our LinkedList class 276 276 00:11:58,430 --> 00:12:00,920 and we're gonna add a size field, 277 277 00:12:00,920 --> 00:12:03,620 so we'll say private int size 278 278 00:12:03,620 --> 00:12:06,170 and that will be initialised to zero by default 279 279 00:12:06,170 --> 00:12:07,410 when the list is created 280 280 00:12:07,410 --> 00:12:09,610 and then whenever we add an employee of course, 281 281 00:12:09,610 --> 00:12:12,070 we want to increment the size 282 282 00:12:12,070 --> 00:12:14,360 and it would be nice for us to have a get, 283 283 00:12:14,360 --> 00:12:16,280 I'll just type this out 'cause it's quick, 284 284 00:12:16,280 --> 00:12:21,280 so public int getSize and we just return the size. 285 285 00:12:21,880 --> 00:12:23,540 Let's go back to our main method 286 286 00:12:23,540 --> 00:12:25,500 and call our getSize method 287 287 00:12:25,500 --> 00:12:27,180 after we've added these employees, 288 288 00:12:27,180 --> 00:12:32,180 so we'll say System.out.print line list.getSize 289 289 00:12:32,480 --> 00:12:34,363 and we should get four. 290 290 00:12:35,270 --> 00:12:36,173 Let's run. 291 291 00:12:38,520 --> 00:12:42,510 And sure enough we get four employees in our list. 292 292 00:12:42,510 --> 00:12:45,180 So, we could now add an isEmpty method 293 293 00:12:45,180 --> 00:12:47,650 that tells us whether the linked list is empty. 294 294 00:12:47,650 --> 00:12:49,870 We could just call the getSize method 295 295 00:12:49,870 --> 00:12:52,190 and test whether the number of items 296 296 00:12:52,190 --> 00:12:54,560 in the list is zero but there's another way 297 297 00:12:54,560 --> 00:12:56,090 to test whether a list is empty. 298 298 00:12:56,090 --> 00:12:58,260 Can you think about what that way is 299 299 00:12:58,260 --> 00:13:01,230 if we looked at the linked list implementation for a minute? 300 300 00:13:01,230 --> 00:13:03,380 Can you come up with another way, 301 301 00:13:03,380 --> 00:13:06,920 a quick way of testing whether a linked list is empty? 302 302 00:13:06,920 --> 00:13:10,420 If head is null, that means the list is empty, right? 303 303 00:13:10,420 --> 00:13:11,330 'Cause head is null. 304 304 00:13:11,330 --> 00:13:13,030 It's not pointing to any nodes. 305 305 00:13:13,030 --> 00:13:15,700 So, one way we could write an isEmpty method 306 306 00:13:15,700 --> 00:13:18,207 is to say public boolean isEmpty 307 307 00:13:20,850 --> 00:13:25,350 and we return head equals null 308 308 00:13:26,310 --> 00:13:28,080 and so, if we go back to main now, 309 309 00:13:28,080 --> 00:13:30,415 and we call this right at the top, 310 310 00:13:30,415 --> 00:13:34,620 before we add anything and we say System.out.print line 311 311 00:13:34,620 --> 00:13:38,300 list.isEmpty we should get true here. 312 312 00:13:38,300 --> 00:13:39,133 Let's run. 313 313 00:13:40,880 --> 00:13:42,780 And sure enough we get true. 314 314 00:13:42,780 --> 00:13:46,370 Our list is empty before we've added anything to it. 315 315 00:13:46,370 --> 00:13:48,020 So, the last method we'll look at 316 316 00:13:48,020 --> 00:13:50,050 is how do we remove items from the front? 317 317 00:13:50,050 --> 00:13:51,870 And we saw that in the slides. 318 318 00:13:51,870 --> 00:13:53,540 Let's go to our LinkedList class 319 319 00:13:53,540 --> 00:13:55,050 and let's write the method, 320 320 00:13:55,050 --> 00:13:58,450 so we'll say public and we're gonna return the node 321 321 00:13:58,450 --> 00:14:01,070 that we removed in case the caller wants 322 322 00:14:01,070 --> 00:14:02,070 to do anything with it 323 323 00:14:02,070 --> 00:14:03,947 and we'll same removeFromFront. 324 324 00:14:05,870 --> 00:14:07,410 We don't need to pass anything 325 325 00:14:07,410 --> 00:14:09,550 'cause we're always gonna remove the node 326 326 00:14:09,550 --> 00:14:10,910 that's right at the front. 327 327 00:14:10,910 --> 00:14:12,050 So, the first thing we do 328 328 00:14:12,050 --> 00:14:14,130 is we need to test if the list is empty 329 329 00:14:14,130 --> 00:14:15,730 because if the list is empty, 330 330 00:14:15,730 --> 00:14:17,010 there's nothing to remove, 331 331 00:14:17,010 --> 00:14:19,060 so we'll say if isEmpty, 332 332 00:14:20,990 --> 00:14:22,263 we'll just return null. 333 333 00:14:23,150 --> 00:14:24,663 We don't have anything to do. 334 334 00:14:26,070 --> 00:14:28,080 If the list isn't empty, 335 335 00:14:28,080 --> 00:14:32,600 what we're gonna do is store the first node 336 336 00:14:32,600 --> 00:14:35,220 on the list, I'll call this removeNode, 337 337 00:14:35,220 --> 00:14:37,780 that's what I called it in the slides, 338 338 00:14:37,780 --> 00:14:40,070 is the head node, right? 339 339 00:14:40,070 --> 00:14:41,830 So, the node that we're gonna remove 340 340 00:14:41,830 --> 00:14:44,260 is pointed to by the head node, 341 341 00:14:44,260 --> 00:14:46,860 that's the object reference that's stored in the head field. 342 342 00:14:46,860 --> 00:14:49,177 So, we'll assign that to removeNode 343 343 00:14:50,120 --> 00:14:52,790 and then we want to move the head. 344 344 00:14:52,790 --> 00:14:55,490 The head will equal head.getNext. 345 345 00:14:55,490 --> 00:14:57,770 So, if we have the situation where we're pointing 346 346 00:14:57,770 --> 00:15:00,610 at Mike, head is pointing to Mike 347 347 00:15:00,610 --> 00:15:02,720 and we want Mike to be removed, 348 348 00:15:02,720 --> 00:15:04,870 we want the head field to point 349 349 00:15:04,870 --> 00:15:07,900 to whatever Mike's next field is pointing to 350 350 00:15:07,900 --> 00:15:09,990 because right now that's the second item in the list 351 351 00:15:09,990 --> 00:15:11,910 and that's going to become the front of the list 352 352 00:15:11,910 --> 00:15:14,330 when Mike is removed, so that's all we're doing here. 353 353 00:15:14,330 --> 00:15:16,460 Of course we have to decrement the size 354 354 00:15:16,460 --> 00:15:19,070 because now we'll have one less item 355 355 00:15:19,070 --> 00:15:22,353 and finally we return the removedNode. 356 356 00:15:23,490 --> 00:15:25,680 Now, if we wanted to be really diligent, 357 357 00:15:25,680 --> 00:15:27,530 we could say removedNode.setNext null 358 358 00:15:31,180 --> 00:15:33,850 and that would completely remove Mike 359 359 00:15:33,850 --> 00:15:35,570 or whoever we're moving from the list 360 360 00:15:35,570 --> 00:15:38,190 and that's gonna be an isolated node again 361 361 00:15:38,190 --> 00:15:39,590 but we don't really have to do that 362 362 00:15:39,590 --> 00:15:42,460 because the fact that the next field is still set 363 363 00:15:42,460 --> 00:15:44,250 to a node in the list doesn't mean 364 364 00:15:44,250 --> 00:15:46,480 that we can reach the node we're removing 365 365 00:15:46,480 --> 00:15:49,180 because the head field now points 366 366 00:15:49,180 --> 00:15:51,620 to the node after the node we're removing 367 367 00:15:51,620 --> 00:15:54,470 but we could do that just to be really clean 368 368 00:15:54,470 --> 00:15:57,500 and make sure that we're cleaning up all of the references. 369 369 00:15:57,500 --> 00:15:59,420 So, let's go to the main method now 370 370 00:15:59,420 --> 00:16:02,080 and we'll remove the item at the front of the list. 371 371 00:16:02,080 --> 00:16:06,550 So, after we've added our four employees 372 372 00:16:06,550 --> 00:16:09,620 and printed them, we'll say list.removeFromFront 373 373 00:16:12,600 --> 00:16:15,030 and let's print the size again, 374 374 00:16:15,030 --> 00:16:17,950 System.out.print line list.getSize 375 375 00:16:19,082 --> 00:16:21,090 just to make sure we're decrementing the size correctly 376 376 00:16:21,090 --> 00:16:22,980 and then we'll print our list again. 377 377 00:16:22,980 --> 00:16:24,223 And so, let's run. 378 378 00:16:28,030 --> 00:16:30,780 Here's our list after we've add four employees 379 379 00:16:30,780 --> 00:16:33,030 and we'll see that Mike 380 380 00:16:33,030 --> 00:16:35,060 is the first item on the list 381 381 00:16:35,060 --> 00:16:37,500 and then after we've called removeFromFront, 382 382 00:16:37,500 --> 00:16:39,370 our size goes down to three 383 383 00:16:39,370 --> 00:16:41,470 and we'll see that Mary 384 384 00:16:41,470 --> 00:16:44,169 is now the first employee on the list. 385 385 00:16:44,169 --> 00:16:47,950 And if we go to the end, we'll see that our list 386 386 00:16:47,950 --> 00:16:50,051 is one employee shorter. 387 387 00:16:50,051 --> 00:16:52,930 So, once again, whether to use a linked list, 388 388 00:16:52,930 --> 00:16:54,720 an array or a list will depend 389 389 00:16:54,720 --> 00:16:57,080 on what your application wants to do. 390 390 00:16:57,080 --> 00:17:00,050 If it wants to do a bunch of random accesses, 391 391 00:17:00,050 --> 00:17:02,490 then a linked list would be a bad choice 392 392 00:17:02,490 --> 00:17:04,740 'cause you're always gonna have to be traversing the list 393 393 00:17:04,740 --> 00:17:07,170 to get to whatever you want to access 394 394 00:17:07,170 --> 00:17:08,840 but if you want to load a bunch of data 395 395 00:17:08,840 --> 00:17:11,720 into the list and you're always gonna be most interested 396 396 00:17:11,720 --> 00:17:13,670 in whatever's at the front of the linked list, 397 397 00:17:13,670 --> 00:17:16,000 then that could be a really good choice, 398 398 00:17:16,000 --> 00:17:17,810 a linked list could be a good choice 399 399 00:17:17,810 --> 00:17:19,920 for data structure depending 400 400 00:17:19,920 --> 00:17:21,050 on what else your application 401 401 00:17:21,050 --> 00:17:22,710 is going to wanna do with the data. 402 402 00:17:22,710 --> 00:17:24,840 So, this is a simple implementation 403 403 00:17:24,840 --> 00:17:26,970 of a singly linked list 404 404 00:17:26,970 --> 00:17:30,760 just to give you an idea of what would be going on 405 405 00:17:30,760 --> 00:17:33,330 under the covers in a linked list implementation. 406 406 00:17:33,330 --> 00:17:35,150 In the next video, we'll take a look 407 407 00:17:35,150 --> 00:17:36,700 at doubly linked lists. 408 408 00:17:36,700 --> 00:17:37,663 I'll see you there. 34912

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.