Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insert SLL
What are we testing for in the insert function? If the list is empty, and what else? Do we only have to add to the list  at the head or at nth spot also?
You should use assertions to make sure the index is a legal index.

I see four cases: the list is empty, the index is zero (insertion at the front), the indexed is equal to the size (insertion at the back), and the index is somewhere in between. Similar for deltion.
So I did if statements and else ifs. So for the if I checked if the list is empty. So to check if the index is 0 should I do something like
else if(index ==0) or is there another way to do it?

Do I need a else statement that'll throw an error message if it's none of those cases?
Yes, your second case (index 0) is correct.

No, the assertions that the index is within range will mean the four cases cover everything. The last case should be an else (not an else if).

Forum Jump:

Users browsing this thread: 1 Guest(s)