示例#1
0
文件: test.cs 项目: mygaraj/Interview
 public void EmptyElementTest()
 {
     LinkedList emptyList = new LinkedList();
     LinkedList actualRevertedList = Factory.GetLinkedListReverter().Reverse(emptyList);
     Assert.AreSame(emptyList, actualRevertedList);
     Assert.IsNull(actualRevertedList.Data);
     Assert.IsNull(actualRevertedList.Next);
 }
示例#2
0
文件: test.cs 项目: mygaraj/Interview
 public void TwoElementTest()
 {
     LinkedList fistElement = new LinkedList();
     fistElement.Data = 1;
     LinkedList secondElement = new LinkedList() { Data = 2 };
     fistElement.Next = secondElement; // { 1 } -> { 2 }
     LinkedList actualRevertedList = Factory.GetLinkedListReverter().Reverse(fistElement);
     Assert.IsNotNull(actualRevertedList);
     Assert.AreEqual(2, actualRevertedList.Data); // { 2 // check } -> { 1 }
     Assert.IsNotNull(actualRevertedList.Next); // { 2 } -> /*check*/ { 1 }
     Assert.AreEqual(1, actualRevertedList.Next.Data); // { 2 } -> { 1 // check }
 }
示例#3
0
文件: test.cs 项目: mygaraj/Interview
        public void ThousandElenetsTest()
        {
            int elementsCount = 1000;
            LinkedList root = new LinkedList();
            LinkedList current = root;
            for (int i = 0; i < elementsCount; i++)
            {
                current.Data = i;
                current.Next = i == elementsCount - 1 ? null : new LinkedList();
                current = current.Next;
            }

            LinkedList actualRevertedList = Factory.GetLinkedListReverter().Reverse(root);

            for (int i = elementsCount - 1; i > 0; i--)
            {
                Assert.AreEqual( i, actualRevertedList.Data);
                actualRevertedList = actualRevertedList.Next;
            }

            Assert.AreEqual(0, actualRevertedList.Data);
            Assert.IsNull(actualRevertedList.Next);
        }
示例#4
0
        /// <summary>
        /// Invert the order of the elements in a<see cref="LinkedList"/>
        /// </summary>
        /// <param name="source">sequence of elements to revert</param>
        /// <returns>a <see cref="LinkedList"/> with elements in reverse order.</returns>
        /// <remarks> O(N): 4 * N assigments, N comparasions. </remarks>
        public LinkedList Reverse(LinkedList source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Next == null)
                return source;

            LinkedList current = source;
            LinkedList previous = null;

            do
            {
                LinkedList next = current.Next;
                current.Next = previous;
                previous = current;
                current = next;
            } while (current != null);

            return previous;
        }
示例#5
0
文件: test.cs 项目: mygaraj/Interview
 public void OneElementTest()
 {
     LinkedList oneElementList = new LinkedList() { Data = 1 };
     LinkedList actualRevertedList = Factory.GetLinkedListReverter().Reverse(oneElementList);
 }