Strategies for LL: Traversing A Linked List (

advertisement
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:
1. Setup
2. Iteration Logic
3. Work
(that is done at each iteration)
4. Teardown
(anything that happens after the iteration)
What you need to do for this exercise:
1. You need to examine the Print method, and implement the Find, and
PrintAllMatching methods 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.
b. You do not need to implement any other methods for this exercise.
2. 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.
a. 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.
3. 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.
4. 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.
5.
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, and return true. 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.
Download