Exemplo n.º 1
0
        public bool FindTest()
        {
            Console.WriteLine("SinglyLinkedListTests : FindTest");
            var result = true;

            var list = new SinglyLinkedList <string>();

            list.Add("jim");
            list.Add("dave");

            if (list.Find("dave") == null)
            {
                result = false;
            }
            if (list.Find("jim") == null)
            {
                result = false;
                if (list.Find("jim").Node != "jim")
                {
                    result = false;
                }
                if (list.Find("jim").Node == "jIm")
                {
                    result = false;
                }
            }
            if (list.Find("Z") != null)
            {
                result = false;
            }

            return(result);
        }
        public void AddTest()
        {
            Node <string> node;
            var           list = new SinglyLinkedList <string>();

            list.Add("x");
            Assert.IsTrue(list.Count == 1);

            node = list.Contains("x");
            Assert.IsTrue(node.Value == "x");
            Assert.IsTrue(node.Next == null);

            list.Add("y");
            Assert.IsTrue(list.Count == 2);

            node = list.Contains("x");
            Assert.IsTrue(node.Value == "x");
            Assert.IsTrue(node.Next.Value == "y");
            Assert.IsTrue(node.Next.Next == null);


            list.Add("z");

            Assert.IsTrue(list.Count == 3);

            node = list.Contains("x");
            Assert.IsTrue(node.Value == "x");
            Assert.IsTrue(node.Next.Value == "y");
            Assert.IsTrue(node.Next.Next.Value == "z");
            Assert.IsTrue(node.Next.Next.Next == null);
        }
        /// <summary>
        /// method that finds the node that k nodes from the last node
        /// </summary>
        /// <param name="k">an integer amount to count from the last node</param>
        /// <returns>target node</returns>
        public static Node KthElement(int k)
        {
            SinglyLinkedList datLL = new SinglyLinkedList(new Node(42));
            datLL.Add(new Node(24));
            datLL.Add(new Node(12));

            // we should now print 12 --> 24 --> 42
            Console.WriteLine("\nHere is our starting linked list:");
            datLL.Print();

            try
            {
                datLL.Current = datLL.Head;
                Node runner = datLL.Head;
                int counter = 0;
                while (runner.Next != null)
                {
                    counter++;
                    runner = runner.Next;
                    if (counter > k)
                        datLL.Current = datLL.Current.Next;
                }
                if (k > counter)
                {
                    Console.WriteLine("Error: k out of range");
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return datLL.Current;
        }
Exemplo n.º 4
0
        public CommonMessage SeeTheSLList()
        {
            var message = new CommonMessage()
            {
                OK      = true,
                Message = "",
            };

            var list = new SinglyLinkedList <string>();

            list.Add("C'est");
            list.Add("la");
            list.Add("vie");

            //foreach (var node in list)
            //{
            //    message.Message += $"{node.Data} -> ";
            //}

            // message.Message += "(end)";


            list.Remove(list.Head);
            list.Remove(list.Tail);
            list.Remove(list.Head);


            message.Message = string.Join(" -> ", list);

            return(message);
        }
Exemplo n.º 5
0
 public void BeginTestMethod()
 {
     target = new SinglyLinkedList<int>();
     Assert.AreEqual(target.Leng, 0);
     target.Add(2);
     target.Add(3, 1);
     target.AddLast(5);
 }
        public void AddTest_AddTwoElements_PrintsAllElements()
        {
            SinglyLinkedList <string> strList = new SinglyLinkedList <string>();

            strList.Add("One");
            strList.Add("Two");
            Assert.AreEqual(" One Two", strList.ToString());
        }
Exemplo n.º 7
0
        public void TestProcessShortList()
        {
            SinglyLinkedList <int> shortList = new SinglyLinkedList <int>();

            shortList.Add(1);
            shortList.Add(2);
            int value = ProcessList <int> .GetElementFromEndAt(shortList);
        }
        public void TestSinglyLinkedListAdd()
        {
            SinglyLinkedList<string> list = new SinglyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");

            Assert.AreEqual(3, list.Count);
        }
Exemplo n.º 9
0
        private SinglyLinkedList <int> InitSinglyLinkedList()
        {
            SinglyLinkedList <int> SinglyLinkedList = new SinglyLinkedList <int>();

            SinglyLinkedList.Add(1);
            SinglyLinkedList.Add(2);
            SinglyLinkedList.Add(3);

            return(SinglyLinkedList);
        }
Exemplo n.º 10
0
        public void FindLinkedList()
        {
            var actual = new SinglyLinkedList();

            actual.Add(11);
            actual.Add(13);
            actual.Add(15);

            Assert.AreEqual(13, actual.Find(13).Data);
        }
Exemplo n.º 11
0
        public void AddAddsTwoElements()
        {
            var list = new SinglyLinkedList <int>();

            list.Add(10);
            list.Add(20);
            list.Length.Should().Be(2);
            list.GetAtIndex(0).Should().Be(10);
            list.GetAtIndex(1).Should().Be(20);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            SinglyLinkedList sll = new SinglyLinkedList();

            sll.Add(5);
            sll.Add(7);
            sll.Add(0);
            sll.PrintValues();
            Console.WriteLine(sll.Find(0));
        }
Exemplo n.º 13
0
        public void AddTest()
        {
            string listContent             = "3, 7, 2";
            SinglyLinkedList <int> intList = new SinglyLinkedList <int>();

            intList.Add(3);
            intList.Add(7);
            intList.Add(2);
            Assert.AreEqual(listContent, intList.ToString());
        }
        public void TestSinglyLinkedListAdd()
        {
            SinglyLinkedList <string> list = new SinglyLinkedList <string>();

            list.Add("One");
            list.Add("Two");
            list.Add("Three");

            Assert.AreEqual(3, list.Count);
        }
Exemplo n.º 15
0
        public void AddTest()
        {
            var list = new SinglyLinkedList <int>(0);

            list = list.Add(1);
            list = list.Add(2);
            list = list.Add(3);
            Assert.AreEqual(4, list.Count);
            Assert.IsTrue(list.SequenceEqual(new [] { 3, 2, 1, 0 }));
        }
Exemplo n.º 16
0
        public void NotFondLinkedList()
        {
            var actual = new SinglyLinkedList();

            actual.Add(11);
            actual.Add(13);
            actual.Add(15);

            Assert.IsNull(actual.Find(17));
        }
Exemplo n.º 17
0
        public void CreateLinkedList()
        {
            var actual = new SinglyLinkedList();

            actual.Add(11);
            actual.Add(13);
            actual.Add(15);

            Assert.AreEqual(3, actual.Count());
        }
        public void RemoveTest_HigherIndexRemoved_ThrowsException()
        {
            SinglyLinkedList <string> strList = new SinglyLinkedList <string>();

            strList.Add("One");
            strList.Add("Two");
            strList.Add("Three");
            strList.Add("Four");

            strList.Remove(10);
        }
        public void RemoveFromCount()
        {
            SinglyLinkedList singleList = new SinglyLinkedList();

            singleList.Add("David");
            singleList.Add("James");
            singleList.Add("Pirate Pete");
            singleList.Add("Brenda");
            singleList.Delete("James");
            Assert.AreEqual(singleList.Count(), 3);
        }
Exemplo n.º 20
0
        public void RemoveFails()
        {
            SinglyLinkedList <int> l = new SinglyLinkedList <int>();

            l.Add(1);
            l.Add(2);
            l.Add(3);
            l.Add(4);
            l.Add(5);
            Assert.IsFalse(l.Remove(7), "does not return failure");
        }
Exemplo n.º 21
0
        public void CanAdd()
        {
            SinglyLinkedList <int> l = new SinglyLinkedList <int>();

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

            Assert.AreEqual <int>(1, l.Head.Data, "Head does not match expected value");
            Assert.AreEqual <int>(3, l.Tail.Data, "Tail does not match expected value");
        }
Exemplo n.º 22
0
 public void MyListAddTest()
 {
     list = new SinglyLinkedList <int>();
     Assert.AreEqual(0, list.Count);
     list.Add(100);
     Assert.AreEqual(1, list.Count);
     for (int i = 0; i < 10000; ++i)
     {
         list.Add(i);
     }
     Assert.AreEqual(10001, list.Count);
 }
        public void ListToArray()
        {
            string[]         singleNodeArray = { "David", "James", "Pirate Pete", "Brenda" };
            SinglyLinkedList singleList      = new SinglyLinkedList();

            singleList.Add("David");
            singleList.Add("James");
            singleList.Add("Pirate Pete");
            singleList.Add("Brenda");
            string[] result = singleList.Values();
            CollectionAssert.AreEqual(singleNodeArray, result);
        }
        public void LastIndexOf_ExistingItem_ShouldReturnCorrectIndex()
        {
            var list = new SinglyLinkedList<int>();

            list.Add(1);
            list.Add(101);
            list.Add(1);
            list.Add(101);

            var index = list.LastIndexOf(1);
            Assert.AreEqual(2, index);
        }
        public void FirstIndexOf_NonExistingItem_ShouldReturnMinusOne()
        {
            var list = new SinglyLinkedList<int>();

            list.Add(1);
            list.Add(101);
            list.Add(1);
            list.Add(101);

            var index = list.FirstIndexOf(1001);
            Assert.AreEqual(-1, index);
        }
        public void TestSinglyLinkedListIndexer()
        {
            SinglyLinkedList<string> list = new SinglyLinkedList<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 TestSinglyLinkedListIndexOf()
        {
            SinglyLinkedList <string> list = new SinglyLinkedList <string>();

            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";
            list.Insert(2, "Four");

            Assert.AreEqual(3, list.IndexOf("Three"));
        }
        public void RemoveTest_RemoveFirstElement_FirstElementRemoved()
        {
            SinglyLinkedList <string> strList = new SinglyLinkedList <string>();

            strList.Add("One");
            strList.Add("Two");
            strList.Add("Three");
            strList.Add("Four");

            strList.Remove(0);
            Assert.AreEqual(" Two Three Four", strList.ToString());
        }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            var myList = new SinglyLinkedList();

            myList.Add(new Node(1));
            myList.Add(new Node(2));
            myList.Add(new Node(3));
            myList.Add(new Node(4));
            myList.Add(new Node(5));

            myList.Display();
        }
Exemplo n.º 30
0
        public void GetAt()
        {
            SinglyLinkedList <int> l = new SinglyLinkedList <int>();

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

            Assert.AreEqual <int>(3, l.getAt(2), "need to check my getAt code");
        }
Exemplo n.º 31
0
        public void DoesNotContain()
        {
            SinglyLinkedList <int> l = new SinglyLinkedList <int>();

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

            Assert.IsFalse(l.Contains(7), "list thinks a non-existant value is in the list");
        }
Exemplo n.º 32
0
        public void DoesContain()
        {
            SinglyLinkedList <int> l = new SinglyLinkedList <int>();

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

            Assert.IsTrue(l.Contains(4), "doesn't identify value is in list");
        }
        public void RemoveTailAssertTailIs3()
        {
            var linkedList = new SinglyLinkedList <int>();

            linkedList.Add(1);
            linkedList.Add(2);
            linkedList.Add(3);
            linkedList.Add(4);

            linkedList.Remove(4);
            Assert.AreEqual(3, linkedList.Tail.Value);
            Assert.AreEqual(3, linkedList.Count);
        }
        public void TestSinglyLinkedListIndexer()
        {
            SinglyLinkedList <string> list = new SinglyLinkedList <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));
        }
Exemplo n.º 35
0
        public static void Next_MiddleNode_ShouldReturnNextNode()
        {
            var list = new SinglyLinkedList() as ILinkedList <ISinglyLinkedListNode>;

            const string nodeValue = "Michael Moorcock";

            list.Add(nodeValue);

            var currNode = list.Add(nodeValue);
            var nextNode = list.Add(nodeValue);

            Assert.AreSame(currNode.Next, nextNode);
        }
        public void TestSinglyLinkedListInsert_InsertInTheMiddle()
        {
            SinglyLinkedList<string> list = new SinglyLinkedList<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));
        }
Exemplo n.º 37
0
    static void Main()
    {
        var myList = new SinglyLinkedList<int>();
        myList.Add(1);
        myList.Add(2);
        myList.Add(3);
        myList.Add(2);
        myList.Add(1);

        Console.WriteLine(string.Join(" ", myList));
        Console.WriteLine(myList.FirstIndexOf(2));
        myList.Remove(4);
        Console.WriteLine(string.Join(" ", myList));
        Console.WriteLine(myList.Count);
    }
Exemplo n.º 38
0
 public void TestSinglyLinkedList()
 {
     SinglyLinkedList<int> singlyLinkedList = new SinglyLinkedList<int>();
     singlyLinkedList.AddAtEnd(5);
     singlyLinkedList.AddAtEnd(6);
     singlyLinkedList.AddAtEnd(7);
     singlyLinkedList.AddAtEnd(8);
     singlyLinkedList.AddAtStart(4);
     singlyLinkedList.AddAtStart(3);
     singlyLinkedList.AddAtStart(2);
     singlyLinkedList.AddAtStart(1);
     singlyLinkedList.Add(9, 10);
     singlyLinkedList.Add(11, 9, true);
     singlyLinkedList.Add(10, 11);
 }
Exemplo n.º 39
0
        public static void Main()
        {
            var list = new SinglyLinkedList<int>();

            for (int i = 0; i < 10; i++)
            {
                list.Add(i);
                Console.Write("Added " + i);
                Console.WriteLine(" Count -> " + list.Count);
            }

            Console.WriteLine("Items:");
            foreach (var item in list)
            {
                Console.Write(item + " ");
            }

            Console.WriteLine();

            int index = 0;
            for (int i = 0; i < 10; i++)
            {
                list.Remove(i - index++);
                Console.Write("Remmoved " + i);
                Console.WriteLine(" Count -> " + list.Count);
            }

            Console.WriteLine("Items:");
            foreach (var item in list)
            {
                Console.Write(item + " ");
            }
        }
Exemplo n.º 40
0
 public void AddTest()
 {
     var list = new SinglyLinkedList<int> { 1, 0, -1 };
     Assert.AreEqual(3, list.Count, "Did not add the items");
     list.Add(1);
     Assert.AreEqual(4, list.Count, "Did not add the item");
 }
        public void TestSinglyLinkedListForEach()
        {
            SinglyLinkedList<string> list = new SinglyLinkedList<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));
        }
        public void Add_EmptyList_ShouldIncrementCount()
        {
            const int N = 100;
            var list = new SinglyLinkedList<int>();

            for (int i = 0; i < N; i++)
            {
                list.Add(i);
            }

            Assert.AreEqual(N, list.Count);
        }
Exemplo n.º 43
0
        public void SortListTest()
        {
            var list = new SinglyLinkedList<int>();
            foreach (int n in a) {
                list.Add(n);
            }

            MergeSort.Sort(list);
            string expected = "[ -59 -45 -12 11 11 44 53 62 80 94 ]";
            string actual = list.ToString();
            Assert.AreEqual(actual, expected);
        }
Exemplo n.º 44
0
        private static void Main()
        {
            var list = new SinglyLinkedList<int>();

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

            list.Add(5);
            list.Add(3);
            list.Add(2);
            list.Add(10, 1);
            list.Add(5);
            list.Add(99);
            Console.WriteLine("Count = {0}", list.Count);

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

            list.Remove(1);
            Console.WriteLine("Count = {0}", list.Count);
            list.ForEach(Console.WriteLine);
            Console.WriteLine("--------------------");

            Console.WriteLine(list.FirstIndexOf(5));
            Console.WriteLine("--------------------");

            Console.WriteLine(list.LastIndexOf(5));
        }
        public void Add_EmptyList_ShouldAdd()
        {
            const int N = 100;
            var list = new SinglyLinkedList<int>();

            for (int i = 0; i < N; i++)
            {
                list.Add(i);
            }

            int currentItem = 0;
            foreach (var item in list)
            {
                Assert.AreEqual(currentItem, item);
                currentItem++;
            }
        }
        public void TestSinglyLinkedListIndexOf()
        {
            SinglyLinkedList<string> list = new SinglyLinkedList<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            list[0] = "Zero";
            list.Insert(2, "Four");

            Assert.AreEqual(3, list.IndexOf("Three"));
        }
        public void Remove_ValidIndex_ShouldRemove()
        {
            const int N = 100;
            var list = new SinglyLinkedList<int>();

            for (int i = 0; i < N; i++)
            {
                list.Add(i);
                list.Remove(0);
            }

            Assert.AreEqual(0, list.Count);
        }
        public void Remove_ValidIndex_ShouldDecrementCount()
        {
            const int N = 100;
            var list = new SinglyLinkedList<int>();

            for (int i = 0; i < N; i++)
            {
                list.Add(i);
            }

            list.Remove(0);
            list.Remove(1);
            list.Remove(2);

            Assert.AreEqual(N - 3, list.Count);
        }
Exemplo n.º 49
0
        public void RemoveMiddleItemTest([PexAssumeUnderTest]int[] newElements, int position)
        {
            PexAssume.IsTrue(newElements.Length > 0);
            PexAssume.IsTrue(position >= 0 && position < newElements.Length);

            //Requires the elements to be unique
            for (int i = 0; i < newElements.Length; i++)
                for (int j = i + 1; j < newElements.Length; j++)
                    PexAssume.IsTrue(newElements[i] != newElements[j]);

            SinglyLinkedList<int> sll = new SinglyLinkedList<int>();

            for (int i = 0; i < newElements.Length; i++)
            {
                sll.Add(newElements[i]);
            }

            bool actual = sll.Remove(newElements[position]);

            if (position > 0 && position < newElements.Length - 1)
            {
                PexAssert.AreEqual(newElements[0], sll.Head.Value);
                PexAssert.AreEqual(newElements[newElements.Length - 1], sll.Tail.Value);
            }
            PexAssert.AreEqual(newElements.Length - 1, sll.Count);
            PexAssert.IsTrue(actual);
        }
Exemplo n.º 50
0
        public void GetEnumeratorTest()
        {
            SinglyLinkedList<int> sll = new SinglyLinkedList<int>();
            IEnumerable enumerSll = sll;

            sll.Add(10);

            Assert.IsNotNull(enumerSll.GetEnumerator());
        }