예제 #1
0
    static void Main()
    {
        LinkedListClass linkedListClass = GetLinkedList();

        Console.WriteLine(linkedListClass.Count);
        PrintLinkedList(linkedListClass);
    }
예제 #2
0
        public void AddSimpleTest()
        {
            var linkedList = new LinkedListClass <string>();

            linkedList.IsEmpty.Should().BeTrue();
            linkedList.Count.Should().Be(0);
            linkedList.AddFirst("B");
            linkedList.AddFirst("A");
            linkedList.AddLast("C");
            linkedList.AddLast("D");
            linkedList.AddLast("E");
            linkedList.IsEmpty.Should().BeFalse();
            linkedList.Count.Should().Be(5);
            linkedList.First.Data.Should().Be("A");
            linkedList.First.Next.Data.Should().Be("B");
            linkedList.First.Next.Next.Data.Should().Be("C");
            linkedList.First.Next.Next.Next.Data.Should().Be("D");
            linkedList.First.Next.Previous.Data.Should().Be("A");
            linkedList.First.Next.Next.Previous.Data.Should().Be("B");
            linkedList.First.Next.Next.Next.Previous.Data.Should().Be("C");
            linkedList.Last.Data.Should().Be("E");
            linkedList.Last.Previous.Data.Should().Be("D");
            linkedList.Clear();
            linkedList.IsEmpty.Should().BeTrue();
            linkedList.Count.Should().Be(0);
        }
예제 #3
0
    private static LinkedListClass GetLinkedList()
    {
        LinkedListClass linkedListClass = new LinkedListClass();
        int             n = int.Parse(Console.ReadLine());

        for (int i = 0; i < n; i++)
        {
            string[] input = Console.ReadLine()
                             .Split(' ');
            string command = input[0];
            int    number  = int.Parse(input[1]);


            switch (command)
            {
            case "Add":
                linkedListClass.Add(number);
                break;

            case "Remove":
                linkedListClass.Remove(number);
                break;
            }
        }
        return(linkedListClass);
    }
예제 #4
0
    private static void PrintLinkedList(LinkedListClass linkedListClass)
    {
        StringBuilder sb = new StringBuilder();

        foreach (var number in linkedListClass)
        {
            sb.Append($"{number.ToString()} ");
        }
        Console.WriteLine(sb.ToString());
    }
예제 #5
0
        public void ReverseSimpleTest()
        {
            var linkedList = new LinkedListClass <string>();

            linkedList.AddLast("A");
            linkedList.AddLast("B");
            linkedList.AddLast("C");
            linkedList.Reverse();
            linkedList.First.Data.Should().Be("C");
            linkedList.Last.Data.Should().Be("A");
        }
예제 #6
0
        public void SearchAndFindNode()
        {
            LinkedListClass list = new LinkedListClass();

            list.Add1(56);
            list.Add1(30);
            list.Add1(70);
            Node node = list.Search(30);

            Assert.AreEqual(30, node.data);
        }
예제 #7
0
        public void InsertAtPosition()
        {
            LinkedListClass list = new LinkedListClass();

            list.Add1(56);
            list.Add1(30);
            list.Add1(70);
            list.InsertAtParticularPosition(3, 40);
            int position = list.findPosition(40);

            Assert.AreEqual(3, 3);
        }
예제 #8
0
        public void DeleteAndGetSize()
        {
            LinkedListClass list = new LinkedListClass();

            list.Add1(56);
            list.Add1(30);
            list.Add1(40);
            list.Add1(70);
            list.DeleteNode(40);
            int size = list.size();

            Assert.AreEqual(3, 3);
        }
예제 #9
0
 public void WriteAndRead_LinkedList()
 {
     try
     {
         LinkedListClass s = new LinkedListClass();
         s.s.AddFirst("");
         IO.WriteToXmlFile(IO.CurrentDirectoryFolder + "\\tests\\WriteToXmlFileTesting.xml", s);
         Assert.Fail();
     }
     catch
     {
         Assert.AreEqual(true, true);
     }
 }
예제 #10
0
        public void FindSimpleTest()
        {
            var linkedList = new LinkedListClass <string>();

            linkedList.AddLast("A");
            linkedList.AddLast("B");
            linkedList.AddLast("C");
            var firstItem  = linkedList.Find("A");
            var secondItem = linkedList.Find("B");
            var thirdItem  = linkedList.Find("C");

            firstItem.Data.Should().Be("A");
            secondItem.Data.Should().Be("B");
            thirdItem.Data.Should().Be("C");
            var fourthItem = linkedList.Find("X");

            fourthItem.Should().Be(null);
        }
예제 #11
0
        public void RemoveSimpleTest()
        {
            var linkedList = new LinkedListClass <string>();

            linkedList.AddLast("A");
            linkedList.AddLast("B");
            linkedList.AddLast("C");
            linkedList.AddLast("D");
            linkedList.AddLast("E");
            linkedList.RemoveFirst();
            linkedList.RemoveLast();
            linkedList.First.Data.Should().Be("B");
            linkedList.Last.Data.Should().Be("D");
            linkedList.RemoveLast();
            linkedList.RemoveLast();
            linkedList.Count.Should().Be(1);
            linkedList.First.Data.Should().Be("B");
        }
예제 #12
0
        public void Test1()
        {
            var linkedList = new LinkedListClass <string>();

            linkedList.AddLast("A");
            linkedList.AddLast("B");
            linkedList.AddLast("C");
            linkedList.AddLast("D");
            linkedList.AddLast("E");
            linkedList.Contains("A").Should().BeTrue();
            linkedList.Contains("B").Should().BeTrue();
            linkedList.Contains("C").Should().BeTrue();
            linkedList.Contains("D").Should().BeTrue();
            linkedList.Contains("E").Should().BeTrue();
            linkedList.Contains("F").Should().BeFalse();
            linkedList.Contains("G").Should().BeFalse();
            linkedList.Contains("H").Should().BeFalse();
        }
        public void LinkedList()
        {
            LinkedList <string> strings = new LinkedList <string>();

            strings.AddLast("test1");
            strings.AddLast("test2");
            LinkedListClass a = new LinkedListClass
            {
                Strings = strings,
            };

            XmlDocument target = new XmlDocument();

            Serializer.Serialize((SystemXmlAdapter)target, a);

            Assert.AreEqual(1, target.DocumentElement.ChildNodes.Count);

            LinkedListClass b = Deserializer.Deserialize <LinkedListClass>((SystemXmlAdapter)target);

            IEnumerator <string> aEnum = a.Strings.GetEnumerator();
            IEnumerator <string> bEnum = b.Strings.GetEnumerator();

            while (true)
            {
                bool aDone = !aEnum.MoveNext();
                bool bDone = !bEnum.MoveNext();
                if (aDone && bDone)
                {
                    break;
                }
                Assert.AreEqual(aDone, bDone);
                Assert.AreEqual(aEnum.Current, bEnum.Current);
            }

            aEnum.Dispose();
            bEnum.Dispose();
        }
예제 #14
0
        public void LinkedList()
        {
            LinkedList <string> strings = new LinkedList <string>();

            strings.AddLast("test1");
            strings.AddLast("test2");
            LinkedListClass a = new LinkedListClass
            {
                Strings = strings,
            };

            JObject target = new JObject();

            Serializer.Serialize((NewtonsoftJsonAdapter)target, a);

            AssertChildren(3, target);

            LinkedListClass b = Deserializer.Deserialize <LinkedListClass>((NewtonsoftJsonAdapter)target);

            IEnumerator <string> aEnum = a.Strings.GetEnumerator();
            IEnumerator <string> bEnum = b.Strings.GetEnumerator();

            while (true)
            {
                bool aDone = !aEnum.MoveNext();
                bool bDone = !bEnum.MoveNext();
                if (aDone && bDone)
                {
                    break;
                }
                Assert.AreEqual(aDone, bDone);
                Assert.AreEqual(aEnum.Current, bEnum.Current);
            }

            aEnum.Dispose();
            bEnum.Dispose();
        }