Programming Fundamentals
Old Revision Content
-
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 the functions
add_movie()
andprint_genre()
insidecinema.c
:add_movie()
: This function should add a newstruct movie
node to the*movies
linked list inside its correspondingstruct genre
node.print_genre()
: This function shoud print each movie associated with a given genre name. Each movie should be printed in the format: "TITLE, RATING/100 (LENGTH minutes)"
add_genre()
is provided as an example. To help you visualise the structure of the cinema, a diagram has been provided below: -
This program 'won't work'. What's wrong with it? Split off into groups and discuss why. Line numbers are included for reference.
This week's exercise has been simplified compared to previous weeks feedback
Here is a version without line numbers for easy copying1 #include <stdio.h> 2 3 struct node { 4 node next; 5 int value; 6 } 7 8 struct node *new_node(int data) { 9 struct node *new = malloc(sizeof(node *)); 10 new->data = data; 11 return new; 12 } 13 14 int main() { 15 struct node = new_node(1); 16 node->next = new_node(2); 17 node->next->next = new_node(3); 18 19 int i = 0; 20 while (node[i] != NULL) { 21 printf("%d\n", node[i].value); 22 } 23 }
#include <stdio.h> struct node { node next; int value; } struct node *new_node(int data) { struct node *new = malloc(sizeof(node *)); new->data = data; return new; } int main() { struct node head = new_node(1); node->next = new_node(2); node->next->next = new_node(3); int i = 0; while (node[i] != NULL) { printf("%d\n", node[i].value); } }
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.
To introduce you to the idea of 2-dimensional linked lists, this question invites you to picture how a cinema would work. Each cinema has various genres to show, and each genre has various movies that follow it. You will need cinema.c for the following question.
Debugging
Check in week
Reminder: This week is a check-in week, so please make sure to stay around in the lab time for your one-on-one with one of your Tutors.
Part 0: Assignment 2
In this section, we will briefly discuss how assignment 2 is going.
Part 1: Free
Freeing memory is an important part of using C, and of writing linked lists. We will quickly explore an example of what the
free
function does, and then use it in context!
Part 2: Linked List Exercises
Now, we're going to practice the use of linked lists.
You might want to download and have a look at the following file: linked_list.c.