예제 #1
0
        public void Remove_Last_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();

            linkedList.AddFirst('A');
            linkedList.AddFirst('B');
            linkedList.AddFirst('C');
            linkedList.AddFirst('D');
            linkedList.First.Value.Should().Be('D');
            linkedList.Count.Should().Be(4);
            linkedList.Last.Value.Should().Be('A');

            linkedList.RemoveLast();
            linkedList.Last.Value.Should().Be('B');
            linkedList.Count.Should().Be(3);
            linkedList.RemoveLast();
            linkedList.Last.Value.Should().Be('C');
            linkedList.Count.Should().Be(2);
            linkedList.RemoveLast();
            linkedList.Last.Value.Should().Be('D');
            linkedList.Count.Should().Be(1);
            linkedList.RemoveLast();
            linkedList.Last.Should().BeNull();
            linkedList.First.Should().BeNull();
            linkedList.Count.Should().Be(0);
            linkedList.IsEmpty.Should().BeTrue();

            Action act = () => linkedList.RemoveLast();

            act.Should().Throw <IndexOutOfRangeException>()
            .WithMessage("Linked list is empty.");
        }
        public void AddFirstTest()
        {
            var list = new SingleLinkedList<int>();
            list.AddFirst(2);
            list.AddFirst(4);
            list.AddFirst(6);

            Assert.AreEqual(list.Head.Value, 6);
            Assert.AreEqual(list.Tail.Value, 2);
        }
        public void AddFirst_and_AddLast_OneItem_CorrectState()
        {
            list.AddFirst(1);

            CheckStateWithSingleNode(list);

            list.RemoveFirst();
            list.AddLast(1);

            CheckStateWithSingleNode(list);
        }
예제 #4
0
        public void Find_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');
            var b = linkedList.AddFirst('B');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.Find('B').Value.Should().Be('B');
            linkedList.Find('E').Should().BeNull();
        }
예제 #5
0
        public void Clear_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');
            var b = linkedList.AddFirst('B');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.Clear();
            linkedList.First.Should().BeNull();
            linkedList.Count.Should().Be(0);
            linkedList.IsEmpty.Should().BeTrue();
        }
예제 #6
0
        public void Reverse_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');
            var b = linkedList.AddFirst('B');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.Reverse();
            linkedList.First.Value.Should().Be('A');
            linkedList.First.Next.Value.Should().Be('B');
            linkedList.First.Next.Next.Value.Should().Be('C');
            linkedList.First.Next.Next.Next.Value.Should().Be('D');
        }
예제 #7
0
        public void AddToFrontOfLinkedList()
        {
            //Arrange
            SingleLinkedList <int> l = new SingleLinkedList <int>();

            l.AddFirst(1);
            l.AddFirst(2);

            //Act
            l.AddFirst(3);

            //Assert
            Assert.AreEqual(l.Contains(3), true);
            Assert.AreEqual(l.GetElementPosition(3), 1);
        }
예제 #8
0
        public void Contains_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');
            var b = linkedList.AddFirst('B');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.First.Value.Should().Be('D');
            linkedList.Count.Should().Be(4);
            linkedList.Contains('A').Should().BeTrue();
            linkedList.Contains('C').Should().BeTrue();
            linkedList.Contains('D').Should().BeTrue();
            linkedList.Contains('E').Should().BeFalse();
        }
예제 #9
0
        public void AddFirstArrayTest(int[] original, int[] values, int[] expected)
        {
            SingleLinkedList sList = new SingleLinkedList(original);

            sList.AddFirst(values);
            Assert.AreEqual(expected, sList.ToArray());
        }
예제 #10
0
        static void ListTest()
        {
            SingleLinkedList <ImageNode> imageList = new SingleLinkedList <ImageNode>();

            imageList = new SingleLinkedList <ImageNode>();
            for (int i = 0; i < 20; i++)
            {
                var       name = "bild-" + i + ".png";
                ImageNode n    = new ImageNode();
                n.Data = name;
                imageList.AddLast(n);
            }
            Console.WriteLine(imageList);
            var addFirst  = new ImageNode("addFirst.png");
            var addAfter  = new ImageNode("AddAfter.png");
            var addBefore = new ImageNode("AddBefore.png");

            imageList.AddFirst(addFirst);
            imageList.AddAfter(addFirst, addAfter);
            imageList.AddBefore(addFirst, addBefore);
            Console.WriteLine(imageList);

            imageList.Remove(addFirst);
            Console.WriteLine(imageList);
            imageList.Remove(addAfter);
            Console.WriteLine(imageList);
            imageList.Remove(addBefore);
            Console.WriteLine(imageList);
            //
        }
예제 #11
0
        static void Main(string[] args)
        {
            #region LinkedList
            SingleLinkedList <int>     singleLL = new SingleLinkedList <int>();
            SingleLinkedListNode <int> snode1   = new SingleLinkedListNode <int>(20);
            singleLL.AddFirst(snode1); singleLL.Add(30); singleLL.AddLast(90); singleLL.AddFirst(11);
            singleLL.PrintList("");

            DoubleLinkedList <int>     doubleLL = new DoubleLinkedList <int>();
            DoubleLinkedListNode <int> node1    = new DoubleLinkedListNode <int>(45);
            doubleLL.AddFirst(node1); doubleLL.AddLast(90); doubleLL.Add(33); doubleLL.Add(30); doubleLL.Add(31);
            doubleLL.PrintList("");

            #endregion

            #region Stack
            // To Evaluate PostFix Expression.
            PostFixCalculator();
            #endregion

            #region Queue
            QueueUsingArray <int> queue1 = new QueueUsingArray <int>();
            queue1.Enqueue(50); queue1.Enqueue(60); queue1.Enqueue(70); queue1.Enqueue(80); queue1.Enqueue(90);
            queue1.Dequeue(); queue1.Dequeue();
            queue1.Enqueue(50); queue1.Enqueue(60); queue1.Enqueue(70); queue1.Enqueue(80); queue1.Enqueue(90);
            queue1.Dequeue(); queue1.Dequeue();
            queue1.PrintQueue();

            QueueUsingList <int> queue2 = new QueueUsingList <int>();
            queue2.Enqueue(50); queue2.Enqueue(60); queue2.Enqueue(70); queue2.Enqueue(80); queue2.Enqueue(90);
            queue2.Dequeue(); queue2.Dequeue();
            queue2.Enqueue(50); queue2.Enqueue(60); queue2.Enqueue(70); queue2.Enqueue(80); queue2.Enqueue(90);
            queue2.Dequeue(); queue2.Dequeue();
            queue2.PrintQueue();
            #endregion

            #region Trees



            #endregion
        }
예제 #12
0
        public void TestSingleLinkedList()
        {
            SingleLinkedList <int> singleLinkedList = new SingleLinkedList <int>();

            singleLinkedList.AddLast(10);
            singleLinkedList.AddFirst(5);
            singleLinkedList.InsertAfter(5, 8);
            singleLinkedList.InsertBefore(8, 6);
            // SingleLinkedList.Display();

            Assert.IsTrue(singleLinkedList.Head.Next.Data == 5);
        }
예제 #13
0
        public void ShouldGetMaxWithOneElement()
        {
            // Given
            var list = new SingleLinkedList <double>();
            var max  = 80;

            // When
            list.AddFirst(max);
            var result = list.Max();

            // Then
            result.ShouldBe(max);
        }
예제 #14
0
        public void Add_First_Simple_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');

            linkedList.First.Value.Should().Be('A');
            linkedList.Last.Value.Should().Be('A');
            var b = linkedList.AddFirst('B');

            linkedList.First.Value.Should().Be('B');
            linkedList.Last.Value.Should().Be('A');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.First.Value.Should().Be('D');
            linkedList.Count.Should().Be(4);
            d.Next.Value.Should().Be('C');
            c.Next.Value.Should().Be('B');
            b.Next.Value.Should().Be('A');
            a.Next.Should().BeNull();
            linkedList.Last.Value.Should().Be('A');
        }
예제 #15
0
        public void ShouldFind()
        {
            // Given
            var list          = new SingleLinkedList <string>();
            var expectedValue = "value";

            // When
            list.AddFirst(expectedValue);
            var result = list.Find(expectedValue);

            // Then
            result.ShouldBe(true);
        }
예제 #16
0
        public void Remove_First_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();
            var a = linkedList.AddFirst('A');
            var b = linkedList.AddFirst('B');
            var c = linkedList.AddFirst('C');
            var d = linkedList.AddFirst('D');

            linkedList.RemoveFirst();
            d.Next.Should().BeNull();
            linkedList.First.Value.Should().Be('C');
            linkedList.Last.Value.Should().Be('A');
            linkedList.Count.Should().Be(3);
            linkedList.RemoveFirst();
            c.Next.Should().BeNull();
            linkedList.First.Value.Should().Be('B');
            linkedList.Last.Value.Should().Be('A');
            linkedList.Count.Should().Be(2);
            linkedList.RemoveFirst();
            b.Next.Should().BeNull();
            linkedList.First.Value.Should().Be('A');
            linkedList.Last.Value.Should().Be('A');
            ReferenceEquals(linkedList.Last, linkedList.First).Should().BeTrue();
            linkedList.Count.Should().Be(1);
            linkedList.RemoveFirst();
            a.Next.Should().BeNull();
            linkedList.First.Should().BeNull();
            linkedList.Last.Should().BeNull();
            linkedList.Count.Should().Be(0);
            linkedList.IsEmpty.Should().BeTrue();

            Action act = () => linkedList.RemoveFirst();

            act.Should().Throw <IndexOutOfRangeException>()
            .WithMessage("Linked list is empty.");
        }
예제 #17
0
        public void ShouldReverseRecursive(int capacity)
        {
            // Given
            var list         = new SingleLinkedList <int>();
            var expectedList = Enumerable.Range(0, capacity).ToList();

            // When
            for (var i = 0; i < capacity; ++i)
            {
                list.AddFirst(i);
            }
            list.ReverseRecursive();

            // Then
            list.ShouldBeEquivalentTo(expectedList, options => options.WithStrictOrdering());
        }
예제 #18
0
        public void ShouldAppend()
        {
            // Given
            var capacity     = 10;
            var linkedList   = new SingleLinkedList <string>();
            var expectedList = Enumerable.Range(0, capacity).Select(x => x.ToString()).Reverse().ToList();

            // When
            for (int i = 0; i < capacity; i++)
            {
                linkedList.AddFirst(i.ToString());
            }

            // Then
            linkedList.ShouldBeEquivalentTo(expectedList, options => options.WithStrictOrdering());
        }
예제 #19
0
        public void ShouldSort()
        {
            // Given
            var capacity      = 50;
            var list          = new SingleLinkedList <int>();
            var expectedList  = Enumerable.Range(0, capacity).ToArray();
            var randomNumbers = expectedList.ToArray();

            randomNumbers.Shuffle();

            foreach (var number in randomNumbers)
            {
                list.AddFirst(number);
            }

            // When
            list.Sort();

            // Then
            list.ShouldBeEquivalentTo(expectedList, options => options.WithStrictOrdering());
        }
예제 #20
0
        public void ShouldSortWithComparer()
        {
            // Given
            var capacity     = 50;
            var list         = new SingleLinkedList <KeyValuePair <int, string> >();
            var expectedList = Enumerable.Range(0, capacity)
                               .Select(x => new KeyValuePair <int, string>(x, x.ToString()))
                               .ToArray();
            var randomNumbers = expectedList.ToArray();

            randomNumbers.Shuffle();

            foreach (var number in randomNumbers)
            {
                list.AddFirst(number);
            }

            // When
            list.Sort(x => x.Key);

            // Then
            list.ShouldBeEquivalentTo(expectedList, options => options.WithStrictOrdering());
        }
예제 #21
0
        public void ShouldGetLastMax()
        {
            // Given
            var list = new SingleLinkedList <double>();
            var max  = 80;

            // When
            list.AddFirst(max);
            list.AddFirst(23);
            list.AddFirst(56);
            list.AddFirst(3);
            list.AddFirst(1);
            list.AddFirst(34);
            var result = list.Max();

            // Then
            result.ShouldBe(max);
        }
 public void Push(int item)
 {
     Items.AddFirst(item);
 }
예제 #23
0
        static void DSSingleLinkedListMain(string[] args)
        {
            try
            {
                SingleLinkedList singleLinkedList = new SingleLinkedList();

                Console.WriteLine("Effect of Add First (30,20, 10)");
                singleLinkedList.AddFirst(30);
                singleLinkedList.AddFirst(20);
                singleLinkedList.AddFirst(10);
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Add Last(40,50)");
                singleLinkedList.AddLast(40);
                singleLinkedList.AddLast(50);
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Add At position(3) value(25)");
                singleLinkedList.AddAt(25, 3);
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Remove At Position(3) value(25)");
                singleLinkedList.RemoveAt(3);
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Get Kth Node From End( get 3rd node)");
                int result = singleLinkedList.GetKthNodeFromEnd(3);
                Console.WriteLine("3rd node from end is: {0}", result);

                Console.WriteLine();
                Console.WriteLine("Effect of Print Middle Node (when size is odd)");
                singleLinkedList.PrintMiddleNode();

                Console.WriteLine();
                Console.WriteLine("Effect of Print Middle Node (when size is even)");
                Console.WriteLine("Adding 1 more node 60 at the end");
                singleLinkedList.AddLast(60);
                singleLinkedList.PrintMiddleNode();
                singleLinkedList.RemoveLast();

                Console.WriteLine();
                Console.WriteLine("Effect of Delete Middle Node (when size is odd)");
                singleLinkedList.RemoveMiddleNode();
                singleLinkedList.Print();
                singleLinkedList.AddAt(30, 3);

                Console.WriteLine();
                Console.WriteLine("Effect of Delete Middle Node (when size is even)");
                Console.WriteLine("Adding 1 more node 60 at the end");
                singleLinkedList.AddLast(60);
                singleLinkedList.RemoveMiddleNode();
                singleLinkedList.Print();
                singleLinkedList.AddAt(30, 3);
                singleLinkedList.AddAt(40, 4);
                singleLinkedList.RemoveLast();

                Console.WriteLine();
                Console.WriteLine("Effect of Remove First(10)");
                singleLinkedList.RemoveFirst();
                singleLinkedList.Print();
                singleLinkedList.AddFirst(10);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove Last(50)");
                singleLinkedList.RemoveLast();
                singleLinkedList.Print();
                singleLinkedList.AddLast(50);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove Even Nodes");
                singleLinkedList.RemoveEvenNodes();
                singleLinkedList.Print();
                singleLinkedList.AddAt(20, 2);
                singleLinkedList.AddAt(40, 4);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove Odd Nodes");
                singleLinkedList.RemoveOddNodes();
                singleLinkedList.Print();
                singleLinkedList.AddAt(10, 1);
                singleLinkedList.AddAt(30, 3);
                singleLinkedList.AddAt(50, 5);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove First Node By Value(30)");
                singleLinkedList.RemoveFirstNodeByValue(30);
                singleLinkedList.Print();
                singleLinkedList.AddAt(30, 3);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove Last Node By Value(30)");
                singleLinkedList.RemoveLastNodeByValue(30);
                singleLinkedList.Print();
                singleLinkedList.AddAt(30, 3);

                Console.WriteLine();
                Console.WriteLine("Effect of Remove All Nodes By Value(30)");
                Console.WriteLine("Adding 3 more 30's in different places");
                singleLinkedList.AddAt(30, 5);
                singleLinkedList.AddAt(30, 2);
                singleLinkedList.AddAt(30, 1);
                singleLinkedList.Print();
                singleLinkedList.RemoveAllNodesByValue(30);
                Console.WriteLine("After Remove");
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Reverse Linked List");
                singleLinkedList.Reverse();
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Effect of Remove All");
                singleLinkedList.RemoveAll();
                singleLinkedList.Print();

                Console.WriteLine();
                Console.WriteLine("Check the given List has loop or not");
                SingleLinkedList list = SingleLinkedList.CreateWithLoop();
                Console.WriteLine("Is List is haivng loop: {0}", list.HasLoop());

                Console.ReadKey();

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #24
0
        public void AddFirst_CalledManyTimes_CountReturnsCorrectValue(int[] numbers)
        {
            foreach (int number in numbers)
            {
                _list.AddFirst(number);
            }

            Assert.That(_list.Count, Is.EqualTo(numbers.Length));
        }