text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
in that list, as suggested here.
1,876.6
1.99
So, now I'm just giving myself a temporary variable called pointer,
1,878.59
2.914
but I can call it anything I want.
1,881.504
1.416
And I'm declaring it as node star, so that it can store the address of a node
1,882.92
3.57
as well, and then I'm just initializing it to be
1,886.49
2.2
the exact value that was passed in.
1,888.69
1.491
So, I don't want to accidentally break the list that I was passed.
1,890.181
2.749
I don't want to change the value of my parameter
1,892.93
2
unnecessarily and complicate things.
1,894.93
1.73
I just really want a temporary variable, much
1,896.66
2.14
like I in the world of loops that allows me to constantly iterate
1,898.8
4.33
through something and update it as I go while the whole along the way
1,903.13
5.476
I want to be checking this.
1,908.606
1.124
While pointer is not null.
1,909.73
1.97
If pointer is null, that means I'm at the end of the list,
1,911.7
3.02
or maybe more curiously, I was passed null, in which case there is no list.
1,914.72
4.601
And that's a valid scenario that could happen, even though it's a bit strange.
1,919.321
3.249
But if pointer is null, I don't want to proceed further inside of this loop.
1,922.57
5.8
But so long as pointer is not null, let me go ahead and do this.
1,928.37
3.89
Let me follow that pointer and go inside that node and say
1,932.26
3.98
is your n value equal equal to the [? end ?]
1,936.24
2.84
value that I've been asked to search for?
1,939.08
2.05
And if so, return true.
1,941.13
1.482
I don't want to just return false now because otherwise I'd
1,942.612
2.458
only ever be checking the first element in a linked list.
1,945.07
2.97
So, I now want to do the equivalent in spirit of i plus plus.
1,948.04
6.45
But I'm not using i's.
1,954.49
0.917
I don't need to use pointer arithmetic here,
1,955.407
1.832
and indeed it won't work because I have this thing stitched together.
1,957.239
2.971
It's not an array of contiguous memory.
1,960.21
2.26
I want to say that my current temporary value
1,962.47
2.37
pointer should get whatever pointer arrow next
1,964.84
3.59
is, and then let this loop continue.
1,968.43
2.46
If it's not null, check again.
1,970.89
1.38
If it's [? end ?] value equals what I'm looking for and repeat, repeat,
1,972.27
3.2
repeat until pointer equals null.
1,975.47
2.234
So, let's make this more concrete.
1,977.704
1.416
Let me go ahead and just draw a temporary picture here.
1,979.12
2.75
And let me suppose here that what I have been passed
1,981.87
3.21
is something like the following.
1,985.08
2.84
Let's do a very simple linked list that has maybe the number one,
1,987.92
4.53
and has the number two, and has the number three.
1,992.45
5.74
And, again, I've drawn gaps between these nodes,
1,998.19
2
because they could be anywhere in memory.
2,000.19
1.39
So, technically they don't need to be left to right like this,
2,001.58
2.2
but that'll keep us sane.
2,003.78
1.21
And if this is indeed a correct linked list,
2,004.99
2.19
there are pointers in each of those fields
2,007.18
3.11
that point to the next node in the list, and that slash
2,010.29
2.415
I drew in the last one just means null.
2,012.705
1.625
You can draw it however you want.
2,014.33
1.43
But for a linked list to work, we need to know the beginning of this thing.
2,015.76
3.39
So, we'll call this first, and that of course
2,019.15
3.34
has to point to the first element in my linked list.
2,022.49
2.314
So, here's the state of our world.
2,024.804
1.416
It's a linked list quite similar in spirit to the other one.
2,026.22
2.59
It's a little shorter, just for the sake of discussion.
2,028.81
2.291
And now, let's consider my function search,
2,031.101
2.959
which again takes two arguments.
2,034.06
2.04
So, that the first argument is of type int called n.
2,036.1
3.4
And suppose I'm searching for the number three.
2,039.5
2.72
The second argument is a node star, so the address of a node called list.
2,042.22
3.82
So, what does that mean?
2,046.04
1.11
When this function search is called, let's
2,047.15
3.759
suppose that we currently have the value n, which
2,050.909
4.191
is going to be 3, because that's arbitrarily
2,055.1
1.91
the number of decided to search for.
2,057.01
1.629
And then this other value pointer is going to be initialized-- sorry,
2,058.639
7.031
not that.
2,065.67
1.219
List is going to be whatever pointer is passed
2,066.889
5.6
in as the second argument to this function.
2,072.489
2.281
So, let's suppose that this linked list, this sample linked list at the top,
2,074.77
4.07
is indeed what is passed in to this function.
2,078.84
3.682
So, I've passed in 3, because I want to search for 3.
2,082.522
2.208
So what goes here?
2,084.73
1.199
If I pass this sample linked list into my search function,
2,085.929
6.29
what is the value of list?
2,092.219
3.23
Well, list if I'm past this first pointer
2,095.449
4.411
is really going to be the pointer to the first element in the list.
2,099.86
3.72
That's all we're saying.
2,103.58
1
Node star list just means give me the address
2,104.58
1.949
of a linked list, which means give me the address of the first node
2,106.529
2.791
in the linked list, which means that initially when
2,109.32
2.125
I call search my picture-- my stack frame, if you will,
2,111.445
3.775
in terms of my local arguments-- is going to look like this.
2,115.22
2.88
All right, so with that said, how does this code work?
2,118.1
3.44
We recall in this code have this while loop that
2,121.54
2.94
just sits in a loop checking whether the current nodes
2,124.48
3.69
n equals equals the one we're looking for, and if not, it updates it.
2,128.17
3.08
So, we need one more local variable called pointer that's
2,131.25
3.19
initialized to the start of this list.
2,134.44
3.37
So, this will be a pointer and it's initialized to the same thing
2,137.81
5.15
that my second argument is initialized to.
2,142.96
3.48
So, this now is the state of our world once one line of code has executed,
2,146.44
4.87
that very first one in there.
2,151.31
2.196
So, now let's implement the loop.
2,153.506
1.374
While pointer does not equal null, so here's pointer.
2,154.88
3.18
Does it equal null?
2,158.06
1.252
No, because if it did, we would just draw
2,159.312
1.708
a slash or some other piece of syntax.
2,161.02
1.9