Esempio n. 1
0
        private static void SwapValues(Common.LinkedList <int> .LinkedListNode node, Common.LinkedList <int> .LinkedListNode otherNode)
        {
            var swap = node.Value;

            node.Value      = otherNode.Value;
            otherNode.Value = swap;
        }
Esempio n. 2
0
        public static Common.LinkedList <int> SwapEveryTwoNodes(Common.LinkedList <int> list)
        {
            var current = list.Root;

            while (current != null)
            {
                SwapValues(current, current.Next);

                current = current.Next.Next;
            }

            return(list);
        }
Esempio n. 3
0
        public static bool IsSinglyLinkedListPalindrome(Common.LinkedList <int> list)
        {
            var doublyLinkedList = new LinkedList <int>();

            var current = list.Root;

            while (current != null)
            {
                doublyLinkedList.AddLast(current.Value);

                current = current.Next;
            }

            return(IsDoublyLinkedListPalindrome(doublyLinkedList));
        }
Esempio n. 4
0
        public static void Reverse <T>(this Common.LinkedList <T> list)
            where T : IComparable <T>
        {
            var currentNode  = list.Root;
            var previousNode = (Common.LinkedList <T> .LinkedListNode)null;

            while (currentNode != null)
            {
                var nextNode = currentNode.Next;

                currentNode.Next = previousNode;
                previousNode     = currentNode;
                currentNode      = nextNode;
            }

            list.Root = previousNode;
        }
Esempio n. 5
0
        public static IList <T> ToList <T>(this Common.LinkedList <T> linkedList)
            where T : IComparable <T>
        {
            var list = new List <T>();

            if (linkedList.Root != null)
            {
                var currentNode = linkedList.Root;

                while (currentNode != null)
                {
                    list.Add(currentNode.Value);
                    currentNode = currentNode.Next;
                }
            }

            return(list);
        }
Esempio n. 6
0
        public static Common.LinkedList <int> MergeLists(List <Common.LinkedList <int> > lists)
        {
            var list = new Common.LinkedList <int>();

            while (lists.Count > 0)
            {
                var min = lists.Min();

                list.Add(min.Root.Value);

                if (min.Root.Next == null)
                {
                    lists.Remove(min);
                }
                else
                {
                    min.Root = min.Root.Next;
                }
            }

            return(list);
        }
Esempio n. 7
0
        /// <summary>
        /// Calculates the optimal shortest path from source vertex to target vertex
        /// </summary>
        public IEnumerable <Edge> Dijkstra(int source, int target)
        {
            var unvisited = new HashSet <int>(_vertices.Where(i => i != source));
            var sourceMap = new Dictionary <int, Tuple <int, Common.LinkedList <Edge> > > {
                [source] = new Tuple <int, Common.LinkedList <Edge> > (0, null)
            };

            while (!sourceMap.ContainsKey(target))
            {
                var minEdge = _edges.Where(i => unvisited.Contains(i.In) != unvisited.Contains(i.Out))
                              .Aggregate((m, n) => sourceMap[m.Out].Item1 + m.Weight < sourceMap[n.Out].Item1 + n.Weight ? m : n);
                var llNode = new Common.LinkedList <Edge> {
                    Value = minEdge, Prev = sourceMap[minEdge.Out].Item2
                };

                sourceMap[minEdge.In] = new Tuple <int, Common.LinkedList <Edge> >(sourceMap[minEdge.Out].Item1 + minEdge.Weight, llNode);
                unvisited.Remove(minEdge.In);
                unvisited.Remove(minEdge.Out);
            }

            return(sourceMap[target].Item2.Compile());
        }