List Insert Nth

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 listInsertNth.c that includes the list.h header file. In it, you should implement listInsertNth, a function which takes a linked list and two integers, n and val, and creates a new node with the vlaue of val and places it in the nth position in the list (counting from 0 as with arrays). It should assume that it is given a positive integer less than or equal to the length of the list. It should have this prototype:

void listInsertNth (List l, int n, int val);

For example, if you had a list l, with the values 1 -> 2 -> 3 -> X and you ran this snippet of code:

listInsertNth (l, 2, 17);
listPrint (l);

The output should be 1 -> 2 -> 17 -> 3 -> X.

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

To run some simple automated tests:

$ 1511 autotest listInsertNth

To run Styl-o-matic:

$ 1511 stylomatic listInsertNth.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_listInsertNth

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