Exemplo n.º 1
0
        public static BasicLinkedList <int> Swap_Nodes(BasicLinkedList <int> list)
        {
            BasicLinkedList <int> .Node first  = list.Root;
            BasicLinkedList <int> .Node second = first.Next;
            BasicLinkedList <int> .Node prev   = null;

            if (second == null)
            {
                return(list);
            }
            else
            {
                list.Root = second;
            }

            while (first != null && second != null)
            {
                // Swap Nodes
                first.Next  = second.Next;
                second.Next = first;

                // Point previos node to second node of pair
                if (prev != null)
                {
                    prev.Next = second;
                }

                //jump to next pair of nodes
                prev   = first;
                first  = first.Next;
                second = first != null ? first.Next : null;
            }

            if (first == null)
            {
                list.Last = prev;
            }
            else
            {
                list.Last = first;
            }

            return(list);
        }
Exemplo n.º 2
0
        public static BasicLinkedList <int> Swap_Values(BasicLinkedList <int> list)
        {
            BasicLinkedList <int> .Node first  = list.Root;
            BasicLinkedList <int> .Node second = first.Next;
            int temp;

            while (first != null && second != null)
            {
                // Swap values
                temp       = first.Val;
                first.Val  = second.Val;
                second.Val = temp;

                //jump to next pair of nodes
                first  = second.Next;
                second = first != null ? first.Next:null;
            }

            return(list);
        }
Exemplo n.º 3
0
 public InOut(string l, string l2) : base(BasicLinkedList <int> .Assemble(l), BasicLinkedList <int> .Assemble(l2), true)
 {
     AddSolver((arg, erg) => erg.Setze(Swap_Nodes(arg.Copy())), "Swap Nodes");
     AddSolver((arg, erg) => erg.Setze(Swap_Values(arg.Copy())), "Swap Values");
 }