Week 09 Tutorial Questions
-
How are you progressing with the assignment?
-
Do you have any questions? Have you learned anything that would be useful to share with the rest of the tutorial?
-
What does
free
do? -
What is the input to
free
and how does it help it do what it needs to do? -
What is a use after free error? Give an example.
-
Why are these one of the worst causes of bugs in C programs?
-
What is a memory leak? What does
dcc --leak-check
do? -
In the following example, how much memory are we leaking? How do we stop it from leaking?
int i = 0; struct node *head = NULL; while (i < 10) { // `insert_first` is a function which malloc's a new node, // inserts it at the head of the list, and // returns it. head = insert_first(i, head); i++; } free(head);
-
What is wrong with this code? How do we fix it so it
malloc
's a node?struct node *new_node(int data) { struct node *new = malloc(sizeof(struct node *)); new->data = data; new->next = NULL; return new; }
-
Implement a function
copy
which returns a copy of a linked list.copy
should have this prototype.struct node *copy(struct node *old_head);
copy
should callmalloc
to create a new linked list of the same length and which contains the same data. -
Implement a function
list_append
which creates a new list by appending the second list to the first.list_append
should have this prototype:struct node *list_append(struct node *first_list, struct node *second_list);
Why do we need to make sure it is a new list? Why can't we just change the first list's final node's next pointer to the second list's head?
-
Implement a function
identical
that returns 1 if the contents of the two linked lists are identical (same length, same values in data fields) and otherwise returns 0.identical
should have this prototype:int identical(struct node *first_list, struct node *second_list);
identical
should not create (malloc
) any new list elements. -
Implement a function
set_intersection
which given two linked lists in strictly increasing order returns a new linked list containing a copy of the elements found in both lists.set_intersection
should have this prototype:struct node *set_intersection(struct node *set1, struct node *set2);
The new linked list should also be in strictly increasing order. It should include only elements found in both lists.
set_intersection
should callmalloc
to create the nodes of the new linked list. -
Implement all the functions in
cinema.c
. To help test your code, amain.c
has been provided for input/ouput. To help you visualise the structure of a cinema, a diagram has been provided below.
Assignment 2 Checkin
Free
More Linked Lists
The rest of the tutorial questions are linked list exercises.
Your tutor will choose to cover some of these exercises, but likely will not have time to cover all of the exercises. Any exercises that are not covered can be used as revision questions for linked lists.
For these questions, struct node
has this definition:
struct node {
int data;
struct node *next;
};
To follow along with your tutor, download list.c
and
list.h
here.
2D Linked Lists
This section of the tutorial is meant to provide a guide on traversing linked lists of 2 dimensions to prepare you for later stages of assignment 2.
You will need the following files to complete this questioncinema.c
cinema.h
main.c