-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlinear.html
317 lines (306 loc) · 10.2 KB
/
linear.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Linear Structures</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/reveal.css">
<link id="theme" rel="stylesheet" href="css/theme/black.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/monokai.css">
<script src="js/diagrams.js"></script>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="center">
<h1>Linear Data Structures</h1>
</section>
<!-- Linked Lists -->
<section>
<section>
<h2>The Linked List</h2>
<h5>An alternative to the array</h5>
<canvas id="test" height="500" width="1000"></canvas>
<script>
var canvas = document.getElementById('test')
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "red";
var list = new LinkedList();
function appendTest() {
console.log("APPEND");
list.append(1);
ctx.clearRect(0, 0, canvas.width, canvas.height);
list.draw(ctx, true, false);
}
appendTest();
appendTest();
</script>
</section>
<section>
<h3>Motivation</h3>
<table>
<thead>
<tr>
<th>Structure</th>
<th>Array</th>
<th>Dynamic <br>Array</th>
</tr>
</thead>
<tbody>
<tr>
<td>Prepend</td>
<td>O(n)</td>
<td>O(n)</td>
</tr>
<tr>
<td>Insert</td>
<td>O(n)</td>
<td>O(n)</td>
</tr>
<tr>
<td>Append</td>
<td>O(n)</td>
<td>O(1)</td>
</tr>
<tr>
<td>Remove First</td>
<td>O(n)</td>
<td>O(n)</td>
</tr>
<tr>
<td>Remove Index</td>
<td>O(n)</td>
<td>O(n)</td>
</tr>
<tr>
<td>Remove Last</td>
<td>O(n)</td>
<td>O(1)</td>
</tr>
<tr>
<td>Random Access</td>
<td>O(1)</td>
<td>O(1)</td>
</tr>
</tbody>
</table>
</p>
</section>
<section>
<h3>How</h3>
<p>What if we stored memory non-contiguously?</p>
<canvas class="fragment" id="LL" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById('LL');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
drawNode(ctx, [400, 50], null, null);
drawNode(ctx, [600, 150], null, null);
drawNode(ctx, [100, 300], null, null);
drawNode(ctx, [700, 400], null, null);
</script>
<p class="fragment">Unlike arrays, in linked lists, data is stored <br><em>non-contiguously</em> in
these nodes</p>
</section>
<section>
<h3>Singly Linked List</h3>
<p>Where nodes only contain <code>data</code> and <code>*next</code></p>
<canvas id="SLL" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById('SLL');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
var n = drawNode(ctx, [100, 150], undefined, 290);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 485);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 680);
n = drawNode(ctx, [n[0] + 100, 150], undefined, null);
</script>
</section>
<section>
<h3>Circular Singly Linked List</h3>
<p>Where nodes only contain <code>data</code> and <code>*next</code>, but no node points to null</p>
<canvas id="CSLL" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById('CSLL');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
var n = drawNode(ctx, [100, 150], undefined, 290);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 485);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 680);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 100);
</script>
</section>
<section>
<h3>Doubly Linked List</h3>
<p>Where nodes only contain <code>data</code> and <code>*next</code>, but no node points to null</p>
<canvas id="DLL" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById('DLL');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
var n = drawNode(ctx, [50, 50], null, 270);
n = drawNode(ctx, [n[0] + 100, 50], n[0] + 5, 495);
n = drawNode(ctx, [n[0] + 100, 50], n[0] + 5, 720);
n = drawNode(ctx, [n[0] + 100, 50], n[0] + 5, null);
</script>
</section>
<section>
<h3>Circular Doubly Linked List</h3>
<p>Where nodes only contain <code>data</code> and <code>*next</code>, but no node points to null</p>
<canvas id="CDLL" width="1000" height="500"></canvas>
<script>
var canvas = document.getElementById('CDLL');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
var n = drawNode(ctx, [50, 150], 850, 270);
n = drawNode(ctx, [n[0] + 100, 150], n[0] + 5, 495);
n = drawNode(ctx, [n[0] + 100, 150], n[0] + 5, 720);
n = drawNode(ctx, [n[0] + 100, 150], n[0] + 5, 50);
</script>
</section>
</section>
<!-- Stacks -->
<section>
<section class="center">
<h2>Stacks</h2>
<h5>Last In First Out</h5>
</section>
</section>
<!-- Queues -->
<section>
<section class="center">
<h2>Queues</h2>
<h5>First in First Out</h5>
</section>
<section>
<h3>Queues: Required Methods</h3>
<ul>
<li>Enqueue (push)</li>
<li>Dequeue (pop)</li>
<li>Is Empty</li>
</ul>
<p class="fragment">What should the underlying structure be?</p>
</section>
<section>
<h3>Queues: SLL Backend Visual</h3>
<canvas id="queue" height="500" width="1000"></canvas>
<script>
var canvas = document.getElementById('queue');
var ctx = canvas.getContext('2d');
var color = "red";
ctx.lineWidth = 3;
ctx.strokeStyle = color;
ctx.fillStyle = color;
var n = drawNode(ctx, [50, 150], undefined, 240);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 435);
n = drawNode(ctx, [n[0] + 100, 150], undefined, 630);
n = drawNode(ctx, [n[0] + 100, 150], undefined, null);
</script>
</section>
<section>
<h2>Deque</h2>
<h5>Double Ended Queues</h5>
<p class="fragment">They allow <em>fast</em> pushes and pops from either end.</p>
<ul class="fragment">
<li>pushleft</li>
<li>pushright</li>
<li>popleft</li>
<li>popright</li>
</ul>
</section>
</section>
</div>
</div>
<script src="js/reveal.js"></script>
<script>
Reveal.initialize({
dependencies: [{
src: 'plugin/markdown/marked.js'
},
{
src: 'plugin/markdown/markdown.js'
},
{
src: 'plugin/notes/notes.js',
async: true
},
{
src: 'plugin/highlight/highlight.js',
async: true
},
{
src: 'plugin/math/math.js',
async: true
},
{
src: 'plugin/menu/menu.js',
async: true
},
{
src: 'plugin/chalkboard/chalkboard.js',
async: true
}
],
hash: true,
center: false,
keyboard: {
// toggle notes canvas when 'c' is pressed
67: function () {
RevealChalkboard.toggleNotesCanvas()
},
// toggle chalkboard when 'b' is pressed
66: function () {
RevealChalkboard.toggleChalkboard()
},
// clear chalkboard when 'DEL' is pressed
46: function () {
RevealChalkboard.clear()
},
// reset chalkboard data on current slide when 'BACKSPACE' is pressed
8: function () {
RevealChalkboard.reset()
},
// downlad recorded chalkboard drawing when 'd' is pressed
68: function () {
RevealChalkboard.download()
},
},
chalkboard: {
toggleChalkboardButton: {
left: "80px"
},
toggleNotesButton: {
left: "130px"
},
color: ['rgba(255, 255, 255, 1)', 'rgba(255, 255, 255, 1)']
},
menu: {
titleSelector: 'h2',
hideMissingTitles: true,
}
});
</script>
</body>
</html>