The RemoveMin class should remove the first occurrence of the smallest element from the input list. Despite what the inputs are I keep getting the same NullReference error from having 'rem = null'. I don't know if I'm using recursion right or not. I have a feeling the starting reference for the ConsList may not be right. This was a homework assignment but it was due a couple days ago. I just want to understand for future references what I need to do. Anything pointers or examples would be appreciated. Here is the line of code giving me problems.
public static ConsList<int> RemoveMin(ConsList<int> list, out int min)
{
ConsList<int> r;
min = list.Head;
if (list.Tail.Head > min)
{
r = new ConsList<int>(list.Head, RemoveMin(list.Tail, out min));
return r;
}
else
{
r = new ConsList<int>(min, RemoveMin(list.Tail, out min));
return r;
}
}
Here is the class that implements RemoveMin. 'rem' keeps causing a null reference. This class shouldn't need any editing.
private ConsList<int> SelectionSort(ConsList<int> list)
{
if (list == null)
{
return list;
}
else
{
int min;
ConsList<int> rem = RemoveMin(list, out min);
return new ConsList<int>(min, SelectionSort(rem));
}
}