comp1511 18s1 - webcms3.cse.unsw.edu.au€¦ · welcome! 0 comp1511 18s1 programming fundamentals....

24
0 Welcome! COMP1511 18s1 Programming Fundamentals

Upload: others

Post on 25-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

0

Welcome!COMP1511 18s1

Programming Fundamentals

Page 2: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

1COMP1511 18s1 — Lecture 17 —

Linked ListsAndrew Bennett

<[email protected]>

Page 3: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

2

Overviewafter this lecture, you should be able to…

have a better understanding of linked lists

write code to create a linked list

write code to traverse a linked list

solve simple problems using linked lists

(note: you shouldn’t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember: programming is

like learning any other language, it takes consistent and regular practice.)

Page 4: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

3

Admin

Don’t panic!assignment 2

(if you haven’t started yet, start ASAP) deadline extended to Sunday 13th May

assignment 1 tutor marking/feedback in progress

week 9 weekly test out now

don’t forget about help sessions! see course website for details

Page 5: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

4

Help SessionsWednesday

6-8pm, J17 201

Thursday 6-8pm, J17 201

Friday 10am-12pm, Brass Lab (J17 Level 3)

2pm-4pm, Brass Lab (J17 Level 3) 4pm-6pm, Oboe Lab (J17 Level 3)

note: Brass Lab = Bugle/Horn

Page 6: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

5

a quick recap of yesterday

Page 7: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

6

The node struct

struct node {

int data;

struct node *next;

};

Page 8: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

7

Interacting with a node struct

struct node {

int data;

struct node *next;

};

// "struct node hello" (no *)

// "hello" is an actual node in the function's memory

struct node hello;

hello.data = 10;

hello.next = NULL;

// in the function's memory

// ______

// hello | 10 |

// |------|

// | NULL |

// |______|

Page 9: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

8

Making a new node

// Allocates memory for a new node; returns its address

struct node *make_node(int value) {

struct node *new = malloc(1 * sizeof(struct node));

new->data = value;

new->next = NULL;

return new;

}

// "struct node * hello"

// "hello" is a pointer to a node,

// it just stores the _address_

// (of the memory we get from malloc)

struct node *hello = make_node(10);

// in the heap (malloced memory)

// ______

// hello | 10 |

// |------|

// | NULL |

// |______|

Page 10: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

9

Freeing a node

// In accordance with Newton's 3rd Law of Memory Allocation

// "For every malloc, there is an equal and opposite free"

void free_node(struct node *node) {

free(node);

}

struct node *hello = make_node(10);

free_node(hello);

Page 11: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

10

Node pointers vs allocated nodesreference to a node

arrow

struct node *curr ...

vs

making (allocating) a new node circle

... = malloc(1 * sizeof(struct node));

Page 12: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

11

Node pointers vs allocated nodesreference to a node

arrow

struct node *curr ...

vs

making (allocating) a new node circle

... = malloc(1 * sizeof(struct node));

Page 13: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

12

Node pointers vs allocated nodesreference to a node (arrow) vs

making (allocating) a new node (circle)

Page 14: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

13

array/list “traversal”(going through every element)

Page 15: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

14

Traversing… an Array

void fillArray (int array[ARRAY_SIZE], int value) {

int i = 0;

while (i < ARRAY_SIZE) {

array[i] = value; // set the value

i++; // move to next element

}

}

Page 16: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

15

Traversing… a Linked List

void fillList (struct node *list, int value) {

struct node *curr = list;

while (curr != NULL) {

curr->data = value; // set the value

curr = curr->next; // move to next node

}

}

Page 17: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

16and now for today’s content…

Page 18: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

17

The Standard List Loop

struct node *curr = list;

while (curr != NULL) {

?????

curr = curr->next;

}

Page 19: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

18

The Standard List Loop – List LengthHow can we calculate the length of a list?

i.e. how many nodes are in the list

struct node *curr = list;

int num_nodes = 0;

while (curr != NULL) {

num_nodes += 1;

curr = curr->next;

}

Page 20: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

19

The Standard List Loop – List SumHow can we sum all of the elements in a list?

i.e. add the values of all of the nodes together

struct node *curr = list;

// int num_nodes = 0;

?????

while (curr != NULL) {

// num_nodes += 1;

?????

curr = curr->next;

}

Page 21: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

20

Inserting Into a Listadding new nodes to our list….

insert at the start

insert at the end

insert in the middle

Page 22: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

21

An aside: When things go wrongwhat if our list is empty?

what would this look like in code?

Page 23: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

22

An aside: Function Commentsit’s important to document your functions:

what do they assume?

what does the caller need to do?

Page 24: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17

23

Building Blockswe can construct complex list operations out of simple functions