List Get Middle

This is a pair exercise and must be competed in your tutorial or lab with your partner.

For this activity, you will be looking at a linked list.

Download list.h, or copy it into your current directory on a CSE system by running

$ cp /web/cs1511/17s2/week11/files/list.h .

Make sure you understand the list and node data structures before beginning this task.

Create a file called listGetMiddle.c that includes the list.h header file. In it, you should implement listGetMiddle, a function which takes a linked list and returns the value of the middle item in the list, which is calculated with this formula (assuming the list is counted from 0, as with arrays):

int middle = listLength(l) / 2;

It should assume that it is not given an empty list. It should have this prototype:

int listGetMiddle (List l);

You should write your own tests in a separate file; listGetMiddle.c should not contain a main.

To run some simple automated tests:

$ 1511 autotest listGetMiddle

To run Styl-o-matic:

$ 1511 stylomatic listGetMiddle.c
Looks good!

You’ll get advice if you need to make changes to your code.

Submit your work with the give command, like so:

$ give cs1511 wk11_listGetMiddle

Or, if you are working from home, upload the relevant file(s) to the wk11_listGetMiddle activity on Give Online.