Beispiel #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 TestGetData()
        {
            Employee        emp1  = new Employee(1);
            Node <Employee> node1 = new Node <Employee>(emp1);

            Assert.That(node1.GetData(), Is.EqualTo(emp1));
        }
Beispiel #3
0
        /// <summary>
        /// Change the supplied node's data to the data value passed
        /// </summary>
        /// <param name="newData">The new data to set</param>
        /// <param name="node">The node to have its data replaced</param>
        /// <returns>The old data from the node</returns>
        private T SetData(T newData, Node <T> node)
        {
            T oldData = node.GetData();

            node.SetData(newData);

            return(oldData);
        }
        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);
        }
Beispiel #5
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());
        }
Beispiel #6
0
        /// <summary>
        /// Remove the node at the tail of the linked list and return the data it contains
        /// </summary>
        /// <returns>The data stored in the node being removed</returns>
        private T UnlinkTail()
        {
            Node <T> oldTail = tail;

            tail = tail.GetPrevious();

            tail.SetNext(null);

            size--;

            return(oldTail.GetData());
        }
Beispiel #7
0
 /// <summary>
 /// Returns the data from the node
 /// </summary>
 /// <returns>The data from the node</returns>
 public T Get()
 {
     // Determine if linked list size is 0
     if (size == 0)
     {
         throw new IndexOutOfRangeException("No such element");
     }
     else
     {
         return(head.GetData());
     }
 }
Beispiel #8
0
 /// <summary>
 /// Returns the last data item in the linked list.
 /// </summary>
 /// <returns>The data from the last node</returns>
 public T GetLast()
 {
     // Determine if size of linked list is 0 and throw an exception if it is
     if (size == 0)
     {
         throw new IndexOutOfRangeException("No such element");
     }
     else
     {
         return(tail.GetData());
     }
 }
        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));
        }
Beispiel #10
0
        /// <summary>
        /// Returns the data using a value stored in the linked list
        /// </summary>
        /// <param name="data">Data to look for in the list</param>
        /// <returns>Node found containing the data specified</returns>
        public T Get(T data)
        {
            Node <T> node = Find(data);

            // Determine if node is null and throw exception if it is
            if (node == null)
            {
                throw new IndexOutOfRangeException("No such element");
            }
            else
            {
                return(node.GetData());
            }
        }
Beispiel #11
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);
        }