1
00:00:01,030 --> 00:00:11,350
OK, so in today's video, I'm going to go over the delete function and that was in the coding exercise

2
00:00:11,350 --> 00:00:12,760
is a challenge for you all.

3
00:00:13,540 --> 00:00:15,880
This was might have been a little bit difficult.

4
00:00:17,020 --> 00:00:24,280
If you think a lot about the insert function, you're kind of able to infer how you might do this delete

5
00:00:24,280 --> 00:00:32,290
function because it's a little bit similar, but sometimes it's hard to think about and figure it out

6
00:00:32,290 --> 00:00:36,670
without getting, you know, a decent amount of errors if you got through it.

7
00:00:37,270 --> 00:00:37,780
Definitely.

8
00:00:37,780 --> 00:00:39,610
Congratulations to you.

9
00:00:39,610 --> 00:00:41,260
Otherwise, don't worry about it.

10
00:00:41,270 --> 00:00:47,980
I'm going to go ahead and go over it with some visuals and of course, the code as well right now.

11
00:00:48,640 --> 00:00:49,900
So let's go ahead and get started.

12
00:00:49,900 --> 00:00:54,060
I went in, went ahead and made a new project from sources.

13
00:00:54,070 --> 00:01:00,970
So just starting off from our insert function that was here and I'm going to go ahead and just add the

14
00:01:00,970 --> 00:01:03,520
delete function in between that one and print here.

15
00:01:04,060 --> 00:01:09,040
First, I'm going to go over to my header file and just put the delete function here.

16
00:01:09,040 --> 00:01:10,630
I'm actually just going to call it Del.

17
00:01:11,290 --> 00:01:13,230
And we're going to have the same thing.

18
00:01:13,240 --> 00:01:15,190
That can be a data value pass to it.

19
00:01:18,100 --> 00:01:19,640
So go back to the TPP.

20
00:01:22,020 --> 00:01:28,120
OK, so let's go ahead and copy this template so you can put that there.

21
00:01:29,110 --> 00:01:34,390
It's going to be void list T and it's going to be.

22
00:01:36,370 --> 00:01:36,850
OK.

23
00:01:36,910 --> 00:01:44,920
So similar to our insert function and a pen, I'm going to start off with a pointer right away, but

24
00:01:44,920 --> 00:01:48,700
it's actually just going to be to start at the head.

25
00:01:48,700 --> 00:01:55,180
And it's kind of going to be like similar to occur where we were using that as like an iterator, kind

26
00:01:55,180 --> 00:01:55,410
of.

27
00:01:55,420 --> 00:02:00,480
So I'm going to just call this del node.

28
00:02:00,490 --> 00:02:06,400
So it's the node that we want to delete from a set of equal to the head just right off the bat.

29
00:02:07,060 --> 00:02:08,110
So we start with that.

30
00:02:09,190 --> 00:02:14,680
And first off, I'm going to do the situation in which the list is empty, which we don't really have

31
00:02:14,680 --> 00:02:15,850
to do anything.

32
00:02:15,850 --> 00:02:23,800
So we're just going to say like that if the list is empty, we're just going to do your check to make

33
00:02:23,800 --> 00:02:27,340
sure that we have size is equal to zero.

34
00:02:27,880 --> 00:02:32,630
And we're just going to honestly return and not do anything else, if that's the case.

35
00:02:32,630 --> 00:02:34,630
So actually, I don't really need these, then.

36
00:02:37,030 --> 00:02:43,210
And there's a second case where if the size is one,

37
00:02:47,200 --> 00:02:55,570
then all we really need to do is check to see if the data value and this is the type of delete we're

38
00:02:55,570 --> 00:02:55,900
doing.

39
00:02:55,900 --> 00:03:03,580
If if that that wasn't clear yet, we are getting past this data value and we're trying to find that

40
00:03:03,580 --> 00:03:08,590
data value and delete the node that is associated with that data value, the node that has that data

41
00:03:08,590 --> 00:03:09,010
value.

42
00:03:09,700 --> 00:03:14,500
So I'm going to have to go ahead and check here to see if, in fact, this singular note that we have

43
00:03:14,500 --> 00:03:17,230
in the list has the same data we're looking for.

44
00:03:17,680 --> 00:03:24,250
So I'm going to say it's just going to be the head somewhere safe since downloads equal to the head.

45
00:03:24,250 --> 00:03:32,740
I'm going to see if downloads data is equal to the data value that is passed.

46
00:03:34,060 --> 00:03:44,220
If that is the case, then we're just going to say that head and tail on are going to be null pointer.

47
00:03:45,640 --> 00:03:47,980
We're going to decrease the size by one.

48
00:03:48,910 --> 00:03:57,520
So I'm going to say this one for size and then we're going to delete the download, which is pointing

49
00:03:57,520 --> 00:04:00,310
to head, which is going to be the only thing that's in our list.

50
00:04:03,580 --> 00:04:12,910
OK, so the next thing is or else which is going to be kind of similar, like I said, to insert just

51
00:04:12,910 --> 00:04:13,780
a little bit different.

52
00:04:14,920 --> 00:04:19,240
I'm going to say I already have the download pointer, so I'm not going to declare some new pointer

53
00:04:19,240 --> 00:04:20,770
to traverse that linked list.

54
00:04:20,780 --> 00:04:22,630
I'm going to go ahead and just start with that wow loop.

55
00:04:24,040 --> 00:04:30,520
And I'm going to say, well, download not equal to, you know, pointer.

56
00:04:33,820 --> 00:04:38,530
And then we're going to have to look at the different cases of deleting.

57
00:04:38,530 --> 00:04:46,900
We know that we only want to delete if we find a situation where download data is equal to the data

58
00:04:46,900 --> 00:04:47,860
value that's being passed.

59
00:04:47,860 --> 00:04:50,800
So let's go ahead and just start that because we know we want that.

60
00:04:52,360 --> 00:05:00,670
So I'm going to say, if downloads data is equal to you data well and then we're going to have to put

61
00:05:00,670 --> 00:05:03,040
some conditions inside of here.

62
00:05:03,850 --> 00:05:09,400
So I have some more drawings similar to when we did insert.

63
00:05:11,840 --> 00:05:13,250
So we have a few conditions here.

64
00:05:15,010 --> 00:05:16,370
These two men will look at first.

65
00:05:16,390 --> 00:05:19,960
So we have the situation in which we're at the tail.

66
00:05:21,760 --> 00:05:28,660
And so if we're at the tail, we're going to need to have tails, previous, become the new tail once

67
00:05:28,660 --> 00:05:29,680
we delete this.

68
00:05:31,080 --> 00:05:36,900
And then we're also going to want to set that knows that new tail, we're going to want to say this

69
00:05:36,900 --> 00:05:44,340
next point or to know because now it's supposed to be what tail was pointing to, which was all so pretty

70
00:05:44,340 --> 00:05:44,880
simple.

71
00:05:45,330 --> 00:05:47,850
And then for head, it's the opposite kind of thing.

72
00:05:47,850 --> 00:05:54,180
So if we're deleting the head and we want to have heads next, it's previous.

73
00:05:54,180 --> 00:05:55,350
Should be pointing to no.

74
00:05:55,740 --> 00:06:01,330
And this next is, are you already going to be pointing to the next node?

75
00:06:01,350 --> 00:06:05,180
So I actually shouldn't really have this here.

76
00:06:05,270 --> 00:06:07,130
I should probably go ahead and delete that.

77
00:06:10,060 --> 00:06:10,840
Same here.

78
00:06:10,870 --> 00:06:15,220
I left this note here, so that's actually not going to be.

79
00:06:17,740 --> 00:06:22,000
True, necessarily, unless there is just that one.

80
00:06:25,350 --> 00:06:32,340
Item, in fact, that wouldn't be true, this would just be nil anyways, so yeah, I mean, it could

81
00:06:32,340 --> 00:06:35,100
just have two items like this, but.

82
00:06:36,600 --> 00:06:41,340
Yeah, that's not that's that's not necessarily pointing to, no, so I'm going to go ahead and have

83
00:06:41,340 --> 00:06:42,630
that not be there.

84
00:06:43,830 --> 00:06:45,870
But yeah, this is pretty easy.

85
00:06:45,900 --> 00:06:48,120
We're going to update this previous to Noel.

86
00:06:48,260 --> 00:06:51,420
And so these two things are pretty similar to just this one's opposite.

87
00:06:52,980 --> 00:06:57,210
So let's go ahead and do those two first before I talk about the next case.

88
00:06:59,220 --> 00:07:09,120
So that is going to be one situation is whether downloads, notes previous is equal to, you know,

89
00:07:09,120 --> 00:07:09,600
pointer.

90
00:07:11,430 --> 00:07:21,930
And that is the case in which no, in which the node that we're on is going to be the head.

91
00:07:22,260 --> 00:07:23,820
So I'll go ahead and say that.

92
00:07:24,480 --> 00:07:40,470
I say if the node is guilty and and then we will go ahead and add in our head equals head arrow next.

93
00:07:41,400 --> 00:07:47,970
So we're making had become the thing to it's right.

94
00:07:48,880 --> 00:07:54,640
And then we're going to say that its previous is going to be no point now.

95
00:07:55,870 --> 00:08:01,690
So just going back to the drawing to make that clear, we are deleting the head here.

96
00:08:02,230 --> 00:08:05,350
So we updated head to head Arrow next.

97
00:08:05,380 --> 00:08:06,610
So now this is head.

98
00:08:07,120 --> 00:08:11,950
And then we said that its previous is now equal to no pointer since it's that.

99
00:08:13,950 --> 00:08:24,950
OK, so that should be good for that first case, the next case will be if the node is the tail.

100
00:08:26,070 --> 00:08:36,240
And for that, I will be saying LCF down node arrow next is equal to null pointer.

101
00:08:39,360 --> 00:08:42,450
And in that case, we are going to.

102
00:08:43,960 --> 00:08:52,300
Have the new tale become tales previous and that new tales next needs to get set to know?

103
00:08:52,510 --> 00:08:54,370
So let's go ahead and do that.

104
00:08:54,880 --> 00:09:03,870
I'm going to say tail equals tail arrow three and then I'm going to say that tails next.

105
00:09:03,970 --> 00:09:10,000
This is a new tale now, and I'm saying it's next is going to be able to, you know, pointers set to

106
00:09:10,000 --> 00:09:10,660
no point here.

107
00:09:12,490 --> 00:09:22,330
OK, so now we come up on the final case, which is if the node is somewhere in the middle.

108
00:09:22,660 --> 00:09:26,140
I'm just calling that the middle, but anywhere in between.

109
00:09:27,370 --> 00:09:28,660
So that's going to be an else.

110
00:09:29,890 --> 00:09:32,320
And let's take a look at what that's going to look like.

111
00:09:32,650 --> 00:09:39,460
So if I scroll up here, if we delete something in between, we pretty much need to change both of these

112
00:09:39,460 --> 00:09:40,150
pointers.

113
00:09:40,450 --> 00:09:45,460
So I'm referring to this is still next and download for you.

114
00:09:45,490 --> 00:09:47,050
This is the note that we want to delete.

115
00:09:47,290 --> 00:09:51,130
So what did you notice on our little iterator style pointer?

116
00:09:52,270 --> 00:09:56,950
So we're going to have to set this proved that Dell knows produce of downloads next is going to get

117
00:09:58,150 --> 00:10:03,370
downloads next three is going to get set to download preve and downloads.

118
00:10:03,370 --> 00:10:09,550
Preuves next is going to be set to downloads next and then we'll delete the note.

119
00:10:10,030 --> 00:10:11,890
So let's go ahead and implement that.

120
00:10:13,660 --> 00:10:20,980
So I'm going to say downloads next preve.

121
00:10:22,360 --> 00:10:24,460
And so that's this one right here.

122
00:10:26,820 --> 00:10:31,110
Downloads next preve, and we're going to set it equal to this.

123
00:10:32,100 --> 00:10:33,540
Downloads preve.

124
00:10:35,160 --> 00:10:43,950
So let's go ahead and do that, I'm going to say that setting or setting it to download Creek and then

125
00:10:43,950 --> 00:10:44,880
the opposite.

126
00:10:45,090 --> 00:10:51,330
I'm going to say downloads free next.

127
00:10:53,590 --> 00:10:56,470
It's going to be set to download next.

128
00:10:56,590 --> 00:10:59,050
So let's just go back just to make this really clear.

129
00:11:00,370 --> 00:11:01,730
So downloads.

130
00:11:01,750 --> 00:11:03,490
Previous Next.

131
00:11:03,520 --> 00:11:06,700
If getting set to download next.

132
00:11:07,770 --> 00:11:09,930
So this was going to be in the parentheses.

133
00:11:10,170 --> 00:11:12,720
And then this is going to be definitely a previous next.

134
00:11:13,290 --> 00:11:14,620
And then Frances, and this will you.

135
00:11:14,880 --> 00:11:15,990
That's what's inserted in there.

136
00:11:15,990 --> 00:11:18,270
So we already have that.

137
00:11:18,630 --> 00:11:20,670
So let's put download next in here.

138
00:11:25,560 --> 00:11:26,100
OK.

139
00:11:28,790 --> 00:11:39,800
And then so I didn't add the changes to the size and the actual deletion in each one of these because

140
00:11:39,800 --> 00:11:42,230
we know that if this is the case.

141
00:11:43,350 --> 00:11:47,170
That the data matches we are going to delete no matter what.

142
00:11:47,190 --> 00:11:52,260
So I'm just going to put one instance of this, so it's going to do these things based on these kind

143
00:11:52,260 --> 00:11:53,700
of sub conditions in here.

144
00:11:54,030 --> 00:12:03,240
And then regardless which one of these gets executed with whichever one we jump into the scope of,

145
00:12:03,240 --> 00:12:08,170
we're going to still down here do the same change of the size.

146
00:12:08,170 --> 00:12:14,670
So size minus equals one and then the deletion of download.

147
00:12:14,700 --> 00:12:17,400
So I'm going to say delete, download.

148
00:12:19,370 --> 00:12:26,090
Because we know that like if this is the case where the data is equal, we're going to for sure want

149
00:12:26,090 --> 00:12:28,830
to delete it because we found it.

150
00:12:29,570 --> 00:12:32,120
And at that point, we can just return actually.

151
00:12:32,130 --> 00:12:41,090
So I'm going to say return, and then we need to put our thing that drives the while loop to traverse

152
00:12:41,090 --> 00:12:45,770
the list doesn't try the while loop, it drives our traversal.

153
00:12:46,250 --> 00:12:52,940
So I'm going to say download equals download arrow next.

154
00:12:53,570 --> 00:12:56,840
Just like our curve equals Kerr Arrow next to us up here.

155
00:12:59,560 --> 00:13:07,000
OK, so that should be good, and we should now write some code that will actually test that this works

156
00:13:07,000 --> 00:13:08,410
just like we always do.

157
00:13:09,250 --> 00:13:12,880
So I'm going to head back to Maine.

158
00:13:13,240 --> 00:13:15,910
And if everything else?

159
00:13:17,700 --> 00:13:18,660
It's good.

160
00:13:19,170 --> 00:13:22,800
Hopefully, we will be able to see some decent results.

161
00:13:24,120 --> 00:13:28,890
So in Maine here, I'm just going to add a couple of things.

162
00:13:29,520 --> 00:13:36,240
I'm going to delete a couple of values first that I know are in my list just singularly and then print

163
00:13:36,240 --> 00:13:38,340
it out so I can verify that.

164
00:13:39,330 --> 00:13:44,940
So we just have one through eight in here out of order, because previously we're making that order

165
00:13:44,940 --> 00:13:46,980
link list with the insert function.

166
00:13:48,120 --> 00:13:57,830
And so since this won't do, I'm just going to say, let's see out that we are deleting the value five

167
00:13:58,650 --> 00:14:04,020
and I did not spell delete deleting the value five.

168
00:14:06,750 --> 00:14:09,230
And that's been in line.

169
00:14:09,990 --> 00:14:21,810
And so I'm going to say Link to list what's Dell and then I'm just going to put five in here and then,

170
00:14:21,810 --> 00:14:27,600
let's say, also put the lean evaluate until it from the end.

171
00:14:31,420 --> 00:14:40,410
The line, and then I'll say, let's go ahead and delete like the first value will be won so we can

172
00:14:40,410 --> 00:14:41,160
delete that.

173
00:14:41,910 --> 00:14:47,880
So I'll put let's put a we'll delete these two and then I'll print this out.

174
00:14:48,990 --> 00:14:54,690
So let's do a linked list print.

175
00:14:56,580 --> 00:15:06,680
And then after this, let's go ahead and just say we need value one which.

176
00:15:08,580 --> 00:15:10,200
And then we will do

177
00:15:12,750 --> 00:15:14,250
delete of the value one.

178
00:15:18,540 --> 00:15:21,600
And then after that, yeah, let's go ahead and print that.

179
00:15:25,020 --> 00:15:29,050
After that, we'll go ahead and delete the rest, so I'm actually just going to make a for loop.

180
00:15:30,180 --> 00:15:33,810
There's not going to be this many items in there, but I'm just going to say

181
00:15:36,840 --> 00:15:40,410
I'm just going to say actually that it's just going to go from zero to seven.

182
00:15:40,410 --> 00:15:43,590
So I'll do like zero.

183
00:15:43,590 --> 00:15:47,190
That value is not going to be in there, but I'll go ahead and just write this for a loop.

184
00:15:47,790 --> 00:15:54,120
If we don't find something to delete, then it just basically doesn't do anything and I delete function.

185
00:15:54,130 --> 00:15:54,570
So.

186
00:15:58,130 --> 00:15:59,450
That should be fine.

187
00:15:59,600 --> 00:16:01,760
So I have.

188
00:16:04,830 --> 00:16:10,340
Plus plus A. equals zero I less than eight plus plus.

189
00:16:13,020 --> 00:16:23,790
And then let's just say the list to Levi, and then we'll print it after this to verify that it just

190
00:16:23,790 --> 00:16:25,480
says forwards and backwards.

191
00:16:25,560 --> 00:16:29,940
What does it say with nothing in our link list being printed, such as blank lines there?

192
00:16:30,990 --> 00:16:35,250
So I'm going to say only first print and we shouldn't be seeing anything in there.

193
00:16:36,760 --> 00:16:46,930
OK, so if everything is all good, we should see that it removes the value five and eight versus a

194
00:16:46,930 --> 00:16:49,940
burner phone link list in order one through eight.

195
00:16:49,960 --> 00:16:53,800
Then it should delete five and eight.

196
00:16:54,070 --> 00:16:56,800
It should be the last value and five would be somewhere in the middle there.

197
00:16:56,890 --> 00:16:59,830
So we should notice that when we print it here.

198
00:17:00,790 --> 00:17:04,810
Then you should delete the first value and we can verify that it did that.

199
00:17:06,110 --> 00:17:11,300
By printing right here, and then we'll just delete the rest and verify that everything is working.

200
00:17:12,020 --> 00:17:13,400
So that's how we're going to test this.

201
00:17:13,790 --> 00:17:15,680
Let's go ahead and run.

202
00:17:20,830 --> 00:17:23,530
OK, so we have a problem.

203
00:17:25,590 --> 00:17:26,000
This.

204
00:17:29,690 --> 00:17:35,900
No, oh, yeah, it's like I forgot and l hear my bad.

205
00:17:37,350 --> 00:17:40,710
OK, let's go ahead and do this again.

206
00:17:44,170 --> 00:17:44,980
All right.

207
00:17:47,160 --> 00:17:51,450
OK, so we see and fortunately, it did not delete.

208
00:17:55,090 --> 00:17:56,200
The eight.

209
00:18:02,120 --> 00:18:03,980
Leading up is that because.

210
00:18:07,590 --> 00:18:10,140
Deleting the value eight, I never actually deleted that.

211
00:18:10,500 --> 00:18:11,070
So.

212
00:18:17,470 --> 00:18:19,280
That would be a problem.

213
00:18:19,300 --> 00:18:21,460
So let's go ahead and.

214
00:18:23,050 --> 00:18:27,880
Do that because it just went up to seven in our loop and it didn't delete it, so it looks like stuff

215
00:18:27,880 --> 00:18:30,490
like worked pretty well, but let's go ahead and actually have this.

216
00:18:30,820 --> 00:18:32,530
So it's doing what we want it to do.

217
00:18:34,570 --> 00:18:39,220
OK, so that we're seeing an empty list, so let's go ahead and just verify everything so forwards and

218
00:18:39,220 --> 00:18:40,540
backwards, we have one through eight.

219
00:18:41,740 --> 00:18:43,750
Still leaves the value five deletes evaluate.

220
00:18:44,200 --> 00:18:46,180
We have one two three four No.

221
00:18:46,180 --> 00:18:48,070
Five six seven No.

222
00:18:48,070 --> 00:18:48,430
Eight.

223
00:18:49,760 --> 00:18:57,020
And then we delete the value one, so it starts at two two three four five six seven eight.

224
00:18:59,310 --> 00:19:01,740
And then we delete the rest.

225
00:19:03,150 --> 00:19:06,180
And we have a blank right here.

226
00:19:07,160 --> 00:19:09,020
So it looks like it works pretty well.

227
00:19:10,910 --> 00:19:17,420
So hopefully that explained it enough for you and go back to these drawings here, and yeah, I will

228
00:19:17,420 --> 00:19:21,500
try and include this as some material that you can refer back to.

229
00:19:21,980 --> 00:19:28,070
Hopefully this was explanatory enough for these three different operations that we had in our else.

230
00:19:30,820 --> 00:19:31,320
OK.

231
00:19:31,930 --> 00:19:37,960
So congratulations, if you were able to figure this out in the coding exercise, if not, don't worry.

232
00:19:38,290 --> 00:19:45,010
This is kind of a complicated operation, so hopefully you were able to figure it out now after this

233
00:19:45,010 --> 00:19:48,520
video than that, I will see you in the next lecture.
