Exemple #1
0
            public void ReturnCountWhenListHasItems()
            {
                IntLinkedList list = new IntLinkedList();

                list.Add(8);
                list.Add(13);
                list.Add(21);

                Assert.Equal(3, list.Count);
            }
Exemple #2
0
    public static int[] SortWithoutGenerics(int[] Values)
    {
        IntLinkedList Original = new IntLinkedList();
        IntLinkedList Result   = new IntLinkedList();
        IntLinkedList Sublist  = new IntLinkedList();

        Original.AddRange(Values);
        //While we still have numbers to sort
        while (Original.Count > 0)
        {
            //Clear sublist and take first available number from original to the new sublist
            Sublist.Clear();
            Sublist.Add(Original.FirstItem.Value);
            Original.Remove(Original.FirstItem);

            IntLinkedItem currentOriginalItem = Original.FirstItem;
            //Iterate through original numbers
            while (currentOriginalItem != null)
            {
                //If the number is bigger than the last item in the sublist
                if (currentOriginalItem.Value > Sublist.LastItem.Value)
                {
                    //Add it to the sublist and remove it from original
                    Sublist.Add(currentOriginalItem.Value);
                    //Store the next item
                    IntLinkedItem nextItem = currentOriginalItem.NextItem;
                    //Remove current item from original
                    Original.Remove(currentOriginalItem);
                    //Set next item as current item
                    currentOriginalItem = nextItem;
                }
                currentOriginalItem = currentOriginalItem.NextItem;
            }
            //If this is the first sublist
            if (Result.Count == 0)
            {
                Result.AddRange(Sublist);     //Add all the numbers to the result
            }
            else
            {
                IntLinkedItem currentSublistItem = Sublist.FirstItem;

                //Iterate through the sublist
                while (currentSublistItem != null)
                {
                    bool          inserted          = false;
                    IntLinkedItem currentResultItem = Result.FirstItem;
                    //Iterate through the current result
                    while (currentResultItem != null)
                    {
                        //Is the sublist number lower than the current item from result?
                        if (currentSublistItem.Value < currentResultItem.Value)
                        {
                            //Yes, insert it at the current Result position
                            Result.InsertBefore(currentResultItem, currentSublistItem.Value);
                            inserted = true;
                            break;
                        }
                        currentResultItem = currentResultItem.NextItem;
                    }
                    //Did we inserted the item because found it was lower than one of the result's number?
                    if (!inserted)
                    {
                        Result.Add(currentSublistItem.Value);    //No, we add it to the end of the results
                    }
                    currentSublistItem = currentSublistItem.NextItem;
                }
            }
        }
        //Return the results
        return(Result.ToArray());
    }