Practice Exercise
Reversing a Doubly Linked List
Your task is to write a function, reverseDLList
, that reverses a given doubly linked list. You should not change the values in any nodes or create any new nodes - instead, you should just rearrange the nodes and pointers of the given list.
Assumptions and Constraints
- You must not use arrays.
- You must not use any variant of
malloc
, either directly or indirectly. - You must not change the values in any nodes.
Download
While in your practice exercises directory, run the following command:
unzip /web/cs2521/practice-exercises/lists/reverseDLList/downloads/reverseDLList.zip
If you're working at home, download reverseDLList.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 |
testReverseDLList.c | Contains the main function, which reads in a list from standard input, calls reverseDLList , and prints out the result. |
reverseDLList.c | Contains reverseDLList , 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
./testReverseDLList Enter list: 2 3 5 7 11 Original list: Size: 5 Forwards: [2] -> [3] -> [5] -> [7] -> [11] -> X Backwards: [11] -> [7] -> [5] -> [3] -> [2] -> X Reversed list: Size: 5 Forwards: [11] -> [7] -> [5] -> [3] -> [2] -> X Backwards: [2] -> [3] -> [5] -> [7] -> [11] -> X
./testReverseDLList Enter list: Original list: Size: 0 Forwards: X Backwards: X Reversed list: Size: 0 Forwards: X Backwards: X
./testReverseDLList Enter list: 1 2 7 9 2 1 Original list: Size: 6 Forwards: [1] -> [2] -> [7] -> [9] -> [2] -> [1] -> X Backwards: [1] -> [2] -> [9] -> [7] -> [2] -> [1] -> X Reversed list: Size: 6 Forwards: [1] -> [2] -> [9] -> [7] -> [2] -> [1] -> X Backwards: [1] -> [2] -> [7] -> [9] -> [2] -> [1] -> X
Testing
You can compile and test your function using the following commands:
make # compiles the program ./testReverseDLList # tests with manual input, outputs to terminal ./testReverseDLList < input-file # tests with input from a file, outputs to terminal ./testReverseDLList < 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.