COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals....
Transcript of COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals....
![Page 1: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/1.jpg)
0
Welcome!COMP1511 18s1
Programming Fundamentals
![Page 3: COMP1511 18s1 - webcms3.cse.unsw.edu.au€¦ · Welcome! 0 COMP1511 18s1 Programming Fundamentals. COMP1511 18s1 1 — Lecture 17 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/3.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/4.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/5.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/6.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/7.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/8.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/9.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/10.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/11.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/12.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/13.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/14.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/15.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/16.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/17.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/18.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/19.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/20.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/21.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/22.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/23.jpg)
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 ...](https://reader036.fdocuments.in/reader036/viewer/2022063018/5fdaed113d8a1d03f87cbf2c/html5/thumbnails/24.jpg)
23
Building Blockswe can construct complex list operations out of simple functions