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[index] = new ConsList<int>(list.Head, null);
                arr = Partition(list.Tail, p);
            }
            return arr;
        }

Can anybody point me in the right direction?

Recommended Answers

All 5 Replies

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);
                if (list.Head < p)
                {
                    arr[0] = new ConsList<int>(list.Head, arr[0]);
                    return arr;
                }
                else if (list.Head == p)
                {
                    arr[1] = new ConsList<int>(list.Head, arr[1]);
                    return arr;
                }
                else
                {
                    arr[2] = new ConsList<int>(list.Head, arr[2]);
                    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;

            min = list.Head;

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

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

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

            return mylist;
        }

any help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.