Beispiel #1
0
 private void AddData(T value, DoubleLinkedListNode <T> currentNode)
 {
     if (currentNode.Next == null)
     {
         currentNode.Next      = new DoubleLinkedListNode <T>(value);
         currentNode.Next.Prev = currentNode;
         _tail = currentNode.Next;
         Count++;
     }
     else
     {
         AddData(value, currentNode.Next);
     }
 }
Beispiel #2
0
 // Update links of nodes that stands nearby and update their neighboring nodes links.
 private void UpdateNearbyNodesLinks(DoubleLinkedListNode <T> firstNode, DoubleLinkedListNode <T> secondNode, DoubleLinkedListNode <T> prefirstNode, DoubleLinkedListNode <T> postSecondNode)
 {
     firstNode.Prev  = secondNode;
     firstNode.Next  = postSecondNode;
     secondNode.Prev = prefirstNode;
     secondNode.Next = firstNode;
     if (prefirstNode != null)
     {
         prefirstNode.Next = secondNode;
     }
     if (postSecondNode != null)
     {
         postSecondNode.Prev = firstNode;
     }
 }
Beispiel #3
0
 // Update links of common nodes and their neighboring nodes.
 private void UpdateCommonNodesLinks(DoubleLinkedListNode <T> firstNode, DoubleLinkedListNode <T> secondNode, DoubleLinkedListNode <T> preFirstNode,
                                     DoubleLinkedListNode <T> postFirstNode, DoubleLinkedListNode <T> preSecondNode, DoubleLinkedListNode <T> postSecondNode)
 {
     firstNode.Prev  = preSecondNode;
     firstNode.Next  = postSecondNode;
     secondNode.Prev = preFirstNode;
     secondNode.Next = postFirstNode;
     if (preFirstNode != null)
     {
         preFirstNode.Next = secondNode;
     }
     postFirstNode.Prev = secondNode;
     preSecondNode.Next = firstNode;
     if (postSecondNode != null)
     {
         postSecondNode.Prev = firstNode;
     }
 }
Beispiel #4
0
        private void Check(T value, DoubleLinkedListNode <T> currentNode)
        {
            if (currentNode == null)
            {
                _contains = false;
                return;
            }

            if (currentNode.Data.Equals(value))
            {
                _contains = true;
                return;
            }
            else
            {
                Check(value, currentNode.Next);
            }
        }
Beispiel #5
0
        private void RemoveData(T value, DoubleLinkedListNode <T> currentNode)
        {
            if (currentNode == null)
            {
                throw new ArgumentNullException("Can't find any value");
            }

            if (currentNode.Data.Equals(value))
            {
                // Deleting only one element - head and tail
                if (currentNode.Prev == null && currentNode.Next == null)
                {
                    _head = null;
                    _tail = null;
                }

                // Deleting first node.
                else if (currentNode.Prev == null && currentNode.Next != null)
                {
                    currentNode.Next.Prev = null;
                    _head = currentNode.Next;
                }

                // Deleting node in the middle.
                else if (currentNode.Next != null && currentNode.Prev != null)
                {
                    currentNode.Prev.Next = currentNode.Next;
                    currentNode.Next.Prev = currentNode.Prev;
                }
                // Deleting last node.
                else
                {
                    currentNode.Prev.Next = null;
                    _tail = currentNode.Prev;
                }

                currentNode = null;
                Count--;
            }
            else
            {
                RemoveData(value, currentNode.Next);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Performs swap between elements.
        /// </summary>
        /// <param name="firstValue">first value to be swaped.</param>
        /// <param name="secondValue">second value to be swaped.</param>
        public bool Swap(T firstValue, T secondValue)
        {
            if (!Contains(firstValue))
            {
                return(false);
            }

            if (!Contains(secondValue))
            {
                return(false);
            }

            // Find nodes that contains that values. Since nodes already exists, don't check result.
            DoubleLinkedListNode <T> firstNode  = GetNodeByValue(firstValue);
            DoubleLinkedListNode <T> secondNode = GetNodeByValue(secondValue);

            // Check if nodes stands nearby
            if (firstNode.Next != null && firstNode.Next.Equals(secondNode))
            {
                SwapNearbyNodes(firstNode, secondNode);
            }
            else if (secondNode.Next != null && secondNode.Next.Equals(firstNode))
            {
                SwapNearbyNodes(secondNode, firstNode);
            }
            else
            {
                // Check if first node before second, otherwise reverse params passed to method.
                var firstNodeByOrder = GetFirstNodeByOrder(firstNode, secondNode);

                // Swap values depending on their order.
                if (firstNodeByOrder.Equals(firstNode))
                {
                    SwapCommonNodes(firstNode, secondNode);
                }
                else
                {
                    SwapCommonNodes(secondNode, firstNode);
                }
            }

            return(true);
        }
Beispiel #7
0
 private void UpdateHeadAndTail(DoubleLinkedListNode <T> preFirstNode, DoubleLinkedListNode <T> firstNode, DoubleLinkedListNode <T> secondNode, DoubleLinkedListNode <T> postSecondNode)
 {
     // Swap head and tail.
     if (preFirstNode == null && postSecondNode == null)
     {
         _head = secondNode;
         _tail = firstNode;
     }
     // Swap head with common element.
     else if (preFirstNode == null)
     {
         _head = secondNode;
     }
     // Swap tail with common element.
     else if (postSecondNode == null)
     {
         _tail = firstNode;
     }
 }
Beispiel #8
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);
        }
Beispiel #9
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);
        }
Beispiel #10
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++;
        }