COMP1511 17s2
— Lecture 20 —
The Missing Link
Andrew Bennett
<andrew.bennett@unsw.edu.au>
more linked lists
stacks + queues
recursion
Andrew Bennett
<andrew.bennett@unsw.edu.au>
more linked lists
stacks + queues
recursion
we’re nearly there…
assignment 2 in progress
player.c released this week
don’t forget to check SPOTS
MandelbrArt voting is up! (ends Sunday 23:59:59 this week)
what if… we had multiple lists?
comparing two lists?
combining two lists?
appending one list onto the other?
memory allocation can fail
you could be passed a NULL pointer
memory allocation can fail
Node newNode () {
Node new = calloc(1, sizeof (struct _node));
// What if calloc fails?
new->value = 17;
}
you could be passed a NULL pointer
void someListFunction(List list) {
// What if list is NULL??
list->head->value = 17;
}
memory allocation can fail
Node newNode () {
Node new = calloc(1, sizeof (struct _node));
// What if calloc fails?
if (new == NULL) {
// ... do something.
}
new->value = 17;
}
you could be passed a NULL pointer
void someListFunction(List list) {
// What if list is NULL??
assert (list != NULL);
list->head->value = 17;
}
list == NULL vs
list->head == NULL
one of these is bad
one of these is a valid case to consider
[ ] -> X
vs
[ ] -> 3 -> 1 -> 4 -> X
in order to understand recursion,
you must first understand recursion
in C programming: a function that calls itself
int doSomething (int input) {
return doSomething (input);
}
n! = n * n-1 * n-2 * n-3 * … * 2 * 1
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
base case: when to stop
recursive case: how to keep going
base case:
when to stop
factorial?
1! = 1
recursive case:
how to keep going
factorial?
n! = n * n-1!
base case:
when to stop
fibonacci?
fib(0) = fib(1) = 1
recursive case:
how to keep going
fibonacci?
fib(n) = fib(n-1) + fib(n-2)
some things can be a lot easier with recursion