Пример #1
0
 public void CopyToWithLessSpaceInTheArray()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     int[] array = new int[3];
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.CopyTo(array, 2);
 }
Пример #2
0
 public void ClearList()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.Clear();
     list.ShouldBeEmpty();
 }
Пример #3
0
 public void Contains()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     bool isTrue = list.Contains(7);
     Assert.AreEqual(true, isTrue);
 }
Пример #4
0
 public void CopyToWithNullArray()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     int[] array = new int[0];
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.CopyTo(array, 0);
 }
Пример #5
0
 public void CopyToWithNegativeIndex()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     int[] array = new int[3];
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.CopyTo(array, -2);
 }
Пример #6
0
        public void SimpleLinkedListTest()
        {
            SimpleLinkedList node = new SimpleLinkedList();

            node.Add(5);
            node.Add(6);
            node.Add(12);

            //get the head Node from the Linked List
            SimpleLinkedList.Node currentNode;
            currentNode = node.GetCurrentNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(5, currentNode.Data);

            //get the next Node from the Linked List
            var nextNode = node.GetNext();

            currentNode = nextNode;
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(6, currentNode.Data);

            //get the following node
            nextNode    = node.GetNext();
            currentNode = nextNode;
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(12, currentNode.Data);

            nextNode    = node.GetNext();
            currentNode = nextNode;
            Assert.IsNotNull(currentNode);

            //test removing node from list
            currentNode = node.Head();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(5, currentNode.Data);

            currentNode = node.GetNext();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(6, currentNode.Data);
            node.RemoveCurrentNode();
            currentNode = node.GetCurrentNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(12, currentNode.Data);

            //remove head from list
            currentNode = node.Head();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(5, currentNode.Data);
            node.RemoveCurrentNode();
            currentNode = node.GetCurrentNode();
            Assert.IsNotNull(currentNode);
            Assert.AreEqual(12, currentNode.Data);
        }
Пример #7
0
        static void Main(string[] args)
        {
            SimpleLinkedList<int> list = new SimpleLinkedList<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);

            string listStr = list.ToString();
            Console.WriteLine(listStr);

            /* Remove an item */
            list.Remove(5);
            listStr = list.ToString();
            Console.WriteLine(listStr);
        }
Пример #8
0
        public void Test_SimpleLinkedList()
        {
            var linkedList = new SimpleLinkedList <int>();

            linkedList.Add(0);
            Assert.IsTrue(linkedList.Count == 1);
            Assert.IsTrue(linkedList.Contains(0));
            Assert.IsFalse(linkedList.Contains(1));

            linkedList.Remove(0);
            Assert.IsTrue(linkedList.Count == 0);
            linkedList.Add(1);
            Assert.IsTrue(linkedList.Count == 1);
            Assert.IsTrue(linkedList.Contains(1));
            Assert.IsFalse(linkedList.Contains(0));

            linkedList.Clear();

            var tempList = new List <int>();

            for (int i = 0; i < 1024 * 32; i++)
            {
                var v = _random.Next(0, 256);

                if (_random.Next(0, 2) == 0)
                {
                    linkedList.Add(v);
                    tempList.Add(v);
                }
                else
                {
                    linkedList.Remove(v);
                    tempList.Remove(v);
                }
            }

            {
                tempList.Sort();

                var tempList2 = linkedList.ToList();
                tempList2.Sort();

                Assert.IsTrue(CollectionUtilities.Equals(tempList, tempList2));
            }
        }
Пример #9
0
        static void Main(string[] args)
        {
            SimpleLinkedList <int> list = new SimpleLinkedList <int>();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            list.Add(5);

            string listStr = list.ToString();

            Console.WriteLine(listStr);

            /* Remove an item */
            list.Remove(5);
            listStr = list.ToString();
            Console.WriteLine(listStr);
        }
Пример #10
0
        public void TestHeadAssignmenet_RemoveFirstElementAndAddToFirstPosition_ElementShouldBeAssignedToSecondPosition()
        {
            var sll = new SimpleLinkedList<int> { 1, 2, 3, 4, 5 };
            sll.Remove(0);
            sll.Add(6, 0);

            Assert.AreEqual(5, sll.Count());
            Assert.AreEqual(6, sll.First());
            Assert.AreEqual(2, sll.Skip(1).First());
            Assert.AreEqual(3, sll.Skip(2).First());
            Assert.AreEqual(4, sll.Skip(3).First());
            Assert.AreEqual(5, sll.Skip(4).First());
            Assert.AreEqual(5, sll.Last());
        }
        /// <summary>
        /// Merge and dedup
        /// </summary>
        /// <param name="listA"></param>
        /// <param name="listB"></param>
        /// <returns></returns>
        private SimpleLinkedList MergeList(SimpleLinkedList listA, SimpleLinkedList listB)
        {
            var result = new SimpleLinkedList();

            var listANode = listA.Head;
            var listBNode = listB.Head;

            Node temp;

            while (listANode != null || listBNode != null)
            {
                if (listANode < listBNode)
                {
                    temp = listANode.NextNode;
                    result.Add(listANode);
                    listANode = temp;
                }
                else
                {
                    temp = listBNode.NextNode;
                    result.Add(listBNode);
                    listBNode = temp;
                }

                while (listANode != null && listANode.Value == result.LastNode.Value)
                {
                    listANode = listANode.NextNode;
                }
                while (listBNode != null && listBNode.Value == result.LastNode.Value)
                {
                    listBNode = listBNode.NextNode;
                }
            }

            return(result);
        }
Пример #12
0
        static void Main(string[] args)
        {
            #region Simple Linked
            //var firstNode = new SimpleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new SimpleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new SimpleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new SimpleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new SimpleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new SimpleLinkedNode<string>();
            //sixthNode.Data = "sexto";

            //var seventhNode = new SimpleLinkedNode<string>();
            //seventhNode.Data = "quarto";

            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;
            //sixthNode.Next = seventhNode;

            //var currentNode = firstNode;
            //while (currentNode != null)
            //{
            //    Console.WriteLine(currentNode.Data);
            //    currentNode = currentNode.Next;
            //}
            #endregion

            #region Circular
            //var firstNode = new SimpleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new SimpleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new SimpleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new SimpleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new SimpleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new SimpleLinkedNode<string>();
            //sixthNode.Data = "sexto";



            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;
            //sixthNode.Next = firstNode;

            //var currentNode = firstNode;
            //var loop = 0;
            //while (loop<3)
            //{
            //    if(currentNode == firstNode) Console.WriteLine("\nLoop #" + (loop+1));
            //    Console.WriteLine(currentNode.Data);
            //    if (currentNode == sixthNode) loop++;
            //    currentNode = currentNode.Next;
            //}
            #endregion

            #region Back And Forth
            //var firstNode = new DoubleLinkedNode<string>();
            //firstNode.Data = "primeiro";

            //var secondNode = new DoubleLinkedNode<string>();
            //secondNode.Data = "segundo";

            //var thirdNode = new DoubleLinkedNode<string>();
            //thirdNode.Data = "terceiro";

            //var fourthNode = new DoubleLinkedNode<string>();
            //fourthNode.Data = "quarto";

            //var fifthNode = new DoubleLinkedNode<string>();
            //fifthNode.Data = "quinto";

            //var sixthNode = new DoubleLinkedNode<string>();
            //sixthNode.Data = "sexto";

            //firstNode.Next = secondNode;
            //secondNode.Next = thirdNode;
            //thirdNode.Next = fourthNode;
            //fourthNode.Next = fifthNode;
            //fifthNode.Next = sixthNode;

            //secondNode.Previous = firstNode;
            //thirdNode.Previous = secondNode;
            //fourthNode.Previous = thirdNode;
            //fifthNode.Previous = fourthNode;
            //sixthNode.Previous = fifthNode;

            //var currentNode = firstNode;

            //var goBack = false;
            //var loop = 0;
            //while (currentNode != null)
            //{
            //    if (currentNode == firstNode && goBack && loop < 2)
            //    {
            //        goBack = false;
            //        loop++;
            //    }
            //    if (currentNode == firstNode) Console.WriteLine("\nLööp #" + (loop + 1));
            //    Console.WriteLine(currentNode.Data);
            //    if (currentNode == sixthNode) goBack = true;

            //    if (goBack) currentNode = currentNode.Previous;
            //    else currentNode = currentNode.Next;
            //}
            #endregion

            var list = new SimpleLinkedList <string>("First");
            list.Add("Second");
            list.Add("Third");
            list.Add("Forth");
            list.Add("Fifth");
            list.Add("Sixth");
            list.Add("Seventh");
            list.Add("Eighth");
            list.Add("Seventh");
            list.Add("Nineth");
            list.AddFirst("zero");
            list.Replace("Seventh", "Hello");
        }
Пример #13
0
 public void ShouldNotRemoveNonExistentItems()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     var oldCount = list.Count;
     list.Remove(8);
     list.Count.Equals(oldCount);
 }
Пример #14
0
 public void ShouldRemoveAnItem()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.Remove(3);
     list.ShouldNotContain(3);
 }
Пример #15
0
 public void ShouldSupportAddition()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.ShouldContain(3);
     list.Add(5);
     list.ShouldContain(5);
 }
Пример #16
0
 public void InsertThrowException()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.Insert(-1, 4);
     list.ShouldContain(4);
 }
Пример #17
0
 public void InsertAtIndex()
 {
     SimpleLinkedList<int> list = new SimpleLinkedList<int>();
     list.Add(3);
     list.Add(5);
     list.Add(7);
     list.Insert(1, 4);
     list.ShouldContain(4);
 }