Practice Exercise

Deleting Duplicates from a Linked List

Your task is to write a function, listDeleteDuplicates, that deletes all duplicate values from a given list. For each value that occurs more than once in the list, every instance of the value except the first should be deleted.

Assumptions and Constraints

Download

While in your practice exercises directory, run the following command:

unzip /web/cs2521/practice-exercises/lists/listDeleteDuplicates/downloads/listDeleteDuplicates.zip

If you're working at home, download listDeleteDuplicates.zip by clicking on the above link and then unzip the downloaded file.

Files

list.c Contains the implementation of basic list functions
list.h Contains the definition of the list data structure and function prototypes
testListDeleteDuplicates.c Contains the main function, which reads in a list from standard input, calls listDeleteDuplicates, and prints out the result.
listDeleteDuplicates.c Contains listDeleteDuplicates, the function you must implement
Makefile A makefile to compile your code
tests/ A directory containing the inputs and expected outputs for some basic tests
autotest A script that uses the tests in the tests directory to autotest your solution. You should only run this after you have tested your solution manually.

Examples

./testListDeleteDuplicates
Enter list: 1 2 3 4 5 6 1

Original list: [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [1] -> X
After deleting duplicates: [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> X
./testListDeleteDuplicates
Enter list: 1 2 2 3 3 3 4 4 4 4

Original list: [1] -> [2] -> [2] -> [3] -> [3] -> [3] -> [4] -> [4] -> [4] -> [4] -> X
After deleting duplicates: [1] -> [2] -> [3] -> [4] -> X
./testListDeleteDuplicates
Enter list: 1 2 3 4 5 5 4 3 2 1

Original list: [1] -> [2] -> [3] -> [4] -> [5] -> [5] -> [4] -> [3] -> [2] -> [1] -> X
After deleting duplicates: [1] -> [2] -> [3] -> [4] -> [5] -> X

Testing

You can compile and test your function using the following commands:

make                                        # compiles the program
./testListDeleteDuplicates                  # tests with manual input, outputs to terminal
./testListDeleteDuplicates < input-file     # tests with input from a file, outputs to terminal
./testListDeleteDuplicates < tests/01.in    # for example, tests with input from tests/01.in
                                              # (then manually compare with tests/01.exp)

After you have manually tested your solution, you can autotest it by running ./autotest. This will run some basic tests on your program, as well as check for memory leaks/errors.

It is possible to devise your own tests by creating your own input files. See the existing input files for examples. Note that you will need to check the output yourself.