No. insertFirst()
and insertLast()
insert new nodes without respect to order
so they won't work for an ordered list.
We need to write insertInOrder(int data)
.
The data
can be any int
value, and the list can be any ordered list.
The method needs to create a new Node
and link it into the list at the right location.
Exactly where that location is depends on the data and on the list.
To do this, decide on all the cases and to make sure each is handled correctly.
What are the cases for insertInOrder(int data)
?
It is too easy to overlook a case, and also easy to be too specific.
An example of overlooking a case is failing to consider inserting into an empty list.
An example of being to specific is thinking that "inserting a negative value" is a separate case.
Above are some possible Node
s and three possible OrderedLinkedList
s into which they
should be inserted.
What cases does insertInOrder(int data)
need to handle?