Recursively Print A Linked List
The goal for this
exercise
is to practice both recursion, and operations on linked lists
What you need to do for
this exercise:
- For this exercise,
you need to implement the RecursivelyPrintForward method, the
RecursivelyPrintBackward
method, and the RecursivelyPrint(bool forwards) method, in the provided MyLinkedList class.
- This class should be
found in the Student_Answers.cs file. In the starter project, in the Student_Answers.cs
file, you'll find a class named MyLinkedList.
-
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.
- Create a method named
RecursivelyPrintForward on your linked list class, which will print out
all the values stored in
the list.
- 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.
- 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.
- Create a method named
RecursivelyPrintBackward on your linked list class, which will print out the
contents of the list in reverse order.
- 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.
- 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.
- Once you've figured
out how to print either way, create a method named RecursivelyPrint(bool
forwards).
- 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
- 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
- If there are no items
in the list, this method should not print out anything, regardless of the value
of the forwards parameter.
- 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).
- Once you’ve completed the above task, you should run the tests in
the
NUnit_Tests_LL_RecursivelyPrintForward
class, the
NUnit_Tests_LL_RecursivelyPrintBackward
class, and 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.