示例#1
0
        /// <summary>
        /// Returns the node containing the data specified
        /// </summary>
        /// <param name="data">The data to look for in the linked list</param>
        /// <returns>The node that contains the data</returns>
        private Node <T> Find(T data)
        {
            Node <T> current = head;
            T        currentData;
            bool     firstIteration = true;

            // While data is not equal to current data loop
            do
            {
                // Determine if first iteration, if it is make first iteration false and after update current
                if (firstIteration)
                {
                    firstIteration = false;
                }
                else
                {
                    current = current.GetNext();
                }

                // Determine if current is null
                if (current == null)
                {
                    return(null);
                }
                else
                {
                    currentData = current.GetData();
                }
            } while (currentData.CompareTo(data) < 0 || currentData.CompareTo(data) > 0);

            return(current);
        }
        public void TestDataArgContructor()
        {
            Employee        emp1  = new Employee(1);
            Node <Employee> node1 = new Node <Employee>(emp1);

            Assert.That(node1, Is.Not.Null);
            Assert.That(node1.GetData(), Is.EqualTo(emp1));
            Assert.That(node1.GetNext(), Is.Null);
            Assert.That(node1.GetPrevious(), Is.Null);
        }
        public void TestGetNext()
        {
            Employee        emp1     = new Employee(1);
            Employee        emp2     = new Employee(2);
            Employee        emp3     = new Employee(3);
            Node <Employee> node1    = new Node <Employee>(emp1);
            Node <Employee> node2    = new Node <Employee>(emp2);
            Node <Employee> testNode = new Node <Employee>(emp3, node1, node2);

            Assert.That(testNode.GetNext(), Is.EqualTo(node2));
        }
示例#4
0
        /// <summary>
        /// Removes the specified node from the linked list
        /// </summary>
        /// <param name="node">The node to be removed</param>
        /// <returns>Data from the node being removed</returns>
        private T Unlink(Node <T> node)
        {
            Node <T> previous = node.GetPrevious();
            Node <T> next     = node.GetNext();

            previous.SetNext(next);
            next.SetPrevious(previous);
            size--;

            return(node.GetData());
        }
示例#5
0
        /// <summary>
        /// Returns the node at the position number specified
        /// </summary>
        /// <param name="position">Numeric position of node to look for</param>
        /// <returns>Returns the node</returns>
        private Node <T> Find(int position)
        {
            Node <T> current = head;
            int      i       = 1;

            while (i < position)
            {
                current = current.GetNext();
                i++;
            }

            return(current);
        }
        public void TestAllArgContructor()
        {
            Employee        emp1     = new Employee(1);
            Employee        emp2     = new Employee(2);
            Employee        emp3     = new Employee(3);
            Node <Employee> node1    = new Node <Employee>(emp1);
            Node <Employee> node2    = new Node <Employee>(emp2);
            Node <Employee> testNode = new Node <Employee>(emp3, node1, node2);

            Assert.That(testNode, Is.Not.Null);
            Assert.That(testNode.GetData(), Is.EqualTo(emp3));
            Assert.That(testNode.GetNext(), Is.EqualTo(node2));
            Assert.That(testNode.GetPrevious(), Is.EqualTo(node1));
        }
示例#7
0
        /// <summary>
        /// The new node containing data is to be added to the linked list after the node at the specified number.
        /// </summary>
        /// <param name="data">Data to add</param>
        /// <param name="position">The number of the node to add after</param>
        /// <returns>True when complete</returns>
        public bool AddAfter(T data, int position)
        {
            ValidatePositon(position);

            // Determine if possition is equal to size
            if (position == size)
            {
                LinkTail(data);
            }
            else
            {
                Node <T> previous = Find(position);
                Node <T> next     = previous.GetNext();

                Link(data, previous, next);
            }

            return(true);
        }
示例#8
0
        /// <summary>
        /// Removes the node at the head of the linked list and returns the data it contains
        /// </summary>
        /// <returns>The data from the unlinked head node</returns>
        private T UnlinkHead()
        {
            Node <T> oldHead = head;
            T        data;

            head = head.GetNext();

            size -= 1;

            // Determine if head is now null and if it is make the tail null as well
            if (head == null)
            {
                tail = null;
            }

            head?.SetPrevious(null);

            data = oldHead.GetData();

            return(data);
        }
示例#9
0
        /// <summary>
        /// A node containing the supplied data value is to be added after the node containing oldData
        /// </summary>
        /// <param name="data">The data to be added to the list</param>
        /// <param name="oldData">Value used to to find the node containing the oldData</param>
        /// <returns>True when complete</returns>
        public bool AddAfter(T data, T oldData)
        {
            Node <T> node = Find(oldData);

            // Determine if node is null and throw exception if it is
            if (node == null)
            {
                throw new IndexOutOfRangeException("No such element");
            }
            else
            {
                // Dtermine if the node is the tail
                if (node == tail)
                {
                    LinkTail(data);
                }
                else
                {
                    Link(data, node, node.GetNext());
                }
            }

            return(true);
        }