Exemplo n.º 1
0
        public static CustomLinkedList <T> ToSinglyLinkedList <T>(this IEnumerable <T> enumerable)
        {
            CustomLinkedList <T> output = new CustomLinkedList <T>();

            output.AppendRange(enumerable);
            return(output);
        }
Exemplo n.º 2
0
        public void Sort(IComparer <T> comparer)
        {
            if (head == null || head.Next == null)
            {
                return;
            }

            CustomLinkedList <T> left = new CustomLinkedList <T>();
            Node <T>             tmp  = head;

            left.head = tmp;
            Node <T> slowPointer = head;
            Node <T> fastPointer = head;


            while (fastPointer != null && fastPointer.Next != null)
            {
                tmp         = slowPointer;
                slowPointer = slowPointer.Next;
                fastPointer = fastPointer.Next.Next;
            }

            tmp.Next = null;

            CustomLinkedList <T> right = new CustomLinkedList <T>();

            right.head = slowPointer;

            right.Sort(comparer);
            left.Sort(comparer);

            Merge(left, right, comparer);
        }
Exemplo n.º 3
0
        private void Merge(CustomLinkedList <T> left, CustomLinkedList <T> right, IComparer <T> comparer)
        {
            if (left.head == null)
            {
                head = right.head;
            }
            if (right.head == null)
            {
                head = left.head;
            }


            Node <T> currentLeft  = left.head;
            Node <T> currentRight = right.head;

            if (CompareElements(comparer, currentLeft.Value, currentRight.Value) <= 0)
            {
                head        = currentLeft;
                currentLeft = currentLeft.Next;
            }
            else
            {
                head         = currentRight;
                currentRight = currentRight.Next;
            }

            Node <T> current = head;

            while (currentRight != null && currentLeft != null)
            {
                if (CompareElements(comparer, currentLeft.Value, currentRight.Value) <= 0)
                {
                    current.Next = currentLeft;

                    current      = current.Next;
                    current.Next = currentRight;
                }
                else
                {
                    current.Next = currentRight;
                    current      = current.Next;
                    current.Next = currentLeft;
                }
                currentLeft  = currentLeft.Next;
                currentRight = currentRight.Next;
            }

            if (currentRight != null)
            {
                current.Next = currentRight;
            }

            if (currentLeft != null)
            {
                current.Next = currentLeft;
            }
        }
Exemplo n.º 4
0
        public IEnumerable <int> IndexesOf(T value)
        {
            CustomLinkedList <int> output = new CustomLinkedList <int>();
            int      index   = 0;
            Node <T> current = head;

            while (current != null)
            {
                if (current.Value.Equals(value))
                {
                    yield return(index);
                }

                current = current.Next;
                index++;
            }
        }