Example #1
0
        // Get first node in sequence by value.
        private DoubleLinkedListNode <T> GetNodeByValue(T value)
        {
            DoubleLinkedListNode <T> nodeToBeFind = _head;

            while (nodeToBeFind != null)
            {
                if (nodeToBeFind.Data.Equals(value))
                {
                    return(nodeToBeFind);
                }
                nodeToBeFind = nodeToBeFind.Next;
            }

            if (nodeToBeFind.Equals(_head))
            {
                nodeToBeFind = null;
            }
            return(nodeToBeFind);
        }
Example #2
0
        // Comparing who stands first in the list - first or second node.
        private DoubleLinkedListNode <T> GetFirstNodeByOrder(DoubleLinkedListNode <T> firstNode, DoubleLinkedListNode <T> secondNode)
        {
            DoubleLinkedListNode <T> firstFacedNode = _head;

            while (firstFacedNode != null)
            {
                if (firstFacedNode.Equals(firstNode))
                {
                    firstFacedNode = firstNode;
                    break;
                }
                else if (firstFacedNode.Equals(secondNode))
                {
                    firstFacedNode = secondNode;
                    break;
                }
                firstFacedNode = firstFacedNode.Next;
            }
            return(firstFacedNode);
        }
Example #3
0
        /// <summary>
        /// Insert value at the begining of the list.
        /// </summary>
        /// <param name="value">value to be inserted.</param>
        public void AddFirst(T value)
        {
            if (_head == null)
            {
                _head = new DoubleLinkedListNode <T>(value);
                _tail = _head;
            }
            else if (_head != null && _head.Equals(_tail))
            {
                _head      = new DoubleLinkedListNode <T>(value);
                _head.Next = _tail;
                _tail.Prev = _head;
            }
            else
            {
                var head          = _head;
                var nodeAfterHead = _head.Next;

                _head      = new DoubleLinkedListNode <T>(value);
                _head.Next = head;
                head.Prev  = _head;
            }
            Count++;
        }