COMP9024 (19T0) COMP9024 (19T0) Sample Midterm Exam Data Structures and Algorithms
[Instructions] [C language]
[Q1] [q2] [Q3] [Q4] [Q5]

Question 2 (6 marks)

In the q2 directory, you will find the following files/directories:

Your task for this question is to implement the getPeaks() function in the file getPeaks.c. The getPeaks() function takes a doubly-linked list, and returns a new doubly-linked list containing all the "peaks" in the given list.

A node is a "peak" if it's value is greater than both previous node value and next node value.

The following examples show how the function works:

getPeaks( [4, 8, 10, 5] )    [10]
getPeaks( [3, 16, 55, 40, 32, 20, 22, 70, 62, 50, 80, 11] )    [55, 70, 80]
getPeaks( [4, 6, 8, 9, 12, 45] )    []
getPeaks( [2, 4, 8, 8, 5, 3] )    []

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

$ make    # builds the required program testGetPeaks
$ ./autotest  # apply all the tests in the tests directory to the program testGetPeaks
$ ./autotest <test-number>   # apply only one given test case from the tests directory to the program testGetPeaks

You can find out more about the behaviour of the testGetPeaks program by looking at the files testGetPeaks.c, autotest and the files in the tests directory. The files named tX.in are input files for the test program testGetPeaks and the corresponding expected output are in tX.exp.

If you want to run say test case 2, use the following command:

$ ./autotest 2  

If you want to run all the sample test cases provided, use the following command:

$ ./autotest   

After you run the tests, additional files will appear in the tests directory: tX.out contains the output (from your function) of running test tX.

IMPORTANT: You can add debugging code to getPeaks.c or testGetPeaks.c if you want, however, make sure that you remove it before testing and submitting, otherwise your output won't match the expected output and you'll fail all the tests. You can also add any auxiliary functions to getPeaks.c that you think are necessary.

Once you are satisfied with your program, submit it using the command:

$ submit q2

This will make a copy of the getPeaks.c file from the q2 directory as your answer for this question. You can run the submit command as many times as you like, but make sure that your final submission compiles without any errors or warnings.

Testing: Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using test cases different to the example test cases provided in the tests directory.