public void NextTest()
        {
            SinglyLinkedListNode<int> n2 = new SinglyLinkedListNode<int>(20);
            SinglyLinkedListNode<int> n1 = new SinglyLinkedListNode<int>(10) {Next = n2};

            Assert.AreEqual(20, n1.Next.Value);
        }
 public static SinglyLinkedList<string> Create(
     SinglyLinkedListNode<string> node_singlyLinkedListNode,
     string[] items
     )
 {
     PexAssume.IsNotNull(items);
     SinglyLinkedList<string> singlyLinkedList = new SinglyLinkedList<string>(items);
     return singlyLinkedList;
 }
 public static SinglyLinkedList<int> Create(
     SinglyLinkedListNode<int> node_singlyLinkedListNode,
     int[] elements
     )
 {
     PexAssume.IsNotNull(elements);
     SinglyLinkedList<int> singlyLinkedList = new SinglyLinkedList<int>(elements);
     return singlyLinkedList;
 }
 public void NextTest02()
 {
     SinglyLinkedListNode<int> s0 = new SinglyLinkedListNode<int>(0);
     s0.Next = (SinglyLinkedListNode<int>)null;
     this.NextTest(s0, 0);
     PexAssert.IsNotNull((object)s0);
     PexAssert.AreEqual<int>(0, s0.Value);
     PexAssert.IsNull(s0.Next);
 }
Exemple #5
0
    public void insertBack(Object arg)
    {
        SinglyLinkedListNode freshNode = new SinglyLinkedListNode();
        freshNode.value = arg;
        freshNode.next = null;

        if (this.header == null) {
            {/*$goal 0 reachable*/}
            this.header = freshNode;
        } else {
            {/*$goal 1 reachable*/}
            SinglyLinkedListNode current;
            current = this.header;
            while (current.next != null) {
                {/*$goal 2 reachable*/}
                current = current.next;
            }
            current.next = freshNode;
        }
        {/*$goal 3 reachable*/}
    }
 public void NextTest([PexAssumeUnderTest]SinglyLinkedListNode<int> n2, int value)
 {
     SinglyLinkedListNode<int> n1 = new SinglyLinkedListNode<int>(value) {Next = n2};
     PexAssert.AreEqual(n2.Value, n1.Next.Value);
 }
Exemple #7
0
    public void remove(int index)
    {
        if (index<0) {
            {/*$goal 0 reachable*/}
            throw new RuntimeException();
        }

        SinglyLinkedListNode current;
        current = this.header;
        SinglyLinkedListNode previous;
        previous = null;
        int current_index;
        current_index = 0;

        boolean found = false;

        while (found==false && current != null) {
            {/*$goal 1 reachable*/}

            if (index == current_index) {
                {/*$goal 2 reachable*/}
                found = true;
            } else {
                {/*$goal 3 reachable*/}
                current_index = current_index + 1;
                previous = current;
                current = current.next;
            }
        }

        if (found==false) {
            {/*$goal 4 reachable*/}
            throw new RuntimeException();
        }

        if (previous == null){
            {/*$goal 5 reachable*/}
            this.header = current.next;
        } else {
            {/*$goal 6 reachable*/}
            previous.next = current.next;
        }
    }
Exemple #8
0
 public SinglyLinkedListNode(int nodeData)
 {
     this.data = nodeData;
     this.next = null;
 }
        public void ValueIntTest()
        {
            SinglyLinkedListNode<int> n = new SinglyLinkedListNode<int>(10);

            Assert.AreEqual(10, n.Value);
        }
Exemple #10
0
        /// <summary>
        /// In-place sorting of the given <see cref="SinglyLinkedList{T}"/> elements. Used for descending sort.
        /// </summary>
        /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam>
        /// <param name="list">The <see cref="SinglyLinkedList{T}"/> containing the elements for sorting.</param>
        /// <param name="nodeBeforeStartNode">The <see cref="SinglyLinkedListNode{T}"/> which is the node before the sorted sequence.</param>
        /// <param name="startNode">The start <see cref="SinglyLinkedListNode{T}"/> of the current split.</param>
        /// <param name="endNode">The end <see cref="SinglyLinkedListNode{T}"/> of the current split.</param>
        /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements.</param>
        private static void SplitMergeDescending <T>(SinglyLinkedList <T> list, SinglyLinkedListNode <T> nodeBeforeStartNode, SinglyLinkedListNode <T> startNode, SinglyLinkedListNode <T> endNode, IComparer <T> comparer)
        {
            // End of recursion. If we have 1 item or less we consider it sorted
            if (list.Count < 2)
            {
                return;
            }

            // spliting the list into two lists
            var left  = new SinglyLinkedList <T>();
            var right = new SinglyLinkedList <T>();

            var  curNode                           = startNode;
            var  nodeAfterEndNode                  = endNode.Next;
            bool nodeBeforeStartNodeIsNull         = nodeBeforeStartNode == null;
            SinglyLinkedListNode <T> lastLeftNode  = null;
            SinglyLinkedListNode <T> lastRightNode = null;
            int i = 0;
            int leftItemsNumber = list.Count / 2;

            while (curNode != nodeAfterEndNode)
            {
                if (i++ < leftItemsNumber)
                {
                    var node = curNode;            // save the current node
                    curNode = curNode.Next;        // go to the next node
                    if (nodeBeforeStartNodeIsNull) // if the start node is first in the list
                    {
                        list.Remove(node);         // remove the current node(which is the first node)
                    }
                    else// if the start node is not the first in the list
                    {
                        list.RemoveAfter(nodeBeforeStartNode); // remove the node after the node before the start node(i.e. the current node)
                    }
                    if (lastLeftNode == null)                  // if first time(no last node)
                    {
                        left.AddFirst(node);                   // add at the beginning
                    }
                    else// if we have last node
                    {
                        left.AddAfter(lastLeftNode, node); // add after it(faster than AddLast method)
                    }
                    lastLeftNode = node;                   // update last node
                }
                else
                {
                    var node = curNode;            // save the current node
                    curNode = curNode.Next;        // go to the next node
                    if (nodeBeforeStartNodeIsNull) // if the start node is first in the list
                    {
                        list.Remove(node);         // remove the current node(which is the first node)
                    }
                    else// if the start node is not the first in the list
                    {
                        list.RemoveAfter(nodeBeforeStartNode); // remove the node after the node before the start node(i.e. the current node)
                    }
                    if (lastRightNode == null)                 // if first time(no last node)
                    {
                        right.AddFirst(node);                  // add at the beginning
                    }
                    else// if we have last node
                    {
                        right.AddAfter(lastRightNode, node); // add after it(faster than AddLast method)
                    }
                    lastRightNode = node;                    // update last node
                }
            }

            // Recursively sort both sublists
            // NOTE: node before startNode is null because in the left and right splits
            // we work with all nodes from the list.
            SplitMergeDescending(left, null, left.First, left.Last, comparer);
            SplitMergeDescending(right, null, right.First, right.Last, comparer);

            // Merge the two splits into the given list
            MergeDescending(list, left, right, nodeBeforeStartNode, comparer);

            // NOTE: only the last merging of two lists is done on the given list for sorting
        }
Exemple #11
0
        /// <summary>
        /// Sorts a range of elements in the <see cref="SinglyLinkedList{T}"/> in descending order using the specified comparer beginning from the start node and ending at the end node.
        /// </summary>
        /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam>
        /// <param name="list">The <see cref="SinglyLinkedList{T}"/> containing the elements for sorting.</param>
        /// <param name="startNode">The start <see cref="SinglyLinkedListNode{T}"/> of the range for sorting.</param>
        /// <param name="endNode">The end <see cref="SinglyLinkedListNode{T}"/> of the range for sorting.</param>
        /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements,
        /// or null to use the default comparer <see cref="Comparer{T}.Default"/>.</param>
        /// <returns>Returns the given <see cref="SinglyLinkedList{T}"/> when sorted.</returns>
        public static SinglyLinkedList <T> MergeSortDescending <T>(this SinglyLinkedList <T> list, SinglyLinkedListNode <T> startNode, SinglyLinkedListNode <T> endNode, IComparer <T> comparer)
        {
            if (startNode == null)
            {
                throw new ArgumentNullException("startNode");
            }
            if (endNode == null)
            {
                throw new ArgumentNullException("endNode");
            }
            if (startNode.List != list)
            {
                throw new ArgumentException("startNode doesn't belong to the list!");
            }
            if (endNode.List != list)
            {
                throw new ArgumentException("startNode doesnt't belong to the list!");
            }

            if (comparer == null)
            {
                comparer = Comparer <T> .Default;
            }

            if (list.Last == endNode)                                           // the start node is the first in the list
            {
                SplitMergeDescending(list, null, startNode, endNode, comparer); // the node before it is null
            }
            else
            {
                var curNode = list.First;
                while (curNode.Next != startNode)
                {
                    curNode = curNode.Next;
                }
                SplitMergeDescending(list, curNode, startNode, endNode, comparer);
            }

            return(list);
        }
Exemple #12
0
        /// <summary>
        /// Merging two lists into a given <see cref="SinglyLinkedList{T}"/>. Used for ascending sort.
        /// </summary>
        /// <typeparam name="T">The data type of the <see cref="SinglyLinkedList{T}"/>.</typeparam>
        /// <param name="mergeList">The <see cref="SinglyLinkedList{T}"/> for merging the left and right lists into.</param>
        /// <param name="left">The left <see cref="SinglyLinkedList{T}"/> containing some of the elements for sorting.</param>
        /// <param name="right">The right <see cref="SinglyLinkedList{T}"/> containing some of the elements for sorting.</param>
        /// <param name="nodeBeforeStartNode">The <see cref="SinglyLinkedListNode{T}"/> which is the node after the sorted sequence.</param>
        /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements.</param>
        /// <returns>Returns the given <see cref="SinglyLinkedList{T}"/> for the merged elements from the left <see cref="SinglyLinkedList{T}"/> and right <see cref="SinglyLinkedList{T}"/>.</returns>
        private static void Merge <T>(SinglyLinkedList <T> mergeList, SinglyLinkedList <T> left, SinglyLinkedList <T> right, SinglyLinkedListNode <T> nodeBeforeStartNode, IComparer <T> comparer)
        {
            bool nodeBeforeStartNodeIsNull         = nodeBeforeStartNode == null;
            SinglyLinkedListNode <T> lastAddedNode = nodeBeforeStartNode;

            // merging until one of the lists becomes empty
            while (left.Count > 0 && right.Count > 0)
            {
                if (comparer.Compare(left.First.Value, right.First.Value) <= 0)
                {
                    var node = left.First;
                    left.RemoveFirst();
                    if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list
                    {
                        // Add after the last added node(if null add first)
                        if (lastAddedNode == null)
                        {
                            mergeList.AddFirst(node);
                        }
                        else
                        {
                            mergeList.AddAfter(lastAddedNode, node);
                        }
                    }
                    else// if the start node is not the first
                    {
                        mergeList.AddAfter(lastAddedNode, node);// add after the last added node
                    }
                    lastAddedNode = node;// Update last added node
                }
                else
                {
                    var node = right.First;
                    right.RemoveFirst();
                    if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list
                    {
                        // Add after the last added node(if null add first)
                        if (lastAddedNode == null)
                        {
                            mergeList.AddFirst(node);
                        }
                        else
                        {
                            mergeList.AddAfter(lastAddedNode, node);
                        }
                    }
                    else// if the start node is not the first
                    {
                        mergeList.AddAfter(lastAddedNode, node);// add after the last added node
                    }
                    lastAddedNode = node;// Update last added node
                }
            }

            // add the remaining elements from the left or the right list
            while (left.Count > 0)
            {
                var node = left.First;
                left.RemoveFirst();
                if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list
                {
                    // Add after the last added node(if null add first)
                    if (lastAddedNode == null)
                    {
                        mergeList.AddFirst(node);
                    }
                    else
                    {
                        mergeList.AddAfter(lastAddedNode, node);
                    }
                }
                else// if the start node is not the first
                {
                    mergeList.AddAfter(lastAddedNode, node);// add after the last added node
                }
                lastAddedNode = node;// Update last added node
            }

            while (right.Count > 0)
            {
                var node = right.First;
                right.RemoveFirst();
                if (nodeBeforeStartNodeIsNull)// if the start node is the first in the list
                {
                    // Add after the last added node(if null add first)
                    if (lastAddedNode == null)
                    {
                        mergeList.AddFirst(node);
                    }
                    else
                    {
                        mergeList.AddAfter(lastAddedNode, node);
                    }
                }
                else// if the start node is not the first
                {
                    mergeList.AddAfter(lastAddedNode, node);// add after the last added node
                }
                lastAddedNode = node;// Update last added node
            }
        }
Exemple #13
0
 public SinglyLinkedList()
 {
     this.head = null;
     this.tail = null;
 }
 public void ValueIntTest(int newElem)
 {
     SinglyLinkedListNode<int> n = new SinglyLinkedListNode<int>(newElem);
     PexAssert.AreEqual(newElem, n.Value);
 }
        public void ValueStringTest()
        {
            SinglyLinkedListNode<string> n = new SinglyLinkedListNode<string>("Granville");

            Assert.AreEqual("Granville", n.Value);
        }
 public void ValueStringTest(string newElem)
 {
     SinglyLinkedListNode<string> n = new SinglyLinkedListNode<string>(newElem);
     PexAssert.AreEqual(newElem, n.Value);
 }
        // Complete the insertNodeAtTail function below.

        /*
         * For your reference:
         *
         * SinglyLinkedListNode {
         *     int data;
         *     SinglyLinkedListNode next;
         * }
         *
         */
        static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data)
        {
        }