public void AddLastTest()
        {
            var list = new SingleLinkedList<int>();
            list.AddLast(2);
            list.AddLast(4);
            list.AddLast(6);

            Assert.AreEqual(list.Head.Value, 2);
            Assert.AreEqual(list.Tail.Value, 6);
        }
        public void ContainsTest()
        {
            var list = new SingleLinkedList<int>();
            list.AddLast(2);
            list.AddLast(4);
            list.AddLast(6);

            Assert.AreEqual(list.Contains(2), true);
            Assert.AreEqual(list.Contains(4), true);
            Assert.AreEqual(list.Contains(6), true);
        }
        public void AddFirst_and_AddLast_OneItem_CorrectState()
        {
            list.AddFirst(1);

            CheckStateWithSingleNode(list);

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

            CheckStateWithSingleNode(list);
        }
        public void RemoveTest()
        {
            var list = new SingleLinkedList<int>();
            list.AddLast(2);
            list.AddLast(4);
            list.AddLast(6);

            list.Remove(4);

            Assert.AreEqual(list.Contains(4), false);
        }
예제 #5
0
        public void Add_Last_Test()
        {
            ILinkedList <char> linkedList = new SingleLinkedList <char>();

            linkedList.IsEmpty.Should().BeTrue();
            linkedList.AddLast('A');
            linkedList.First.Value.Should().Be('A');
            linkedList.Last.Value.Should().Be('A');
            linkedList.AddLast('B');
            linkedList.Last.Value.Should().Be('B');
            linkedList.First.Value.Should().Be('A');
            linkedList.Count.Should().Be(2);
        }
        public void RemoveFirstTest()
        {
            var list = new SingleLinkedList<int>();
            list.AddLast(2);
            list.AddLast(4);
            list.AddLast(6);

            Assert.AreEqual(list.Head.Value, 2);

            list.RemoveFirst();

            Assert.AreEqual(list.Head.Value, 4);
        }
예제 #7
0
        public void CountLinkedList()
        {
            //Arrange
            SingleLinkedList <int> l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);

            //Act
            var c = l.Count();

            //Assert
            Assert.AreEqual(c, 2);
        }
예제 #8
0
        public void InsertBeforeNonExistentElementThrowsException()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            //Act
            //Assert
            Assert.Throws <ArgumentException>(() => l.AddBeforeElement(9, 5));
        }
예제 #9
0
        public void InsertElementAtOutOfBoundsPositionThrowsException()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            //Act
            //Assert
            Assert.Throws <ArgumentException>(() => l.AddAtPosition(9, 6));
        }
예제 #10
0
        public void AddToEndOfLinkedList()
        {
            //Arrange
            SingleLinkedList <int> l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);

            //Act
            l.AddLast(3);

            //Assert
            Assert.AreEqual(l.Contains(3), true);
            Assert.AreEqual(l.GetElementPosition(3), 3);
        }
예제 #11
0
        public void DeleteNonExistentElementThrowsException()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            Assert.AreEqual(l.Contains(5), false);

            //Act
            //Assert
            Assert.Throws <ArgumentException>(() => l.Delete(5));
        }
예제 #12
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);
            //
        }
예제 #13
0
        public void AddLastTest(int[] original, int addValue, int[] expected)
        {
            SingleLinkedList sList = new SingleLinkedList(original);

            sList.AddLast(addValue);
            Assert.AreEqual(expected, sList.ToArray());
        }
예제 #14
0
        public void InsertElementAtLastPosition()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            //Act
            l.AddAtPosition(9, 5);

            //Assert
            Assert.AreEqual(l.Contains(9), true);
            Assert.AreEqual(l.GetElementPosition(9), 5);
        }
예제 #15
0
    void TestAddRemove()
    {
        var sll = new SingleLinkedList <string>();

        print("Test Add");
        sll.AddLast("Foo");
        sll.AddLast("Bar");
        sll.AddLast("Baz");
        print("Count: " + sll.Count + ", Value at 1: " + sll.GetValue(1));
        print("Expected Count: 3, Value: Bar");
        print(sll);
        print("Test Remove");
        sll.Remove("Bar");
        print("Count: " + sll.Count + ", Value at 1: " + sll.GetValue(1));
        print("Expected Count: 2, Value: Baz");
        print(sll);
    }
        public void KthNodeFromTheEnd(int k, int value)
        {
            // ARRANGE
            var list = new SingleLinkedList();

            list.AddLast(10);
            list.AddLast(20);
            list.AddLast(30);
            list.AddLast(40);
            list.AddLast(50);

            // ACT
            var kth = list.KthNodeFromTheEnd_OnePass(k);

            // ASSERT
            Assert.Equal(value, kth.Value);
        }
예제 #17
0
        public void InsertAfterLastElementOfLinkedList()
        {
            //Arrange
            SingleLinkedList <int> l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            //Act
            l.AddAfterElement(9, 4);

            //Assert
            Assert.AreEqual(l.Contains(9), true);
            Assert.AreEqual(l.GetElementPosition(9), 5);
        }
예제 #18
0
        public void DeleteLastElement()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);
            l.AddLast(4);

            Assert.AreEqual(l.Contains(4), true);

            //Act
            l.Delete(4);

            //Assert
            Assert.AreEqual(l.Contains(4), false);
        }
예제 #19
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);
        }
예제 #20
0
    void TestAddBeforeAfter()
    {
        var sll = new SingleLinkedList <string>();

        print("Test Add Before/After");
        sll.AddLast("Baz");
        sll.AddBefore(sll.First, "Foo");
        sll.AddBefore(sll.GetNode(1), "Bar");
        print("Count: " + sll.Count + ", Value at 1: " + sll.GetValue(1));
        print("Expected Count: 3, Value: Bar");
        print(sll);
    }
예제 #21
0
        public void ReverseLinkedList()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            l.AddLast(2);
            l.AddLast(3);

            Assert.AreEqual(l.GetElementPosition(1), 1);
            Assert.AreEqual(l.GetElementPosition(2), 2);
            Assert.AreEqual(l.GetElementPosition(3), 3);

            //Act
            l.Reverse();

            //Assert
            Assert.AreEqual(l.GetElementPosition(1), 3);
            Assert.AreEqual(l.GetElementPosition(2), 2);
            Assert.AreEqual(l.GetElementPosition(3), 1);
        }
예제 #22
0
        public void DeleteFirstNodeOneElementList()
        {
            //Arrange
            var l = new SingleLinkedList <int>();

            l.AddLast(1);
            Assert.AreEqual(l.Contains(1), true);

            //Act
            l.DeleteFirstNode();

            //Assert
            Assert.AreEqual(l.Contains(1), false);
        }
예제 #23
0
        public static void ReadCollectionElements(ref SingleLinkedList singleLinkedList, int singleLinkedListLength, ref int index)
        {
            for (int i = 0; i < singleLinkedListLength; i++)
            {
                try
                {
                    OutputHandling.Question("Element " + i + ": ", false);
                    singleLinkedList.AddLast(singleLinkedList, ReadValue(""));
                }

                catch
                {
                    OutputHandling.Error("Invalid input value! Please use an integer");
                    index = i--;
                }
            }
        }
예제 #24
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
        }
예제 #25
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);
            }
        }
예제 #26
0
 public void Enqueue(int item)
 {
     Items.AddLast(item);
 }
예제 #27
0
 public void Enqueue(T item)
 {
     _items.AddLast(item);
 }
예제 #28
0
        public void AddLast_CalledManyTimes_CountReturnsCorrectValue(int[] numbers)
        {
            foreach (int number in numbers)
            {
                _list.AddLast(number);
            }

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