Exemple #1
0
 public void AddAfter(DLinkedListNode <T> node, DLinkedListNode <T> newNode)
 {
     ValidateNode(node);
     ValidateNewNode(newNode);
     InternalInsertNodeBefore(node.next, newNode);
     newNode.list = this;
 }
Exemple #2
0
 private void InternalInsertNodeToEmptyList(DLinkedListNode <T> newNode)
 {
     Debug.Assert(head == null && count == 0, "LinkedList must be empty when this method is called!");
     newNode.next = newNode;
     newNode.prev = newNode;
     head         = newNode;
     count++;
 }
Exemple #3
0
        // -------------------------------------------------------------------------
        // Internals
        // -------------------------------------------------------------------------

        private void InternalInsertNodeBefore(DLinkedListNode <T> node, DLinkedListNode <T> newNode)
        {
            newNode.next   = node;
            newNode.prev   = node.prev;
            node.prev.next = newNode;
            node.prev      = newNode;
            count++;
        }
Exemple #4
0
        public DLinkedListNode <T> AddAfter(DLinkedListNode <T> node, T value)
        {
            ValidateNode(node);
            var result = new DLinkedListNode <T>(node.list, value);

            InternalInsertNodeBefore(node.next, result);
            return(result);
        }
Exemple #5
0
        public DLinkedList <T> Duplicate()
        {
            var result = new DLinkedList <T>();

            foreach (var v in this)
            {
                var n = new DLinkedListNode <T>(v);
                result.AddLast(n);
            }
            return(result);
        }
Exemple #6
0
 public void AddBefore(DLinkedListNode <T> node, DLinkedListNode <T> newNode)
 {
     ValidateNode(node);
     ValidateNewNode(newNode);
     InternalInsertNodeBefore(node, newNode);
     newNode.list = this;
     if (node == head)
     {
         head = newNode;
     }
 }
Exemple #7
0
        internal void ValidateNode(DLinkedListNode <T> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.list != this)
            {
                throw new InvalidOperationException("External link node");
            }
        }
Exemple #8
0
        public DLinkedListNode <T> AddBefore(DLinkedListNode <T> node, T value)
        {
            ValidateNode(node);
            var result = new DLinkedListNode <T>(node.list, value);

            InternalInsertNodeBefore(node, result);
            if (node == head)
            {
                head = result;
            }
            return(result);
        }
Exemple #9
0
        public void Clear()
        {
            var current = head;

            while (current != null)
            {
                var temp = current;
                current = current.Next;   // use Next the instead of "next", otherwise it will loop forever
                temp.Invalidate();
            }

            head  = null;
            count = 0;
        }
Exemple #10
0
        public void AddLast(DLinkedListNode <T> node)
        {
            ValidateNewNode(node);

            if (head == null)
            {
                InternalInsertNodeToEmptyList(node);
            }
            else
            {
                InternalInsertNodeBefore(head, node);
            }
            node.list = this;
        }
Exemple #11
0
        public DLinkedListNode <T> AddLast(T value)
        {
            var result = new DLinkedListNode <T>(this, value);

            if (head == null)
            {
                InternalInsertNodeToEmptyList(result);
            }
            else
            {
                InternalInsertNodeBefore(head, result);
            }
            return(result);
        }
Exemple #12
0
        /// <summary>
        /// Make sublist from give index, and up to size
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size">if -1 then up to end of list</param>
        /// <returns></returns>
        public DLinkedList <T> DuplicateReverse(int index, int size)
        {
            Debug.Assert(index >= 0);
            Debug.Assert(index < Count);
            Debug.Assert(index + size < Count);

            var result  = new DLinkedList <T>();
            var current = GetNodeAtIndex(index);

            while (current != null && (size < 0 || size > 0))
            {
                var n = new DLinkedListNode <T>(current.Value);
                result.AddFirst(n);
                size--;
                current = current.Next;
            }
            return(result);
        }
Exemple #13
0
        public void Reverse()
        {
            if (count < 2)
            {
                return;
            }

            DLinkedListNode <T> temp = null;
            var last  = First; // this will be last
            var start = First; // this is iterator

            head = Last;
            while (start != null)
            {
                temp       = start.next;
                start.next = start.prev;
                start.prev = temp;
                start      = start.Previous;
            }
        }
Exemple #14
0
 internal void InternalRemoveNode(DLinkedListNode <T> node)
 {
     Debug.Assert(node.list == this, "Deleting the node from another list!");
     Debug.Assert(head != null, "This method shouldn't be called on empty list!");
     if (node.next == node)
     {
         Debug.Assert(count == 1 && head == node, "this should only be true for a list with only one node");
         head = null;
     }
     else
     {
         node.next.prev = node.prev;
         node.prev.next = node.next;
         if (head == node)
         {
             head = node.next;
         }
     }
     node.Invalidate();
     count--;
 }
Exemple #15
0
 public void Remove(DLinkedListNode <T> node)
 {
     ValidateNode(node);
     InternalRemoveNode(node);
 }
Exemple #16
0
 internal void Invalidate()
 {
     list = null;
     next = null;
     prev = null;
 }
Exemple #17
0
 public void AddBefore(DLinkedListNode <T> newNode)
 {
     Debug.Assert(list != null);
     list.AddBefore(this, newNode);
 }