Exemplo n.º 1
0
 public void AddLast(T item)
 {
     // if head is null, then this will be the first item
     if (head == null)
     {
         this.AddFirstItem(item);
     }
     else
     {
         DoublyLinkedListNode <T> newNode = new DoublyLinkedListNode <T>(item);
         tail.Next        = newNode;
         newNode.Next     = head;
         newNode.Previous = tail;
         tail             = newNode;
         head.Previous    = tail;
     }
     ++count;
 }
Exemplo n.º 2
0
        public void CopyTo(T[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if (arrayIndex < 0 || arrayIndex > array.Length)
            {
                throw new ArgumentOutOfRangeException("arrayIndex");
            }

            DoublyLinkedListNode <T> node = this.head;

            do
            {
                array[arrayIndex++] = node.Value;
                node = node.Next;
            } while (node != head);
        }
Exemplo n.º 3
0
 public DoublyLinkedListNode <T> this[int index]
 {
     get
     {
         if (index >= count || index < 0)
         {
             throw new ArgumentOutOfRangeException("index");
         }
         else
         {
             DoublyLinkedListNode <T> node = this.head;
             for (int i = 0; i < index; i++)
             {
                 node = node.Next;
             }
             return(node);
         }
     }
 }
Exemplo n.º 4
0
        bool RemoveNode(DoublyLinkedListNode <T> nodeToRemove)
        {
            DoublyLinkedListNode <T> previous = nodeToRemove.Previous;

            previous.Next = nodeToRemove.Next;
            nodeToRemove.Next.Previous = nodeToRemove.Previous;

            // if this is head, we need to update the head reference
            if (head == nodeToRemove)
            {
                head = nodeToRemove.Next;
            }
            else if (tail == nodeToRemove)
            {
                tail = tail.Previous;
            }

            --count;
            return(true);
        }
Exemplo n.º 5
0
        public T RemoveLast()
        {
            //Case 1 - List is empty
            if (_head == null)
            {
                return(default(T));
            }
            //Case 2 - list has single element
            if (_head == _tail)
            {
                return(RemoveFirst());
            }
            //Case 3 - list has more than one element
            T result = _tail.Value;

            _tail      = _tail.Previous;
            _tail.Next = null;
            _count--;
            return(result);
        }
Exemplo n.º 6
0
        public T RemoveFirst()
        {
            //Case 1 - List is empty
            if (_head == null)
            {
                return(default(T));
            }
            T result = _head.Value;

            //Case 2 - list has one element
            if (_head == _tail)
            {
                _head = _tail = null;
            }
            //Case 3 - list has multiple elements
            else
            {
                _head          = _head.Next;
                _head.Previous = null;
            }
            _count--;
            return(result);
        }
Exemplo n.º 7
0
 public void Clear()
 {
     head  = null;
     tail  = null;
     count = 0;
 }
Exemplo n.º 8
0
        public DoublyLinkedListNode <T> Find(T item)
        {
            DoublyLinkedListNode <T> node = FindNode(head, item);

            return(node);
        }
Exemplo n.º 9
0
 public DoublyLinkedList()
 {
     _head  = null;
     _tail  = null;
     _count = 0;
 }
Exemplo n.º 10
0
 private void AddFirstItem(T item)
 {
     _head = _tail = new DoublyLinkedListNode <T>(item);
 }