the assignment is to create a class library for manipulating ConsLists. I haven't found a lot of resources on ConsLists, and I hadn't studied them or recursion before we were assigned this project. That said, this is a (non-functional) method that is supposed to store a ConsList of values less than an int p in an array at index 0, values equal in an array at 1, and values greater in an array 2, then return the array. So:

``````public static ConsList<int>[] Partition(ConsList<int> list, int p)
{
ConsList<int>[] arr = new ConsList<int>[3];
int index;
if (list != null)
{
if (list.Head < p) index = 0;
else if (list.Head == p) index = 1;
else index = 2;
arr = Partition(list.Tail, p);
}
return arr;
}``````

Can anybody point me in the right direction?

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span

I suggest you call Partition on the tail _before_ you examine the first element of the list. Then update the array that Partition returns appropriately to account for the first element of the list. Only construct the array when Partition receives an empty list.

I don't really understand. How can Partition return an array, and how can I alter the array, if I only construct it once list is null?

Well, Partition can clearly return an array. And you can obviously alter the array that it returns. And you can easily construct an an array. I don't understand your question.

Okay. I got it.

``````public static ConsList<int>[] Partition(ConsList<int> list, int p)
{
ConsList<int>[] arr = new ConsList<int>[3];
if (list == null)
{
return arr;
}
else
{
arr = Partition(list.Tail, p);
{
return arr;
}
{
return arr;
}
else
{
return arr;
}
}
}``````

HOW TO Delete the & find Minimum using recurrsion

`````` public static ConsList<int> RemoveMin(ConsList<int> list, out int min)
{

ConsList<int> mylist = null;

if (list == null)
{
return mylist;
}
else
{
if (list.Tail != null)
{
{

mylist = new ConsList<int>(min, RemoveMin(list.Tail, out min));
//mylist = RemoveMin(list.Tail, out min);

}
else
mylist = new ConsList<int>(min, RemoveMin(list.Tail, out min));
}
}

return mylist;
}
``````

any help

Edited by mike_2000_17: Fixed formatting