Week 09 Tutorial Questions

    Assignment 2 Checkin

  1. How are you progressing with the assignment?

  2. Do you have any questions? Have you learned anything that would be useful to share with the rest of the tutorial?

  3. Free

  4. What does free do?

  5. What is the input to free and how does it help it do what it needs to do?

  6. What is a use after free error? Give an example.

  7. Why are these one of the worst causes of bugs in C programs?

  8. What is a memory leak? What does dcc --leak-check do?

  9. 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);
    
  10. More Linked Lists

  11. 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;
    }
    
  12. 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.

  13. 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 call malloc to create a new linked list of the same length and which contains the same data.

  14. 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?

  15. 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.

  16. 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 call malloc to create the nodes of the new linked list.

  17. 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 question
    cinema.c
    cinema.h
    main.c
  18. Implement all the functions in cinema.c. To help test your code, a main.c has been provided for input/ouput. To help you visualise the structure of a cinema, a diagram has been provided below.