text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
And for that, I think I'm going to need a struct.
788.9
2.4
And indeed let me propose that to solve this problem we give ourselves
791.3
4.54
this building block as a new C data type called a node.
795.84
3.802
You can call it anything you want, but the convention
799.642
2.208
would be to call something like this in a data structure-- that's
801.85
2.27
like a puzzle piece or a building block in a data structure
804.12
2.458
would be called a node.
806.578
1.192
Let me propose that we define it as follows.
807.77
2.98
I'm using that same syntax from last time with which we declared a student
810.75
3.31
data type, but here I'm saying inside of this data structure,
814.06
3.66
this node shall be an int.
817.72
2.154
And that's pretty straightforward.
819.874
1.416
Just like a student might have a name and a dorm,
821.29
2.041
this node will have an int called n arbitrarily.
823.331
3.589
And then the only piece of detail that's a little bit new now is
826.92
3.67
the second line, struct node star next.
830.59
2.769
Now, what does that mean?
833.359
1.041
It's pretty verbose, but struct node is just recursively, if you will,
834.4
4.73
referring to this same type of data structure.
839.13
2.95
Star means this is going to be a pointer, the address of one such thing,
842.08
3.72
and next is just an arbitrary but pretty reasonable
845.8
2.36
name to give to such a pointer.
848.16
2.06
So this line here, struct node star next,
850.22
2.83
is the incantation in C with which you declare
853.05
3.74
one of those arrows that will point from one node, one rectangle
856.79
3.9
to another node, another rectangle.
860.69
2.57
And the fact that we have a little bit of additional verbiage
863.26
3.44
up here, typedef struct node, is because again C
866.7
3.58
is a language that is read top to bottom, left to right,
870.28
2.53
so words have to exist before you actually use them.
872.81
2.77
So, whereas last time when we declared a student,
875.58
2.064
we didn't actually mention struct student or anything like that.
877.644
2.666
We just said typedef open curly brace.
880.31
2.26
Today, when declaring a node, we actually
882.57
2.26
have to have some additional syntax here just called struct node.
884.83
3.167
And technically this word could be anything,
887.997
1.833
but I'll leave it as node for consistency.
889.83
2.09
And that allows me inside of this definition
891.92
2.68
or to specify that the second data member is
894.6
3.75
going to be a pointer to exactly that kind of data structure.
898.35
3.08
But typedef, just to be clear, allows me to type a smaller name for this data
901.43
4.75
structure here, which I will simply called node at this point.
906.18
4.89
So, what can we actually do with this kind of data structure now?
911.07
3.066
And, indeed, let's give this data structure a name.
914.136
2.124
Let's start calling a linked list.
916.26
1.89
Previously, we had arrays, but now we have linked lists, both of which
918.15
3.482
at the end of the day are types of lists, but linked lists,
921.632
2.458
as the name suggests, are linked or threaded together using pointers.
924.09
3.66
Now, when you have a linked list, what might be some operations,
927.75
3.146
some algorithms that you might want to run on them?
930.896
2.124
Well, if you've got a linked list of say numbers, for the sake of discussion,
933.02
3.208
you might want to insert a new number into that list.
936.228
2.253
You might want to delete a number from that list
938.481
1.999
and you might want to search that list.
940.48
3.034
And that allows us to then consider how we might implement
943.514
2.416
each of these kinds of things.
945.93
1.32
But it turns out while all simply-- while fairly simple intuitively,
947.25
4.36
we're going to have to be a little careful now by way of our pointers.
951.61
3.25
So, let's more formally declare a linked list to look something like this.
954.86
3.3
It's a collection of nodes that are linked together
958.16
2.95
with pointers as represented by these arrows here,
961.11
2.9
but we're going to need some special pointer, at least
964.01
2.8
at the beginning of the list.
966.81
1.259
Let's just call it first.
968.069
1.041
It doesn't necessarily store a actual integer.
969.11
3.15
It itself first is just a pointer to the start of the list.
972.26
4.28
And by way of that pointer can we access the first actual node in the list.
976.54
3.52
From there can we get at the second, from there can we get at the third,
980.06
3
and the fourth, and the fifth, and any number of others.
983.06
2
And this syntax over here might just represent null.
985.06
2.46
Because you don't want to have that pointer just
987.52
1.38
pointing off into no man's land, that will have to be a null pointer so
988.9
3.12
that if we check for that with a condition we know,
992.02
2.125
OK, we're at the end of the list.
994.145
1.627
So, let's pause for just a moment and consider these three algorithms--
995.772
2.958
insert, delete, and search, and consider what's going to be involved.
998.73
5.115
Well, how would you go about searching for an element of this list?
1,003.845
3.565
Suppose I wanted to find the number 22?
1,007.41
2.86
What do you do?
1,010.27
0.88
Well, me, I, the human can just look at this and be like all right,
1,011.15
2.791
22 is right there.
1,013.941
1.489
But a computer can't do that.
1,015.43
1.26
A computer every time we've had this discussion
1,016.69
1.64
can only look at one thing at a time.
1,018.33
1.76
But moreover the computer this time is even more constrained
1,020.09
2.9
because it can't just use our old friend binary search
1,022.99
4.04
or divide and conquer, because how do you get to the middle of a linked list?
1,027.03
4.285
Well, you have to find your way there.
1,031.315
1.764
The only thing you have in a linked list from the outset
1,033.079
3.231
is one pointer called first or whatever it is, but one pointer that leads you
1,036.31
4.44
to the beginning of the list, the first node in the list.
1,040.75
3.19
So, if you want to get to the second node in the list,
1,043.94
2.25
you can't just go to bracket one, or bracket two, or bracket three
1,046.19
2.81
to get any number of other elements in the list.
1,049
2
You have to follow these bread crumbs, if you will.
1,051
2.2
You have to follow these arrows or these addresses to go from one node's address
1,053.2
3.67
to the other to the other.
1,056.87
1.62
And so, we've paid a price already.
1,058.49
3.14
And we'll see that there is still an advantage here,
1,061.63
2.3
but what's the running time?
1,063.93
1.42