text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
But it's pointing at clearly
something that exists. | 2,162.92 | 2.25 |
So, this node here has
some valid address. | 2,165.17 | 2.79 |
Pointer is pointing at
it, so it's not null. | 2,167.96 | 2.5 |
So, what do I do inside of my code? | 2,170.46 | 1.93 |
I check if the n value
inside of pointer, PTR, | 2,172.39 | 7.84 |
equals equals the
number I'm looking for, | 2,180.23 | 2.11 |
and if so, return true, otherwise,
if not I update pointer. | 2,182.34 | 3.68 |
So let's check. | 2,186.02 | 0.91 |
Let's follow the arrow, PTR,
and look at the value n. | 2,186.93 | 3.42 |
Recall that the top of these boxes is n. | 2,190.35 | 2.02 |
The bottom of them is called
next-- n, next, n next. | 2,192.37 | 3.91 |
So, I followed this pointer. | 2,196.28 | 1.21 |
I'm looking at the box called n. | 2,197.49 | 3.08 |
Does 3 equal 1? | 2,200.57 | 1.15 |
No, obviously not. | 2,201.72 | 1.5 |
So I update-- pointer gets pointer next. | 2,203.22 | 4 |
So, to be clear, pointer
gets pointer next, | 2,207.22 | 4.6 |
that second to last line of actual code. | 2,211.82 | 2.09 |
So, what does that mean I need to do? | 2,213.91 | 2.14 |
That means I need to update pointer
to be equal to pointer next. | 2,216.05 | 4.57 |
What is pointer next? | 2,220.62 | 0.93 |
Well, here's pointer and here's next. | 2,221.55 | 2.5 |
We were looking a moment ago at n. | 2,224.05 | 1.43 |
Now I'm looking at next. | 2,225.48 | 1.5 |
So, pointer next means that I should
update whatever is inside this box-- | 2,226.98 | 7.11 |
and a lot more on the screen-- to
be equal to pointer next, which | 2,234.09 | 6.35 |
is this field. | 2,240.44 | 0.62 |
This field is pointing at
that, so that line of code | 2,241.06 | 3.08 |
has the effect of updating PTR to
simply point at the second node. | 2,244.14 | 5.05 |
So, what happens next? | 2,249.19 | 1.31 |
I seem to still be in that loop and I
say, well, pointer does not equal null, | 2,250.5 | 3.645 |
and it doesn't. | 2,254.145 | 0.625 |
It's pointing at that second node. | 2,254.77 | 1.416 |
If pointer arrow n equals
equals n, but no that's not | 2,256.186 | 4.034 |
the case, because I'm looking for three. | 2,260.22 | 1.74 |
I'm pointing at two,
so that is again false. | 2,261.96 | 2.45 |
So, again, I don't return true. | 2,264.41 | 1.61 |
I instead update pointer
to equal pointer next. | 2,266.02 | 3.13 |
So, what has to happen here, at the
risk of deleting my handiwork again, | 2,269.15 | 7.81 |
now pointer gets pointer next,
which is this element, which | 2,276.96 | 4.48 |
is equivalent to pointing
at this node here. | 2,281.44 | 2.84 |
And so, now I'm still inside that loop
while pointer-- not equal to null. | 2,284.28 | 5.35 |
It's not null. | 2,289.63 | 0.82 |
If pointer arrow n equals equals
n, well, let's follow that logic. | 2,290.45 | 3.94 |
If pointer, follow the
arrow, n equals equals n, | 2,294.39 | 4.36 |
three-- which is the one I'm
looking for-- returned true. | 2,298.75 | 4.33 |
And so, how then does this
function ultimately behave? | 2,303.08 | 2.8 |
It would seem in this case to return
true, because I have eventually | 2,305.88 | 4.04 |
found that number three. | 2,309.92 | 2.39 |
What would happen by contrast if I were
looking not for three, but for four | 2,312.31 | 4.27 |
with this code? | 2,316.58 | 1.42 |
In other words, what if I'm not
looking for three and I want to go one | 2,318 | 3.65 |
step further? | 2,321.65 | 0.58 |
Well, one step further
is going to update PTR | 2,322.23 | 3.11 |
to equal null, that
slash in my last node. | 2,325.34 | 5.21 |
And that means code wise,
I'm going to break out | 2,330.55 | 2.65 |
of that loop, because
pointer now does equal null. | 2,333.2 | 2.89 |
And so, by default that very last
line of code return false, not found. | 2,336.09 | 4.98 |
So, complicated at first
glance, and it certainly | 2,341.07 | 2.147 |
looks more complicated than
things we've written before, | 2,343.217 | 2.333 |
but again, if you go back to basics,
what does each of these lines mean? | 2,345.55 | 3.46 |
Consider that there's no magic here. | 2,349.01 | 2.21 |
This first line means give me a
variable that's a pointer to a node. | 2,351.22 | 3.967 |
It'd be in other words
the address of a node | 2,355.187 | 1.833 |
and assign it whatever I was passed in. | 2,357.02 | 1.816 |
While pointer [? naught ?] equals
null, we've seen null before. | 2,358.836 | 2.624 |
It's this special zero
value, and I'm just | 2,361.46 | 2.15 |
making sure that the pointer I'm using,
PTR, does not equal that special value. | 2,363.61 | 3.63 |
And then inside of this loop I'm using
one piece of new syntax, this arrow | 2,367.24 | 3.21 |
notation, which just like the picture
suggests means go there, and then | 2,370.45 | 3.51 |
look at the field called n and check
if it equals the n you're looking for, | 2,373.96 | 3.81 |
and if so return true. | 2,377.77 | 1 |
Otherwise, update yourself
much like i plus plus | 2,378.77 | 3.27 |
but specifically update pointer to
be whatever the value is when you | 2,382.04 | 4.48 |
follow the arrow in that next field. | 2,386.52 | 4.234 |
So, this of course is just search. | 2,390.754 | 1.416 |
We've not actually changed the
list, but imagine, if you will, | 2,392.17 | 3.69 |
that you could now
implement insert and delete, | 2,395.86 | 3.07 |
not simply by following these
pointers but actually changing | 2,398.93 | 3.74 |
the value of next in a node to the left,
a node to the right, or a new node all | 2,402.67 | 6.9 |
together. | 2,409.57 | 1.47 |
So, who cares? | 2,411.04 | 1.66 |
Why did we add all of this complexity? | 2,412.7 | 2.01 |
We had arrays, which were working
really well for a whole bunch of weeks, | 2,414.71 | 3.43 |
and now we've claimed that
arrays are not so good. | 2,418.14 | 2.92 |
We want to use linked lists instead. | 2,421.06 | 1.8 |
But why might we want
to use linked lists? | 2,422.86 | 1.81 |
Well, linked lists gives us dynamism. | 2,424.67 | 1.63 |
We can call malloc and give ourselves
more, and more, and more nodes | 2,426.3 | 3.21 |
and grow our list of numbers, even
if we don't know in advance how | 2,429.51 | 2.91 |
many such numbers we need. | 2,432.42 | 1.44 |
And we can shrink them,
similarly, so we don't | 2,433.86 | 2.19 |
have to allocate a massive
array unnecessarily. | 2,436.05 | 2.81 |
We can shrink our data structure based
on how many numbers we actually need. | 2,438.86 | 3.31 |
But we're paying a price. | 2,442.17 | 1.37 |
Search is a little bit slower,
delete is a little bit slower. | 2,443.54 | 3.15 |
Insert would be slower if we
insist on keeping things sorted, | 2,446.69 | 3.42 |
so we've paid this price. | 2,450.11 | 1.182 |
And indeed, this is thematic. | 2,451.292 | 1.208 |
In CS50, in computer
science more generally, | 2,452.5 | 2.45 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.