text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
this is a key feature of the linked list.
1,584.09
1.708
These nodes could have been malloc from anywhere.
1,585.798
2.432
So indeed, even though we initially kept everyone physically sorted
1,588.23
3.3
left to right-- and you've all cleaned the list up even since-- that's OK.
1,591.53
5.04
The point is that all of these nodes are linked together.
1,596.57
3.024
So, thank you so much to our volunteers.
1,599.594
1.666
You can keep these pieces of paper and later on we'll
1,601.26
2.208
have some stress balls for you.
1,603.468
1.442
But that's the key idea here behind a linked list.
1,604.91
6.23
Thank you.
1,611.14
1.66
So, of course, there are some more complicated operations
1,612.8
3.31
that we might have to deal with.
1,616.11
1.334
For instance, if we want to insert into the middle of the list,
1,617.444
2.624
that's going to be a little more of a burden on me, the program,
1,620.068
2.682
keeping track of where things have to go.
1,622.75
2.77
But nicely enough, there's only these three cases--
1,625.52
2.38
the beginning of the list, the end of the list, and the middle of the list,
1,627.9
2.72
because middle of the list doesn't have to mean literally the middle,
1,630.62
2.7
just anywhere that's not the beginning or the end.
1,633.32
2.127
Of course, we should be careful to make sure
1,635.447
1.833
that we handle the empty list scenario, which
1,637.28
2.73
is equivalent to putting something at both the beginning of the list
1,640.01
3.19
and the end of the list.
1,643.2
1.63
But that would be perhaps a special case we could deal with separately.
1,644.83
3.92
Of course, there are other operations like inserting-- or rather removing
1,648.75
3.71
from the tail of the list, removing from the head of the list,
1,652.46
2.68
and removing in the middle.
1,655.14
1.51
And that would be the opposite of malloc, if you will.
1,656.65
2.73
And in those cases, we have to take care to call
1,659.38
2.29
our friend free to free those bytes of memory,
1,661.67
3
give them back to the operating system so that we don't leak memory.
1,664.67
2.86
But there, too, I'm probably going to have to be careful as to what order
1,667.53
4.28
I change my pointers and free nodes.
1,671.81
2.15
Because what you don't want to do, and what unfortunately you
1,673.96
2.79
might very well accidentally do at some point,
1,676.75
2.45
is free a pointer and then try to access that pointer or change the pointer,
1,679.2
4.57
even after you've told the operating system I'm done with this address.
1,683.77
3.094
That can give you what's called a segmentation
1,686.864
1.916
fault, which is just one of the ways in which you
1,688.78
2.05
can deduce that kind of mistake.
1,690.83
3.29
So, let's actually implement one of these methods.
1,694.12
2.779
And we'll pluck off one that allows us to actually take
1,696.899
2.291
a look at the syntax with which we can manipulate pointers.
1,699.19
2.559
And let's go ahead and implement a function
1,701.749
1.791
called search, for instance, where search
1,703.54
2.02
I [? proposed ?] just returns a bool, true or false, this number n
1,705.56
4.15
is in the given the list.
1,709.71
1.61
And now, why have I said node star list?
1,711.32
2.26
Well, at the end of the day, a linked list is just a whole bunch of nodes.
1,713.58
4.76
But the first of those nodes that we keep calling first is of what
1,718.34
3.95
data type?
1,722.29
1.24
If you have a pointer, a variable, that's
1,723.53
2.6
pointing to a linked list, that means it's storing the address of a node,
1,726.13
6.17
otherwise known as a node star.
1,732.3
1.48
So, this would be the syntax with which you can pass to a function something
1,733.78
4.16
like a linked list.
1,737.94
0.87
You simply have to pass it a pointer to the first element in that list.
1,738.81
3.99
And if I want to go ahead now and implement this,
1,742.8
3.17
let me go ahead and propose the following.
1,745.97
2.7
Let me go ahead here and give myself a temporary value, so node star pointer
1,748.67
5.19
we'll call it, PTR.
1,753.86
1.33
And that's going to equal the start of the list.
1,755.19
2.36
So, I'm just creating another box of memory
1,757.55
1.975
and I'm storing inside of it the same address
1,759.525
1.875
that I was passed in, just so that I have a temporary variable that I
1,761.4
2.874
can use to update.
1,764.274
1.366
After this, let me go ahead and say while that pointer is not
1,765.64
3.46
equal to null-- because recall that null is this special sentinel value that
1,769.1
4.04
means end of the list.
1,773.14
1.53
So inside of this loop, what do I want to do?
1,774.67
3.41
I'm going to go ahead and say if pointer--
1,778.08
4.25
and now I have to get at the number inside of it.
1,782.33
4.03
So, if I recall from the last time, we only spent a little bit of time
1,786.36
3.42
on the student example, but we said something like student
1,789.78
3.07
dot name or student dot dorm.
1,792.85
2.055
And in this case I'm inclined to say pointer dot n, where n is
1,794.905
4.125
the number, the integer that's inside.
1,799.03
2.44
But pointer this time is not a struct, per se.
1,801.47
2.53
It's the address of a node.
1,804
1.83
It's the address of a struct.
1,805.83
1.46
And so, perhaps the most intuitive piece of syntax in C,
1,807.29
5.54
at least retrospectively now, is that if you
1,812.83
2.35
want to access a piece of data that's inside of a node
1,815.18
4.22
and you have a pointer to that node much like our arrows in the pictures imply,
1,819.4
4.35
you literally draw an arrow using a hyphen
1,823.75
2.43
and then using a right angle bracket.
1,826.18
4.57
So, now if we do see-- whoops, let me finish my thought.
1,830.75
3.7
If pointer n equals equals the n we're looking for, let me go ahead in here
1,834.45
4.8
and say return true.
1,839.25
1.97
Or else, let me go ahead and not return false,
1,841.22
2.597
because I don't want to just check one element and then blindly say false.
1,843.817
3.083
I instead want to say pointer should get pointer arrow next.
1,846.9
5.03
And then only after that loop is all complete
1,851.93
3.28
should I say something like nope, return false.
1,855.21
3.83
So, what's actually going on here?
1,859.04
2.05
The function declaration, again, took in two arguments--
1,861.09
2.52
one, an int n that we're looking for, two a pointer to a node,
1,863.61
4.68
otherwise known as a node in a linked list.
1,868.29
1.867
And per the pictures we've been drawing, you
1,870.157
1.833
can access any other element in that linked list by way of the first element
1,871.99
4.61