Beispiel #1
0
        public void InsertAtIndex(int index, int value)
        {
            if (index == 0)
            {
                Prepend(value);
                return;
            }

            if (index == length - 1)
            {
                Append(value);
                return;
            }

            var  newNode            = new DoublyLinkedListNode(value);
            bool isTraverseOrderFwd = (length - index) >= (length / 2);
            var  prevNode           = TraverseToPrevNode(index, isTraverseOrderFwd);

            if (isTraverseOrderFwd)
            {
                newNode.prev  = prevNode;
                newNode.next  = prevNode.next;
                prevNode.next = newNode;
                length++;
                return;
            }

            else
            {
                newNode.next  = prevNode;
                newNode.prev  = prevNode.prev;
                prevNode.prev = newNode;
                length++;
                return;
            }
        }
Beispiel #2
0
 public DoublyLinkedListNode(int _value)
 {
     value = _value;
     next  = null;
     prev  = null;
 }