Practice Exercise

Zipping Two Lists

Your task is to write a function, listZip, that takes two lists and zips their values into a new list by alternating taking values from each of the lists. For example, if the two lists are [7, 5, 2, 9] and [4, 8, 1, 6], the function should return the list [7, 4, 5, 8, 2, 1, 9, 6]. If one of the lists is longer than the other, then the excess values in the longer list should simply be appended to the resulting list.

In the general case, listZip takes two lists \( [a_1, a_2, ... , a_n] \) and \( [b_1, b_2, ... , b_m] \), and produces a new list as shown below:

Assumptions and Constraints

Download

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

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

If you're working at home, download listZip.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
testListZip.c Contains the main function, which reads in two lists from standard input, calls listZip, and prints out the result.
listZip.c Contains listZip, 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

./testListZip
Enter list 1: 1 3 5 7
Enter list 2: 8 6 4 2

List 1: [1] -> [3] -> [5] -> [7] -> X
List 2: [8] -> [6] -> [4] -> [2] -> X
Zipped: [1] -> [8] -> [3] -> [6] -> [5] -> [4] -> [7] -> [2] -> X
./testListZip
Enter list 1: 7 2 8
Enter list 2: 5 1 4 9 3 6

List 1: [7] -> [2] -> [8] -> X
List 2: [5] -> [1] -> [4] -> [9] -> [3] -> [6] -> X
Zipped: [7] -> [5] -> [2] -> [1] -> [8] -> [4] -> [9] -> [3] -> [6] -> X
./testListZip
Enter list 1: 4 3 7 2 9
Enter list 2: 6 1

List 1: [4] -> [3] -> [7] -> [2] -> [9] -> X
List 2: [6] -> [1] -> X
Zipped: [4] -> [6] -> [3] -> [1] -> [7] -> [2] -> [9] -> X
./testListZip
Enter list 1: 
Enter list 2: 1 7 3 8 5 2 4

List 1: X
List 2: [1] -> [7] -> [3] -> [8] -> [5] -> [2] -> [4] -> X
Zipped: [1] -> [7] -> [3] -> [8] -> [5] -> [2] -> [4] -> X

Testing

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

make                           # compiles the program
./testListZip                  # tests with manual input, outputs to terminal
./testListZip < input-file     # tests with input from a file, outputs to terminal
./testListZip < 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.