Print a singly linked list recursively

advertisement
Recursively Print A Linked List
The goal for this exercise is to practice both recursion, and operations on linked lists
Note: the tests for this exercise will display their output in the NUnit "Errors and
Failures" tab, and will NOT display anything into the "Text Output" tab.
What you need to do for this exercise:
1. For this exercise, you need to implement the RecursivelyPrintForward method,
the RecursivelyPrintBackward method, and the RecursivelyPrint(bool
forwards) method, in the provided MyLinkedList class.
a. This class should be found in the Student_Answers.cs file, in a project
named something like 03_PCE_StudentCode. In the starter project, in the
OO_Linked_List project, you'll find a class named MyLinkedList.
b. For this exercise, you should be able to reuse the code you wrote in prior
lectures/exercises. You’ll need to be able to add nodes to the list so that
you can test the code you’ll write for this exercise ; adding nodes only to the
front of the list is fine.
2. Create a method named RecursivelyPrintForward on your linked list class, which
will print out all the values stored in the list.
a. Note that you may need a public RecursivelyPrintForward method that
does a couple of one-time actions, and then passes control to a private
method (perhaps named something like RecursivelyPrintFwd) that actually
does the recursion.
b. This should print the items in the same order that they appear in the nodes
in the list. So if your list contains the values 1, 2, 3 (in that order), this
method should print out each ‘data’ field, ONE per line, like so:
1
2
3
If there are no items in the list, this method should not print out anything.
c. Once you’ve completed the above task, you should run the tests in the
NUnit_Tests_LL_RecursivelyPrintForward class. They should all pass at
this point, and if not, then you should fix your program so that those tests do
pass. You can run the tests using the NUnit support in the provided starter
project. You should feel free to supplement that code with your own test
cases if you wish, but you are neither required nor expected to.
3. Create a method named RecursivelyPrintBackward on your linked list class,
which will print out the contents of the list in reverse order.
a. Just like you did with the RecursivelyPrintForward method, you should use
the approach of having 1 public method, that will then call 1 private method,
which actually does the recursion.
b. This should print the items in the reverse order that they appear in the
nodes in the list. So if your list contains the values 1, 2, 3 (in that order),
this method should print out each ‘data’ field, ONE per line, like so:
3
2
1
If there are no items in the list, this method should not print out anything.
c. Once you’ve completed the above task, you should run the tests in the
NUnit_Tests_LL_RecursivelyPrintBackward class. They should all pass at
this point, and if not, then you should fix your program so that those tests do
pass. You can run the tests using the NUnit support in the provided starter
project. You should feel free to supplement that code with your own test
cases if you wish, but you are neither required nor expected to.
4. Once you've figured out how to print either way, create a method named
RecursivelyPrint(bool forwards).
a. If forwards is true, then this should print the items in the same order that
they appear in the nodes in the list. So if your list contains the values 1, 2, 3
(in that order), this method should print out each ‘data’ field, ONE per line,
like so:
1
2
3
b. If forwards is false, then this should print the items in the reverse order that
they appear in the nodes in the list. So if your list contains the values 1, 2, 3
(in that order), this method should print out each ‘data’ field, ONE per line,
like so:
3
2
1
c. If there are no items in the list, this method should not print out anything,
regardless of the value of the forwards parameter.
d. Note that you may need a public RecursivelyPrint(bool) method that does a
couple of one-time actions, and then passes control to a private
RecursivelyPrintFwdBckwd method that actually does the recursion. Feel
free to add extra parameters to the private method.
This private version of the method should contain all the logic needed to
print in either direction (i.e., you are NOT allowed to call the
RecursivelyPrintForward/ RecursivelyPrintBackwards, and neither should
you create two new methods to do this).
e. Once you’ve completed the above task, you should run the tests in the
NUnit_Tests_LL_RecursivelyPrint class. They should all pass at this point,
and if not, then you should fix your program so that those tests do pass.
You can run the tests using the NUnit support in the provided starter project.
You should feel free to supplement that code with your own test cases if you
wish, but you are neither required nor expected to.
5. Be aware that these tests may use functionality not explicitly required above (for
example, the tests may make use of the InsertAtFront/AddToFront method). If
these other functions are not properly working, then these tests may or may not
reliably pass.
Download