-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_Lists.en.tex
436 lines (360 loc) · 15.5 KB
/
Linked_Lists.en.tex
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
%******************************************************************************%
% %
% sample.en.tex for LaTeX %
% Created on : Tue Mar 10 13:27:28 2015 %
% Made by : David "Thor" GIRON <thor@42.fr> %
% %
%******************************************************************************%
\documentclass{42-en}
%******************************************************************************%
% %
% Header %
% %
%******************************************************************************%
\begin{document}
\title{Linked Lists}
\subtitle{The Archnemesis of Arrays}
\member{tingo -}{tingo@student.42.us.org}
\member{jchung -}{jchung@student.42.us.org}
\member{42 Staff -}{pedago@42.fr}
\summary {
This is an introduction to \texttt{Linked Lists}.
}
\maketitle
\tableofcontents
%******************************************************************************%
% %
% Foreword %
% %
%******************************************************************************%
\chapter{Foreword}
\begin{center}
\textbf{Rick Astley - Never Gonna Give You Up}
\end{center}
We're no strangers to love\\
You know the rules and so do I\\
A full commitment's what I'm thinking of\\
You wouldn't get this from any other guy\\
I just wanna tell you how I'm feeling\\
Gotta make you understand\\
\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry\\
Never gonna say goodbye\\
Never gonna tell a lie and hurt you\\
\\
We've known each other for so long\\
Your heart's been aching but you're too shy to say it\\
Inside we both know what's been going on\\
We know the game and we're gonna play it\\
And if you ask me how I'm feeling\\
Don't tell me you're too blind to see\\
\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry\\
Never gonna say goodbye\\
Never gonna tell a lie and hurt you\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry\\
Never gonna say goodbye\\
Never gonna tell a lie and hurt you\\
Never gonna give, never gonna give\\
\\
(Give you up)\\
(Ooh) Never gonna give, never gonna give\\
(Give you up)\\
\\
We've known each other for so long\\
Your heart's been aching but you're too shy to say it\\
Inside we both know what's been going on\\
We know the game and we're gonna play it\\
I just wanna tell you how I'm feeling\\
Gotta make you understand\\
\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry\\
Never gonna say goodbye\\
Never gonna tell a lie and hurt you\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry\\
Never gonna say goodbye\\
Never gonna tell a lie and hurt you\\
Never gonna give you up\\
Never gonna let you down\\
Never gonna run around and desert you\\
Never gonna make you cry
%******************************************************************************%
% %
% Introduction %
% %
%******************************************************************************%
\chapter{Introduction}
Today's lecture gave us a general overview of linked lists and their
uses in general coding practice. Linked lists are the foundation for
more advanced data structures and are commonly used in place of arrays
for data storage due to their ability to store more information in
non-contiguous blocks of memory. This makes linked lists a very powerful
tool for parallel computation.\\
\\
There are many applications to the different variations of linked lists.
Many games will store cycling character animations in a circular linked
list or a circular array. Many operating systems line up processes and
jobs in a queue. The desire to simulate genetic mutation and give people
superpowers by removing and swapping base pairs in DNA strands can best
be organized by a doubly linked list.\\
\\
Within the 42 curriculum, the entire graphics branch frequently utilizes
linked lists to store coordinates of points for an object in a map or
rendering. In many Unix projects, making a process and job queue for
operating systems is essential to not making a computer crash.
%******************************************************************************%
% %
% Goals %
% %
%******************************************************************************%
\chapter{Goals}
This project lays groundwork for all of the data structures you will learn
in the following days. Today, you will learn how to implement your very own
data structure. You will be responsible for creating the basic functions
that allow a data structure to access values, add values, remove values, and
modify itself in fun and interesting ways.\\
\\
This project is a simple gateway into the larger world of Data Structures
%******************************************************************************%
% %
% General instructions %
% %
%******************************************************************************%
\chapter{General instructions}
The following exercises are designed such that written functions are
to be turned in as standalone functions; they will not be included
in the class for LinkedLists but will still perform standardized
operations. All exercises should be written in Python and no external
function calls should be made except for the first question; only the
functions supplied in the classes provided to you should be needed.\\
\\
Both a \texttt{Node} and \texttt{SinglyList} class have been given to
you for use. The \texttt{SinglyList} class has an in-class iterator defined to
traverse a list, but you are free to iterate through the list yourself :)\\
\\
If a function requires the use of the Node or SinglyList class, do not
include that code in your file submission. If you use a previous solution
please make sure to include that function in the file submission.
\newpage
\section{Node Class}
\begin{42pycode}
class Node(object):
def __init__(self, content):
if content is None:
raise ValueError('Node must have content')
self.c = content
self.n = None
@property
def content(self):
return self.c
@content.setter
def content(self, val):
self.c = val
@property
def next(self):
return self.n
@next.setter
def next(self, val):
self.n = val
\end{42pycode}
\section{SinglyList Class}
\begin{42pycode}
class SinglyList(object):
def __init__(self):
self.h = None
def __iter__(self):
current = self.head
while current:
yield current
current = current.next
@property
def head(self):
return self.h
@head.setter
def head(self, val):
self.h = val
def isEmpty(self):
return self.head == None
def add_head(self, node):
if self.isEmpty():
self.head = node
else:
node.next = self.head
self.head = node
\end{42pycode}
\startexercices
\chapter{Exercise\exercicenumber: Print All Nodes in a List}
\extitle{Print All Nodes in a List}
\exnumber{\exercicenumber}
\exfiles{print\_list.py}
\exforbidden{Everything except print() :D}
\exnotes{n/a}
\makeheaderfiles
Understanding how to traverse a linked list is important to mastering
its concept. Given the head node of a singly linked list, print out all
the items in the list.\\
\\
Your function will be defined as such:\\
\texttt{print\_list(list\_head)}\\
\\
\textbf{Input Format}
Your function will take the head node of a list.\\
\\
\textbf{Output Format}
Nothing special. Just print the items of the list in the order they appear.
\nextexercice
\chapter{Exercise\exercicenumber: Add an Item to the End of a List}
\extitle{Add an Item to the End of a List}
\exnumber{\exercicenumber}
\exfiles{add\_tail.py}
\exforbidden{Everything :D}
\exnotes{There is a similar function in the SinglyList class to reference :)}
\makeheaderfiles
You're given the pointer to the head node of a singly linked list and
the value of a node to add to the list. Create a new node with the given
value. Insert this node at the tail of the linked list and return the head
node after the insertion.\\
\\
Your function will be defined as such:\\
\texttt{add\_tail(list\_head, val)}\\
\\
\textbf{Input Format}
Your function will take the head node of a list and a value.\\
\\
\textbf{Output Format}
There will be no return, but the list must now cotain a new node with the specified value at the end of it.
\nextexercice
\chapter{Exercise\exercicenumber: Remove an Item From a List}
\extitle{Remove an Item From a List}
\exnumber{\exercicenumber}
\exfiles{remove.py}
\exforbidden{Everything :D}
\exnotes{If the list is empty, head will be null. The list will not contain duplicates.}
\makeheaderfiles
You're given the pointer to the head node of a singly linked list and
the value of a node to delete from the list. Delete the node with the given
value if it exists.\\
\\
Your function will be defined as such:\\
\texttt{remove(list\_head, val)}\\
\\
\textbf{Input Format}
Your function will take the head node of a list and a value.\\
\\
\textbf{Output Format}
There will be no return, but the list must no longer contain the node with that value.
\nextexercice
\chapter{Exercise\exercicenumber: Cycle Detection}
\extitle{Cycle Detection}
\exnumber{\exercicenumber}
\exfiles{has\_cycle.py}
\exforbidden{Everything :D}
\exnotes{If the list is empty, head will be null.}
\makeheaderfiles
In a turn-based multiplayer game, a linked list can be used to cyclically
repeat player order by having the last player's \texttt{next} reference the
first player. Check that a given linked list cycles or not.\\
\\
Your function will be defined as such:\\
\texttt{has\_cycle(list\_head)}\\
\\
\textbf{Input Format}
Your function will take the head node of a list.\\
\\
\textbf{Output Format}
It will return True of there is a cycle and False if there is no cycle.
\nextexercice
\chapter{Exercise\exercicenumber: Merge Two Lists}
\extitle{Merge Two Lists}
\exnumber{\exercicenumber}
\exfiles{merge.py}
\exforbidden{Everything :D}
\exnotes{All trains have at least one car, and no two cars have the same weight.}
\makeheaderfiles
Two cargo trains arrive in the trainyard and their cargo needs to be
consolidated into a single train set before it can depart. Both trains
were organized with the heaviest car in the front of the train
descending to the lightest car in the back.\\
\\
Your function will be defined as such:\\
\texttt{merge(train1, train2)}\\
\\
\textbf{Input Format}
Your function will take two list heads, \textit{train1 and train2}.\\
\\
\textbf{Output Format}
Return the head of the new merged train.
\nextexercice
\chapter{Exercise\exercicenumber: Sort a Linked List}
\extitle{Sort a Linked List}
\exnumber{\exercicenumber}
\exfiles{sort\_asc.py}
\exforbidden{Everything :D}
\exnotes{There is no limitation on which sort to implement for this exercise. You will find some sorts are easier to work into a linked list than others...}
\makeheaderfiles
Understanding how to organize information in a list is important. Implement
a sorting algorithm that organizes a linked list with numbers in
\textbf{ascending} order.\\
\\
Your function will be defined as such:\\
\texttt{sort\_asc(unsorted\_list)}\\
\\
\textbf{Input Format}
Your function will take the head node of an unsorted list.\\
\\
\textbf{Output Format}
Sort the list in function, but return nothing.
\chapter{Bonus part}
Now that you've had a chance to get your feet wet with the linked list, let's
revisit a previous problem. The traiyard has run into some more trouble.
See if you can help them.
\warn{This will only count if and only if you have completed the previous excercises correctly. Go back now to make sure.}
\nextexercice
\chapter{Exercise\exercicenumber: Trainyard Revisited}
\extitle{Trainyard Revisited}
\exnumber{\exercicenumber}
\exfiles{trainyard.py}
\exforbidden{Everything :D}
\exnotes{All trains have at least one cart, and two carts \textbf{may} have the same weight.}
\makeheaderfiles
Two more cargo trains arrive in the trainyard and need to be merged before
departure. These two trains are no longer organized by weight from heaviest
to lightest prior to the merge. The two lightest cars must also stay behind
at the trainyard for inspection and leave with the next incoming train.
Take into account cars with equal weights.\\
\\
Your function will be defined as in excercise 04.\\
\\
\textbf{Input Format}
\textit{Same as 04}\\
\\
\textbf{Output Format}
\textit{Same as 04}, but missing the lightest 2 and sorted.
%******************************************************************************%
% %
% Turn-in and peer-evaluation %
% %
%******************************************************************************%
\chapter{Turn-in and peer-evaluation}
This project will be evaluated by your peers and the moulinette. Submit to
the \texttt{GIT} repository as usual. Only work in your repository will be
graded. Make sure you have the \textit{exact} function names as shown in
this document. Only the specified functions will be looked at and graded.
Remember, the bonus will only be evaluated if \textit{all} the previous
excercises are correct.
\end{document}