Strategies for
traversing a linked list
The goal of this exercise
is to clarify and refine your understanding of the “Linked List Traversal”
schema, by implementing several (fairly easy) methods.
Building off what you’ve seen in class, implement the Print, Find, and
PrintAllMatching methods in this week’s starter project. Keep in mind that
the real goal here is to practice using the “Linked List Traversal” schema, so
focus on using and learning that pattern as much as possible.
As a quick summary, the four steps in the "Linked List Traversal" schema are:
- Setup
- Iteration Logic
- Work
(that is done at each iteration)
- Teardown
(anything that happens after the iteration)
What you need to do for
this exercise:
- You need to examine the
Print method, and implement the Find, and PrintAllMatching methods in
the provided MyLinkedList
class.
- This class should
be found in the Student_Answers.cs file
- You do not need to
implement any other methods for this exercise.
- To start, you need to
summarize what is meant by the “linked list traversal” schema, making
sure to pay particular attention to the different ‘stages’ of the
schema. There is a space to put your answer, in comments, above the
‘Print’ method.
- The objective of this
requirement is to get you to think about what this means, and be
able to express this in your own words, so that you can then use
this schema to simplify future linked-list code that you write.
- public void Print();
This method should print all the elements in the list. You’ve seen
implementations of this before, so instead focus on filling in the
comments about how this fits into the list traversal schema.
You are required to put comments into your implementation of this method
making it clear which code implements which stage of the list traversal
schema.
-
public bool Find(int target);
This method is given the target value to look for, and if that
value is anywhere in the list, it will return true. If the value is not
present, then it wil return false (including when the list is
empty).
This should be fairly easy to do, so focus on filling in the comments
about how this fits into the list traversal schema, in addition to
getting this method to work. You are required to put comments into your
implementation of this method making it clear which code implements
which stage of the list traversal schema.
- public bool
PrintAllMatching( int target )
This method is given the target value to look for, and if that
value is anywhere in the list, this method will print out that value to
the screen (once per time that it's in the list), and then return true
once it's gone through the entire list. If the value is not present, then it will
return false (including when the list is empty).
- You are required to put
comments into your implementation of this
method making it clear which code implements which stage of the list
traversal schema.
- There are no NUnit tests for this
exercise