text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
on top of each other.
2,756.41
1.35
But this is just a pointer to a number.
2,757.76
3.07
Why might I want to implement a stack as just a pointer to an int?
2,760.83
3.64
That seems wrong.
2,764.47
2.83
I want lots of numbers, not one number.
2,767.3
3.75
So, what could I do?
2,771.05
1.95
Well, what if in this world to implement a stack I invoke our friend malloc
2,773
4.86
and I say to malloc, malloc, give me enough memory
2,777.86
2.54
for 2,000 numbers or 5,000 numbers.
2,780.4
3.32
What is malloc going to return?
2,783.72
1.43
Well, by definition, we know malloc is going
2,785.15
1.98
to return the address of a chunk of memory, and that chunk of memory
2,787.13
5.17
is going to be of whatever size I ask malloc for,
2,792.3
3.48
and the address of the first is really just equivalent to the address
2,795.78
3.44
of one integer.
2,799.22
1.28
And so long as I, the programmer, remember that I asked malloc
2,800.5
2.65
for 2,000 integers or for 5,000 integers,
2,803.15
3.28
I know implicitly the end of that chunk of memory and malloc
2,806.43
3.24
just need to tell me the beginning.
2,809.67
1.61
So, it's perfectly fine to implement the stack by way of a single pointer,
2,811.28
4.53
because all I need to know is, hey, malloc,
2,815.81
2.19
where should I put my first integer?
2,818
2.26
Because I know via pointer arithmetic, per last week,
2,820.26
2.99
that I can put my next integer four bytes later,
2,823.25
3.03
four bytes later, four bytes later.
2,826.28
1.78
And I'm deliberately going up this time, but it really
2,828.06
2.249
is just an array where you can think of the array as left and right.
2,830.309
2.961
So, this would be a way of giving ourselves a data structure called
2,833.27
3.6
a stack that is not fixed from the outset like this previous version
2,836.87
4.64
to some specific capacity.
2,841.51
1.87
Now, we are limited only by how much physical memory or virtual memory
2,843.38
4.24
my computer actually has.
2,847.62
3.37
So, suppose Apple or someone similar implemented the lines
2,850.99
6.22
outside their stores for the release of the iPhone as a stack.
2,857.21
5.214
So, it's weird maybe to think of people stacking on top of each other,
2,862.424
2.916
but maybe you could imagine Apple funneling everyone into the glass store
2,865.34
6.89
here in Manhattan, and then whoever is the last one in gets their phone first.
2,872.23
5.5
Because why?
2,877.73
0.65
They're closest to the exit.
2,878.38
1.31
So, you have all these people show up super early in the morning or days
2,879.69
2.58
before, you pile them all into the store saying everyone, hey,
2,882.27
2.47
please go into the corner there.
2,884.74
1.333
Please get into the store.
2,886.073
1.097
And then as soon as 9:00 AM rolls around and it's
2,887.17
2.217
time to give out the iPhones, just for logistical convenience you realize,
2,889.387
3.083
all right, why don't we just give the person who came in
2,892.47
1.65
last their phone first because they're closest to the exit
2,894.12
2.45
and get them out, last in, first out?
2,896.57
3.21
Good design, bad design?
2,899.78
2.1
It's correct in so far as everyone's going
2,901.88
2.92
to get an iPhone if supply is there, and that's never going to be the case.
2,904.8
4.27
So, it's not necessarily very equitable or fair,
2,909.07
2.802
and indeed the humans are not going to be very pleased with Apple
2,911.872
2.708
if they used a LIFO data structure or a stack.
2,914.58
4.34
What would these fans of Apple hardware prefer that Apple use?
2,918.92
6.25
We call it a line.
2,925.17
0.87
If you go to the UK, they call it a queue,
2,926.04
1.85
which is actually a perfect answer, because there's this other data
2,927.89
3.49
structure in the world called a queue, which
2,931.38
2.65
is exactly what you would hope the Apple store line would be,
2,934.03
3.1
a line whereby it's first in, first out.
2,937.13
3.41
So, the first person there three days before,
2,940.54
1.875
at 5:00 AM gets his or her phone first, and the one person
2,942.415
3.325
who comes in at 9:01 AM doesn't get their phone
2,945.74
2.26
because they're at the last position in the queue or the list.
2,948
3.015
And a queue, nicely enough, might just have at least two operations--
2,951.015
2.875
enqueue and dequeue whereby enqueue means get
2,953.89
2.84
into line d queue means get out of the line,
2,956.73
2.32
but these happen at different places.
2,959.05
1.604
For instance, if there's a whole bunch of people lined up here on the stage,
2,960.654
3.166
closest over there's the front of the list.
2,963.82
1.52
I get here last.
2,965.34
0.84
I enqueue myself at the end of this data structure,
2,966.18
3.34
but you dequeue someone from the beginning of the list.
2,969.52
2.63
By contrast, when we had a stack, when you push someone onto the stack,
2,972.15
3.97
you pop it off, or him or her off first by nature
2,976.12
4.5
of it being a LIFO data structure.
2,980.62
2.82
So, how might we implement a queue?
2,983.44
2.7
It's actually slightly more complicated, 50% more
2,986.14
3.59
pieces of information you need to keep track of, the front of the list.
2,989.73
3.25
But you can still do it in an array.
2,992.98
2.76
So, suppose that we do use an array, and let me go ahead and draw this
2,995.74
3.07
as follows.
2,998.81
0.97
Suppose that like hopscotch we draw the queue for an Apple Store
2,999.78
6.37
like an array like this.
3,006.15
1.85
And here is the door of the Apple store, so you want to be at location zero,
3,008
3.96
ideally.
3,011.96
0.5
1, 2, 3, 4, 5, 6-- so this is how many people
3,012.46
3.77
can fit into our queue in this case.
3,016.23
2.64
So, suppose that Alice wants to buy an iPhone and she gets to the store first.
3,018.87
4.32
Where should she go to keep things fair?
3,023.19
2.76
This is the queue, so we don't want to put her into the corner,
3,025.95
3.304
so to speak, in our first example.
3,029.254
1.416
We want to put her at the front of the list.
3,030.67
1.833
So, Alice belongs right there, pretty straightforward.
3,032.503
3.367
Now, Bob arrives and he comes in slightly after Alice,
3,035.87
2.62
so he gets to get behind Alice in line.
3,038.49
2.36
And so Bob is there, and maybe Charlie arrives thereafter, and then so forth.
3,040.85
3.96
David maybe comes in fourth and beyond.
3,044.81
2.71
So, that's how people would queue up, so to speak.
3,047.52
2.64