///<summary>Inserts <paramref name="items"/> after the <paramref name="this"/> node and returns the nodes that were inserted.</summary> public static IReadOnlyList <LinkedListNode <T> > AddAfter <T>(this LinkedList <T> @this, LinkedListNode <T> node, IEnumerable <T> items) { Contract.Requires(items != null); Contract.Requires(@this != null); Contract.Requires(@this == node.List); return(node.AddAfter(items)); }
///<summary>Replaces <paramref name="this"/> and returns the nodes that were inserted.</summary> public static IReadOnlyList <LinkedListNode <T> > Replace <T>(this LinkedListNode <T> @this, IEnumerable <T> items) { Contract.Requires(items != null); Contract.Requires(@this != null); var newNodes = @this.AddAfter(items); @this.List.Remove(@this); return(newNodes); }
static void GetCrabby(ref Cups cups, int numCups, int numMoves) { int minCup = cups.Min(); int maxCup = cups.Max(); if (numCups > cups.Count) { int cupsToAdd = numCups - cups.Count; cups = new Cups(cups, Enumerable.Range(maxCup + 1, cupsToAdd)); maxCup += cupsToAdd; } LinkedListNode <int> currentCup = cups.First; for (int i = 0; i < numMoves; i++) { //Pick up the next 3 cups for later use List <LinkedListNode <int> > pickedUp = currentCup.CircularRemoveAfter(3).ToList(); //cups.Skip(1).Take(3).ToList(); //Calcluate our destination cup int destination = currentCup.Value; do { destination--; if (destination < minCup) { destination = maxCup; } } while (pickedUp.Select(x => x.Value).Contains(destination)); //Find the destination cup LinkedListNode <int> destinationCup = cups[destination]; //Insert the picked up cups after the destination cup destinationCup.AddAfter(pickedUp); //Move the crab to the next cup for the next move. currentCup = currentCup.CircularNext(); //cups.Next(); } }