Пример #1
0
 public void should_make_the_node_head_and_tail_of_the_list()
 {
     var list = new DoublyLinkedList<int>();
     list.Add(3);
     Assert.That(list.Head.Data.Equals(3), Is.True);
     Assert.That(list.Tail.Data.Equals(3), Is.True);
 }
Пример #2
0
        public static void Sort(double[] array)
        {
            var buckets = new DoublyLinkedList<double>[10];
            for (var i = 0; i < buckets.Length; i++)
            {
                buckets[i] = new DoublyLinkedList<double>();
            }

            foreach (var v in array)
            {
                if (v < 0 || v >= 1)
                {
                    throw new InvalidOperationException($"{v} is not in [0..1) range.");
                }
                buckets[(int)(v * 10)].Insert(v);
            }

            foreach (var bucket in buckets)
            {
                InsertionSort(bucket);
            }

            var index = 0;
            foreach (var bucket in buckets)
            {
                for (var node = bucket.Head; node != null; node = node.Next)
                {
                    array[index++] = node.Value;
                }
            }
        }
Пример #3
0
        public static void Main()
        {
            var list = new DoublyLinkedList<int>();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.AddLast(5);
            list.AddFirst(3);
            list.AddFirst(2);
            list.AddLast(10);
            Console.WriteLine("Count = {0}", list.Count);

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.RemoveFirst();
            list.RemoveLast();
            list.RemoveFirst();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            list.RemoveLast();

            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");
        }
Пример #4
0
        private static void InsertionSort(DoublyLinkedList<double> bucket)
        {
            if (bucket.Head == null)
            {
                return;
            }

            var currentNode = bucket.Head.Next;
            while (currentNode != null)
            {
                var node = currentNode.Previous;
                while (node != null && node.Value > currentNode.Value)
                {
                    node = node.Previous;
                }

                bucket.Delete(currentNode);
                if (node != null)
                {
                    bucket.InsertAfter(node, currentNode.Value);
                }
                else
                {
                    bucket.Insert(currentNode.Value);
                }

                currentNode = currentNode.Next;
            }
        }
Пример #5
0
 public void should_return_the_node_if_the_value_exists()
 {
     var list = new DoublyLinkedList<int>();
     list.Add(5);
     list.Add(3);
     var node = list.Find(5);
     Assert.That(node.Data.Equals(5), Is.True);
 }
Пример #6
0
 public void BeginTestMethod()
 {
     target = new DoublyLinkedList<int>();
     Assert.AreEqual(target.Leng, 0);
     target.Add(2);
     target.Add(3, 1);
     target.AddLast(5);
 }
Пример #7
0
 public void CopyToWithLessSpaceInTheArray()
 {
     DoublyLinkedList<int> list = new DoublyLinkedList<int>();
     int[] array = new int[3];
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.CopyTo(array, 2);
 }
        public void TestDoublyLinkedListAdd()
        {
            DoublyLinkedList<string> list = new DoublyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");

            Assert.AreEqual(3, list.Count);
        }
        public void CopyTo()
        {
            var list = new DoublyLinkedList<int>(new[] { 1, 2, 3 });
            var array = new int[4];

            list.CopyTo(array, 1);

            CollectionAssert.AreEqual(new[] {0, 1, 2, 3}, array);
        }
Пример #10
0
 public void CopyTo()
 {
     DoublyLinkedList<int> list = new DoublyLinkedList<int>();
     int[] array = new int[3];
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.CopyTo(array, 0);
 }
Пример #11
0
 public void AddAfterTailPUT([PexAssumeUnderTest]IList<int> values, int toAddValue)
 {
     PexAssume.IsTrue(values.Count > 1);
     PexAssume.IsFalse(values.Contains(toAddValue));
     DoublyLinkedList<int> dll = new DoublyLinkedList<int> (values);
     dll.AddAfter(dll.Tail, toAddValue);
     PexAssert.AreEqual(toAddValue, dll.Tail.Value);
     PexAssert.AreEqual(values[values.Count - 1], dll.Tail.Previous.Value);
 }
        private static void RunDoublyLinkedList()
        {
            var doublyLinkedList = new DoublyLinkedList<int>();
            Console.WriteLine("Creating a new list");
            Utils.PrintList(doublyLinkedList.Head);

            foreach (var key in new[] {10, 17, 1, 4, 9})
            {
                Console.WriteLine($"Inserting {key}");
                doublyLinkedList.Insert(key);
            }
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine("Appending 19");
            doublyLinkedList.Append(19);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            var node = doublyLinkedList.Search(17);
            Console.WriteLine($"Inserting 20 before {node.Value}");
            doublyLinkedList.InsertBefore(node, 20);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine($"Inserting 13 after {node.Value}");
            doublyLinkedList.InsertAfter(node, 13);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine("Deleting first element");
            node = doublyLinkedList.Search(9);
            doublyLinkedList.Delete(node);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine("Deleting last element");
            node = doublyLinkedList.Search(10);
            doublyLinkedList.Delete(node);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine("Deleting element in the middle");
            node = doublyLinkedList.Search(1);
            doublyLinkedList.Delete(node);
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);

            Console.WriteLine("Deleting all nodes");
            while (doublyLinkedList.Head != null)
            {
                Console.WriteLine($"Deleting {doublyLinkedList.Head.Value}");
                doublyLinkedList.Delete(doublyLinkedList.Head);
            }
            Utils.PrintList(doublyLinkedList.Head);
            Utils.PrintListReverse(doublyLinkedList.Tail);
        }
Пример #13
0
 public void Clear()
 {
     DoublyLinkedList<int> list = new DoublyLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.Clear();
     list.ShouldBeEmpty();
 }
 public static void ClassInitializer(TestContext context)
 {
     head = new DoublyLinkedListNode<int>(-1);
       doublyLinkedList= new DoublyLinkedList<int>(head);
      doublyLinkedList.Add(4);
      doublyLinkedList.Add(41);
       doublyLinkedList.Add(54);
       doublyLinkedList.Add(9);
 }
Пример #15
0
 public void Contains()
 {
     DoublyLinkedList<int> list = new DoublyLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     bool isTrue = list.Contains(3);
     Assert.AreEqual(true, isTrue);
 }
Пример #16
0
        public void AddAfterTailTest()
        {
            DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10};

            dll.AddAfter(dll.Head, 20);

            Assert.AreEqual(20, dll.Tail.Value);
            Assert.AreEqual(10, dll.Tail.Previous.Value);
        }
        public void AddFirstTest()
        {
            var list = new DoublyLinkedList<int>();
            list.AddFirst(2);
            list.AddFirst(4);
            list.AddFirst(6);

            Assert.AreEqual(list.Head.Value, 6);
            Assert.AreEqual(list.Tail.Value, 2);
        }
        public void RemoveAt()
        {
            var list = new DoublyLinkedList<int>(new[] { 1, 2, 3 });

            list.RemoveAt(1);

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(1, list[0].Value);
            Assert.AreEqual(3, list[1].Value);
        }
Пример #19
0
        public void AddAfterTest()
        {
            DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10, 20, 30};

            dll.AddAfter(dll.Head.Next, 25);

            Assert.AreEqual(25, dll.Head.Next.Next.Value);
            Assert.AreEqual(20, dll.Head.Next.Next.Previous.Value);
            Assert.AreEqual(30, dll.Head.Next.Next.Next.Value);
            Assert.AreEqual(25, dll.Tail.Previous.Value);
        }
        public void ClearTest()
        {
            var list = new DoublyLinkedList<int>();
            list.AddLast(2);
            list.AddLast(4);
            list.AddLast(6);

            list.Clear();

            Assert.AreEqual(list.Head, null);
        }
Пример #21
0
 public void AddAtHead()
 {
     DoublyLinkedList<int> list = new DoublyLinkedList<int>();
     int[] array = new int[3];
     list.Add(3);
     list.ShouldContain(3);
     list.Add(5);
     list.ShouldContain(5);
     list.Add(7);
     list.ShouldContain(7);
     list.CopyTo(array, 0);
 }
        public void TestDoublyLinkedListIndexer()
        {
            DoublyLinkedList<string> list = new DoublyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";

            string[] array = new string[list.Count];
            list.CopyTo(array);
            Assert.AreEqual("Zero, Two, Three", string.Join(", ", array));
        }
        public void Insert()
        {
            var list = new DoublyLinkedList<int>(2);

            list.Insert(0, 0);
            list.Insert(1, 1);
            list.Insert(3, 3);

            for (var i = 0; i < 3; i++)
            {
                Assert.AreEqual(i, list[i].Value);
            }
        }
        public void TestDoublyLinkedListInsert_InsertInTheMiddle()
        {
            DoublyLinkedList<string> list = new DoublyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";
            list.Insert(1, "Ten");

            string[] array = new string[list.Count];
            list.CopyTo(array);
            Assert.AreEqual("Zero, Ten, Two, Three", string.Join(", ", array));
        }
        public void Add()
        {
            var list = new DoublyLinkedList<int>(2);

            list.AddFirst(1);
            list.AddLast(3);
            list.AddFirst(0);
            list.AddLast(4);

            for (var i = 0; i < 5; i++)
            {
                Assert.AreEqual(i, list[i].Value);
            }
        }
        public void Remove()
        {
            var list = new DoublyLinkedList<int>(new [] {0, 1, 2, 3, 4, 5});

            list.RemoveFirst();
            list.RemoveLast();
            list.RemoveFirst();
            list.RemoveLast();

            for (var i = 2; i < 4; i++)
            {
                Assert.AreEqual(i, list[i - 2].Value);
            }
        }
Пример #27
0
        public void AddLast_EmptyList_ShouldAddElement()
        {
            // Arrange
            var list = new DoublyLinkedList<int>();

            // Act
            list.AddLast(5);

            // Assert
            Assert.AreEqual(1, list.Count);

            var items = new List<int>();
            list.ForEach(items.Add);
            CollectionAssert.AreEqual(items, new List<int>() { 5 });
        }
Пример #28
0
        public void AddLast_SeveralElements_ShouldAddElementsCorrectly()
        {
            // Arrange
            var list = new DoublyLinkedList<int>();

            // Act
            list.AddLast(5);
            list.AddLast(10);
            list.AddLast(15);

            // Assert
            Assert.AreEqual(3, list.Count);

            var items = new List<int>();
            list.ForEach(items.Add);
            CollectionAssert.AreEqual(items, new List<int>() { 5, 10, 15 });
        }
        public void TestDoublyLinkedListForEach()
        {
            DoublyLinkedList<string> list = new DoublyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";

            string[] array = new string[list.Count];
            int index = 0;

            foreach (string item in list)
            {
                array[index] = item;
                index++;
            }

            Assert.AreEqual("Zero, Two, Three", string.Join(", ", array));
        }
Пример #30
0
        public void List_AddToBeginning_IsNotEqual()
        {
            //Arrange
            var list  = new DoublyLinkedList();
            var list2 = new DoublyLinkedList();

            //Act
            list.AddToBeginning("a");
            list.AddToBeginning("b");
            list.AddToBeginning("c");

            list2.AddToBeginning("a");
            list2.AddToBeginning("b");
            list2.AddToBeginning("a");

            //Assert

            Assert.IsFalse(list.Equals(list2));
        }
Пример #31
0
        public void List_AddToEnd_IsEqual()
        {
            //Arrange
            var list  = new DoublyLinkedList();
            var list2 = new DoublyLinkedList();

            //Act
            list.AddToEnd("a");
            list.AddToEnd("b");
            list.AddToEnd("c");

            list2.AddToEnd("a");
            list2.AddToEnd("b");
            list2.AddToEnd("c");

            //Assert

            Assert.IsTrue(list.Equals(list2));
        }
Пример #32
0
        public void List_Init_and_Remove()
        {
            int[] arr       = { 5, 4, 3, 2, 1, 5, 4, 3, 2, 1 };
            int[] arr_no5   = { 4, 3, 2, 1, 4, 3, 2, 1 };
            int[] arr_no5_1 = { 4, 3, 2, 4, 3, 2 };
            int[] arr_5     = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

            DoublyLinkedList <int> lst   = new DoublyLinkedList <int>(arr);
            DoublyLinkedList <int> lst_5 = new DoublyLinkedList <int>(arr_5);

            while (lst.Contains(5))
            {
                Assert.IsTrue(lst.Remove(5));
            }

            IEnumerator <int> enumerator = lst.GetEnumerator();

            for (int i = 0; i < arr_no5.Length && enumerator.MoveNext(); ++i)
            {
                Assert.AreEqual(arr_no5[i], enumerator.Current);
            }

            while (lst.Contains(1))
            {
                Assert.IsTrue(lst.Remove(1));
            }

            enumerator = lst.GetEnumerator();
            for (int i = 0; i < arr_no5_1.Length && enumerator.MoveNext(); ++i)
            {
                Assert.AreEqual(arr_no5_1[i], enumerator.Current);
            }

            for (int i = 0; i < arr_5.Length; ++i)
            {
                Assert.IsTrue(lst_5.Remove(5));
            }

            if (lst_5.Count != 0)
            {
                Assert.Fail("Not all items removed");
            }
        }
Пример #33
0
        public void List_Init_and_CopyTo_Less_Memory_allocated()
        {
            int[] arr = { 5, 4, 3, 2, 1 };

            DoublyLinkedList <int> lst = new DoublyLinkedList <int>(arr);

            int[] arr1 = new int[5];

            try
            {
                lst.CopyTo(arr1, 2);

                Assert.Fail("Succesfully copied to source with less memory allocated");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Assert.IsNotNull(e);
            }
        }
        static void Main(string[] args)
        {
            var dlist = new DoublyLinkedList <int>();

            dlist.Add(5);
            dlist.Add(2);
            dlist.Add(6);
            dlist.Add(4);
            dlist.Add(3);
            dlist.Add(1);

            Console.WriteLine("-" + dlist.ToString());

            dlist = PancakeSort(dlist);

            Console.WriteLine("-" + dlist.ToString());

            Console.ReadKey();
        }
    public void AddLast_EmptyList_ShouldAddElement()
    {
        // Arrange
        var list = new DoublyLinkedList <int>();

        // Act
        list.AddLast(5);

        // Assert
        Assert.AreEqual(1, list.Count);

        var items = new List <int>();

        list.ForEach(items.Add);
        CollectionAssert.AreEqual(items, new List <int>()
        {
            5
        });
    }
        void RemoveLast(IReadOnlyList <int> collection, IReadOnlyCollection <int> expectedCollection)
        {
            // Arrange
            var list    = new DoublyLinkedList <int>(collection);
            var version = list.Version;

            // Act
            list.RemoveLast();

            // Assert
            list.Version.Must()
            .BeNotEqualTo(version);
            list.EnumerateForward().Must()
            .BeEnumerableOf <int>()
            .BeEqualTo(expectedCollection);
            list.EnumerateReversed().Must()
            .BeEnumerableOf <int>()
            .BeEqualTo(expectedCollection.Reverse());
        }
Пример #37
0
        // Sorts the ordered linked list alphabetically
        private DoublyLinkedList FinalizeOrder(DoublyLinkedList decimals)
        {
            List <string>    deweys = new List <string>();
            DoublyLinkedList d      = new DoublyLinkedList();

            foreach (var dec in decimals)
            {
                deweys.Add(dec.Data.Decimal + " " + dec.Data.Author);
            }

            string[] array = deweys.ToArray();

            int i, j, l;

            string[] arrayone = array;
            string   temporary;

            l = arrayone.Length;

            for (i = 0; i < l; i++)
            {
                for (j = 0; j < l - 1; j++)
                {
                    if (arrayone[j].CompareTo(arrayone[j + 1]) > 0)
                    {
                        temporary       = arrayone[j];
                        arrayone[j]     = arrayone[j + 1];
                        arrayone[j + 1] = temporary;
                    }
                }
            }

            for (i = 0; i < l; i++)
            {
                d.Add(new DecimalClass()
                {
                    Decimal = arrayone[i].Split(" ")[0],
                    Author  = arrayone[i].Split(" ")[1]
                });
            }

            return(d);
        }
Пример #38
0
        public void AddAtIndexTest()
        {
            var list = new DoublyLinkedList <int>();

            for (int i = 1; i < 5; i++)
            {
                list.Add(i - 1, i);
            }
            for (int i = 1; i < 5; i++)
            {
                Assert.AreEqual(list.Find(i - 1), i);
            }
            list.Add(0, 0);
            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(list.Find(i), i);
            }
            list.Clear();
        }
        public void ConvertFromBinarySearchTreeTest3()
        {
            BinarySearchTree <int> tree = new BinarySearchTree <int>(new BiNode <int>(0));

            int[] input = { 0, 1, 4, 4, 4, 4, 6, 8, 8, 8, 10 };

            for (int i = 1; i < input.Length; i++)
            {
                tree.Insert(input[i]);
            }

            tree.Print();

            var doublyLinkedList = DoublyLinkedList <int> .ConvertFrom(tree);

            doublyLinkedList.Print();

            doublyLinkedList.Print(true); // Print reverse
        }
Пример #40
0
        public void Find(IReadOnlyList <int?> collection, int?value, bool shouldFind)
        {
            // Arrange
            var list = new DoublyLinkedList <int?>(collection);

            // Act
            var result = list.Find(value);

            // Assert
            if (shouldFind)
            {
                result.Should().NotBeNull();
                result.Value.Should().Be(value);
            }
            else
            {
                result.Should().BeNull();
            }
        }
Пример #41
0
        public void DoublyLinkedList_GetIndexOf_Test_2()
        {
            // Initialization.
            XmlDocument doc = new XmlDocument();

            doc.Load(xliffPath);

            XmlNodeList   transUnitList  = doc.GetElementsByTagName("trans-unit");
            TransUnitData firstTestData  = new TransUnitData(transUnitList[0]);
            TransUnitData secondTestData = new TransUnitData(transUnitList[1]);


            DoublyLinkedList doublyLinkedList = new DoublyLinkedList();

            doublyLinkedList.InsertNext(firstTestData);

            // Assertions set.
            Assert.AreEqual(-1, doublyLinkedList.GetIndexOf(secondTestData));
        }
Пример #42
0
        public void WhenListIsCleared_ThenStateIsEquivalentToEmptyCtor()
        {
            var list = new DoublyLinkedList <int>(new[] { 1, 2, 3, 4, 5 });

            list.Clear();

            list.First.Should().BeNull();
            list.Last.Should().BeNull();
            list.Count.Should().Be(0);

            var iterations = 0;

            foreach (var node in list)
            {
                iterations++;
            }

            iterations.Should().Be(0);
        }
Пример #43
0
        public void Reverse(IReadOnlyList <int> collection, IReadOnlyList <int> expected)
        {
            // Arrange
            var list = new DoublyLinkedList <int>(collection);

            // Act
            var result = list.Reverse();

            // Assert
            result.Version.Should().Be(0);
            result.Count.Should().Be(list.Count);
            // result.EnumerateForward().Should().AllBeEquivalentTo(
            //     new { List = result },
            //     options => options
            //         .Including(o => o.List));
            result.EnumerateForward().Should().NotBeSameAs(list.EnumerateForward());
            result.EnumerateForward().Should().Equal(expected);
            result.EnumerateReversed().Should().Equal(collection);
        }
Пример #44
0
            public void TestInsert()
            {
                // Given
                DoublyLinkedList <int> list = new DoublyLinkedList <int>();

                list.Add(1);
                list.Add(2);
                list.Add(3);
                // When
                list.Insert(index: 0, value: 5);
                list.Insert(index: 2, value: 4);
                // Then
                Assert.That(list.Length(), Is.EqualTo(5));
                Assert.That(list.Get(0), Is.EqualTo(5));
                Assert.That(list.Get(1), Is.EqualTo(1));
                Assert.That(list.Get(2), Is.EqualTo(4));
                Assert.That(list.Get(3), Is.EqualTo(2));
                Assert.That(list.Get(4), Is.EqualTo(3));
            }
        void AddItem(IReadOnlyList <int> collection, int item, IReadOnlyList <int> expected)
        {
            // Arrange
            var list    = new DoublyLinkedList <int>(collection);
            var version = list.Version;

            // Act
            list.AddLast(item);

            // Assert
            list.Version.Must()
            .BeNotEqualTo(version);
            list.EnumerateForward().Must()
            .BeEnumerableOf <int>()
            .BeEqualTo(expected);
            list.EnumerateReversed().Must()
            .BeEnumerableOf <int>()
            .BeEqualTo(expected.Reverse());
        }
Пример #46
0
            public void should_delete_the_value_if_it_exists()
            {
                var list = new DoublyLinkedList<int>();
                list.Add(5);
                list.Add(2);
                list.Add(3);
                list.Delete(2);
                Assert.That(list.Tail.Data.Equals(3), Is.True);
                Assert.That(list.Head.Data.Equals(5), Is.True);
                Assert.That(list.Head.Next.Data.Equals(3), Is.True);

                list.Delete(5);
                Assert.That(list.Tail.Data.Equals(3), Is.True);
                Assert.That(list.Head.Data.Equals(3), Is.True);

                list.Delete(3);
                Assert.That(list.Tail, Is.Null);
                Assert.That(list.Head, Is.Null);
            }
Пример #47
0
            public void TestInsertString()
            {
                // Given
                DoublyLinkedList <string> list = new DoublyLinkedList <string>();

                list.Add("1");
                list.Add("2");
                list.Add("3");
                // When
                list.Insert(index: 0, value: "5");
                list.Insert(index: 2, value: "4");
                // Then
                Assert.That(list.Length(), Is.EqualTo(5));
                Assert.That(list.Get(0), Is.EqualTo("5"));
                Assert.That(list.Get(1), Is.EqualTo("1"));
                Assert.That(list.Get(2), Is.EqualTo("4"));
                Assert.That(list.Get(3), Is.EqualTo("2"));
                Assert.That(list.Get(4), Is.EqualTo("3"));
            }
Пример #48
0
        public void Add_ToList_IncrementsCountAndAddsItem(List <int> data)
        {
            // Arrange
            var list = new DoublyLinkedList <int>();

            // Act
            var i = 0;

            foreach (var item in data)
            {
                // Act
                list.Add(item);
                i++;

                // Assert
                Assert.Equal(i, list.Count);
                Assert.Equal(item, list[i - 1]);
            }
        }
        private void shrink()
        {
            if (Math.Abs(filledBuckets - bucketSize * 0.3) < tolerance && bucketSize / 2 > initialBucketSize)
            {
                filledBuckets = 0;
                //reduce array by half
                var newBucketSize = bucketSize / 2;

                var smallerArray = new DoublyLinkedList <HashSetNode <T> > [newBucketSize];

                for (int i = 0; i < bucketSize; i++)
                {
                    var item = hashArray[i];

                    //hashcode changes when bucket size changes
                    if (item?.Head != null)
                    {
                        var current = item.Head;

                        //find new location for each item
                        while (current != null)
                        {
                            var next = current.Next;

                            var newIndex = Math.Abs(current.Data.Value.GetHashCode()) % newBucketSize;

                            if (smallerArray[newIndex] == null)
                            {
                                filledBuckets++;
                                smallerArray[newIndex] = new DoublyLinkedList <HashSetNode <T> >();
                            }

                            smallerArray[newIndex].InsertFirst(current);

                            current = next;
                        }
                    }
                }

                hashArray = smallerArray;
            }
        }
Пример #50
0
        private static void testDoublyLinkedList()
        {
            DoublyLinkedList <int> dlink = new DoublyLinkedList <int>(); // 创建双向链表

            dlink.AppendEnd(100);
            dlink.Insert(0, 10);
            dlink.ShowAll();
            dlink.Insert(0, 30);
            dlink.ShowAll();
            dlink.Insert(0, 40);
            dlink.AppendEnd(100);
            dlink.ShowAll();
            dlink.Insert(1, 20);
            dlink.ShowAll();
            dlink.DelFirst();
            dlink.DelLast();
            dlink.Del(2);
            dlink.ShowAll();
            Console.ReadKey();
        }
Пример #51
0
        public void RemoveFrontTest()
        {
            var list = new DoublyLinkedList <string>();

            list.AddBack("Alpha");
            list.AddBack("Beta");
            list.AddBack("Charlie");
            list.RemoveFront();

            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("Beta", list.GetAt(0));
            Assert.AreEqual("Charlie", list.GetAt(1));
            string shouldBeBeta = list.RemoveFront();

            Assert.AreEqual("Beta", shouldBeBeta);
            string shouldBeCharlie = list.RemoveFront();

            Assert.AreEqual("Charlie", shouldBeCharlie);
            Assert.AreEqual(0, list.Count);
        }
Пример #52
0
        public void TestRemoveAt1()
        {
            DoublyLinkedList <int> list = new DoublyLinkedList <int>();

            list.add(1, 0);
            list.add(4, 1);
            list.add(2, 1);
            list.add(3, 2);

            list.removeAt(1);
            Assert.AreEqual(3, list.size);
            Assert.AreEqual(1, list.get(0));
            Assert.AreEqual(3, list.get(1));
            Assert.AreEqual(4, list.get(2));

            list.removeAt(2);
            Assert.AreEqual(2, list.size);
            Assert.AreEqual(1, list.get(0));
            Assert.AreEqual(3, list.get(1));
        }
        public void TestDoublyLinkedListForEach()
        {
            DoublyLinkedList <string> list = new DoublyLinkedList <string>();

            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";

            string[] array = new string[list.Count];
            int      index = 0;

            foreach (string item in list)
            {
                array[index] = item;
                index++;
            }

            Assert.AreEqual("Zero, Two, Three", string.Join(", ", array));
        }
Пример #54
0
        public void RemovingByValueTest()
        {
            int[] values = new int[6] {
                1, 2, 3, 4, 5, 6
            };
            DoublyLinkedList <int> testList = new DoublyLinkedList <int>();

            for (int i = 0; i < 6; i++)
            {
                testList.Add(values[i]);
            }
            Assert.AreEqual(testList.DeleteByValue(2), 0);
            Assert.AreEqual(testList.DeleteByValue(2), 1);
            Assert.AreEqual(testList.GetByIndex(0), values[0]);
            for (int i = 1; i < 5; i++)
            {
                Assert.AreEqual(testList.GetByIndex(i), values[i + 1]);
            }
            Assert.AreEqual(testList.Length, 5);
        }
Пример #55
0
        public void DoublyLinkedList_InsertNext_Test_1()
        {
            // Initialization.
            XmlDocument doc = new XmlDocument();

            doc.Load(xliffPath);

            XmlNodeList   transUnitList = doc.GetElementsByTagName("trans-unit");
            TransUnitData testData      = new TransUnitData(transUnitList[0]);


            DoublyLinkedList doublyLinkedList = new DoublyLinkedList();

            doublyLinkedList.InsertNext(testData);

            // Assertions set.
            Assert.AreEqual(1, doublyLinkedList.Count);
            Assert.AreEqual(testData, doublyLinkedList.Head.Data);
            Assert.AreEqual(testData, doublyLinkedList.Tail.Data);
        }
        public void RemoveTest()
        {
            //Arrange
            var doublyList = new DoublyLinkedList <int>();

            doublyList.Add(11);
            doublyList.Add(12);
            doublyList.Add(13);
            doublyList.Add(14);

            //Act
            doublyList.Remove(14);
            doublyList.Remove(11);

            //Assert
            Assert.AreEqual(13, doublyList.Tail.Data);
            Assert.AreEqual(2, doublyList.Count);
            Assert.AreEqual(12, doublyList.Head.Data);
            Assert.AreEqual(null, doublyList.Head.PrevCell);
        }
        public void CloneTest()
        {
            //Arrange
            var list = new DoublyLinkedList <int>();

            list.Add(11);
            list.Add(12);
            list.Add(13);
            list.Add(14);

            //Act
            var list2 = list.Clone();

            list.Add(18);

            //Assert
            Assert.AreNotEqual(list.Count, list2.Count);
            Assert.AreEqual(list.Head.Data, list2.Head.Data);
            Assert.AreNotEqual(list.Head, list2.Head);
        }
Пример #58
0
        static void Main(string[] args)
        {
            // Lists here will be hard-coded for the time being.
            // For example, given A = 3 -> 7 -> 3 should return true (i.e. palindrome list), while 3 -> 7 should return false.

            DoublyLinkedList list = new DoublyLinkedList();

            list.appendNode(3);
            list.appendNode(7);
            list.appendNode(3);

            Console.WriteLine($"List 1 is a palindrome: {list.isDLLPalindrome()}.");

            DoublyLinkedList list2 = new DoublyLinkedList();

            list2.appendNode(3);
            list2.appendNode(7);

            Console.WriteLine($"List 2 is a palindrome: {list2.isDLLPalindrome()}.");
        }
Пример #59
0
        public void RemoveLast()
        {
            DoublyLinkedList <int> dll = new DoublyLinkedList <int>();

            dll.AddFirst(new DoublyLinkedListNode <int>(10));
            dll.AddFirst(new DoublyLinkedListNode <int>(20));
            dll.AddLast(new DoublyLinkedListNode <int>(30));
            dll.AddLast(new DoublyLinkedListNode <int>(40));

            //20 <-> 10 <-> 30 <-> 40   : Head 20, Tail 40

            dll.RemoveFirst(); // 10 <-> 30 <-> 40   : Head 10, Tail 40
            dll.RemoveLast();  // 10 <-> 30   : Head 10, Tail 30


            Assert.AreEqual(dll.Head.Value, 10);
            Assert.AreEqual(dll.Head.Next.Value, 30);
            Assert.AreEqual(dll.Tail.Value, 30);
            Assert.AreEqual(dll.Tail.Previous.Value, 10);
        }
        public void Should_remove_node_from_end_of_doubly_linked_list_having_multiple_nodes()
        {
            // arrange
            var card4 = Card.Hearts(4);
            var card6 = Card.Hearts(6);

            var list = new DoublyLinkedList <Card>();

            // act
            list.AddFront(card4);
            list.AddFront(card6);
            var removedNode = list.RemoveEnd();

            // assert
            Assert.AreEqual(expected: 1, actual: list.Count);
            Assert.AreEqual(expected: card6, actual: list.Head.Value);
            Assert.AreEqual(expected: card4, actual: removedNode.Value);
            Assert.AreEqual(expected: null, actual: removedNode.Next);
            Assert.AreEqual(expected: card6, actual: list.Tail.Value);
        }