Week 01 Tutorial Questions

Recursion

If you need a refresher, we encourage you to take a look at last term's COMP1511 lectures, available on YouTube here!
Recursion

For the questions below, use the following data type:

struct node {
	int value;
	struct node *next;
};
  1. Write a recursive function to compute the length of a linked list. It should have this signature:

    int listLength(struct node *l) { ... }
    
  2. Write a recursive function to count the number of odd numbers in a linked list. It should have this signature:

    int listCountOdds(struct node *l) { ... }
    
  3. Write a recursive functions to check whether a list is sorted in ascending order. It should have this signature:

    bool listIsSorted(struct node *l) { ... }
    
  4. Write a recursive function to delete the first instance of a value from a linked list, if it exists. The function should return a pointer to the beginning of the updated list. Use the following interface:

    struct node *listDelete(struct node *l, int value) { ... }
    
  5. Recall that an alternative representation of a linked list uses a struct that contains a pointer to the first node of the list:

    struct list {
    	struct node *head;
    };
    

    How would your recursive solutions for the above questions change if the functions took a struct list pointer instead of a node pointer? For example:

    int listLength(struct list *l);