LL: Insert At Location

advertisement
Insert A New Node At An Arbitrary Index
The goal of this exercise is to make sure that you can implement the logic needed to
add items to the middle of a linked list.
What you need to do for this exercise:
1. For this exercise, you need to implement the InsertAt 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.
2. The InsertAt method looks like:
void InsertAt(int newData, uint index);
You've already seen (from the lecture) how to build a function which will insert the
new integer into a linked list, at the given index, so the overall details will not be
explained here, with two exceptions.
a. Index is a ‘zero-based’ index, meaning that the first item in the list has the
index value “0”. So if the list contains {1,2}, and you call InsertAt(3, 0), the
value three should be placed in the list so that 3 is at location zero; the
resulting list would look like: {3, 1, 2}
b. If index is larger than the list, you should insert the new data item at the
very end of the list.
3. Annotate this method (put a comment next to this method) describing the running
time of that method, using the Big "Oh" notation.
4. Once you’ve completed the above task, you should run the tests in the
NUnit_Tests_LL_InsertAt 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.
a. The test cases include:
Insert into an empty list, at index 0, 1, or 2
Insert into a list with a single item, at index 0, 1, or 2
Insert into a list with a two items, at index 0, 1, 2, 3, or 4
Insert into a list with, say, 5 items, at index 0, 1, 2, 3, 4, 5, 6, or 7
b. 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. You will need to implement these methods.
Download