Practice Exercise
List is Ordered
Your task is to write a function, listIsOrdered
, that determines whether a linked list is sorted in either ascending or descending order. It should return true if the list is sorted in ascending or descending order, and false otherwise. Your function should not modify the list. An empty list is considered to be sorted.
Assumptions and Constraints
- You must not use arrays.
- You must not use any variant of
malloc
, either directly or indirectly. - You must not modify the given list.
Download
While in your practice exercises directory, run the following command:
unzip /web/cs2521/practice-exercises/lists/listIsOrdered/downloads/listIsOrdered.zip
If you're working at home, download listIsOrdered.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 |
testListIsOrdered.c | Contains the main function, which reads in a list from standard input, calls listIsOrdered , and prints out the result. |
listIsOrdered.c | Contains listIsOrdered , 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
./testListIsOrdered Enter list: 2 4 5 5 8 9 listIsOrdered returned TRUE
./testListIsOrdered Enter list: 2 4 7 5 4 listIsOrdered returned FALSE
./testListIsOrdered Enter list: 9 8 7 2 listIsOrdered returned TRUE
./testListIsOrdered Enter list: listIsOrdered returned TRUE
Testing
You can compile and test your function using the following commands:
make # compiles the program ./testListIsOrdered # tests with manual input, outputs to terminal ./testListIsOrdered < input-file # tests with input from a file, outputs to terminal ./testListIsOrdered < 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.