Exemple #1
0
        public int CompareTo(DLinkedListNode <T> other)
        {
            if (other == null)
            {
                return(-1);
            }

            return(this.Data.CompareTo(other.Data));
        }
Exemple #2
0
            public bool MovePrevious()
            {
                if (_current.Previous != null)
                {
                    _current = _current.Previous;
                }
                else
                {
                    return(false);
                }

                return(true);
            }
Exemple #3
0
            public bool MoveNext()
            {
                if (_current.Next != null)
                {
                    _current = _current.Next;
                }
                else
                {
                    return(false);
                }

                return(true);
            }
Exemple #4
0
        /// <summary>
        /// Returns a number of elements as specified by countOfElements, starting from the specified index.
        /// </summary>
        /// <param name="index">Starting index.</param>
        /// <param name="countOfElements">The number of elements to return.</param>
        /// <returns>Doubly-Linked List of elements</returns>
        public virtual DLinkedList <T> GetRange(int index, int countOfElements)
        {
            DLinkedListNode <T> currentNode = null;
            DLinkedList <T>     newList     = new DLinkedList <T>();

            // Handle Index out of Bound errors
            if (Count == 0)
            {
                return(newList);
            }
            else if (index < 0 || index > Count)
            {
                throw new IndexOutOfRangeException();
            }

            // Decide from which reference to traverse the list, and then move the currentNode reference to the index
            // If index > half then traverse it from the end (_lastNode reference)
            // Otherwise, traverse it from the beginning (_firstNode refrence)
            if (index > (Count / 2))
            {
                currentNode = this._lastNode;
                for (int i = (Count - 1); i > index; --i)
                {
                    currentNode = currentNode.Previous;
                }
            }
            else
            {
                currentNode = this._firstNode;
                for (int i = 0; i < index; ++i)
                {
                    currentNode = currentNode.Next;
                }
            }

            // Append the elements to the new list using the currentNode reference
            while (currentNode != null && newList.Count <= countOfElements)
            {
                newList.Append(currentNode.Data);
                currentNode = currentNode.Next;
            }

            return(newList);
        }
Exemple #5
0
        /// <summary>
        /// Append the specified dataItem at the end of the list.
        /// </summary>
        /// <param name="dataItem">Data item.</param>
        public virtual void Append(T dataItem)
        {
            DLinkedListNode <T> newNode = new DLinkedListNode <T>(dataItem);

            if (_firstNode == null)
            {
                _firstNode = _lastNode = newNode;
            }
            else
            {
                var currentNode = _lastNode;
                currentNode.Next = newNode;
                newNode.Previous = currentNode;
                _lastNode        = newNode;
            }

            // Increment the count.
            _count++;
        }
Exemple #6
0
        /// <summary>
        /// Gets the element at the specified index
        /// </summary>
        /// <param name="index">Index of element</param>
        /// <returns>Element</returns>
        protected virtual T _getElementAt(int index)
        {
            if (IsEmpty() || index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException("List is empty.");
            }

            if (index == 0)
            {
                return(First);
            }
            else if (index == (Count - 1))
            {
                return(Last);
            }
            else
            {
                DLinkedListNode <T> currentNode = null;

                // Decide from which reference to traverse the list, and then move the currentNode reference to the index
                // If index > half then traverse it from the end (_lastNode reference)
                // Otherwise, traverse it from the beginning (_firstNode refrence)
                if (index > (Count / 2))
                {
                    currentNode = this._lastNode;
                    for (int i = (Count - 1); i > index; --i)
                    {
                        currentNode = currentNode.Previous;
                    }
                }
                else
                {
                    currentNode = this._firstNode;
                    for (int i = 0; i < index; ++i)
                    {
                        currentNode = currentNode.Next;
                    }
                }

                return(currentNode.Data);
            }
        }
Exemple #7
0
        /// <summary>
        /// Inserts the dataItem at the specified index.
        /// </summary>
        /// <param name="dataItem">Data item.</param>
        /// <param name="index">Index.</param>
        public virtual void InsertAt(T dataItem, int index)
        {
            if (index < 0 || index > Count)
            {
                throw new IndexOutOfRangeException();
            }

            if (index == 0)
            {
                Prepend(dataItem);
            }
            else if (index == Count)
            {
                Append(dataItem);
            }
            else
            {
                DLinkedListNode <T> currentNode = null;
                DLinkedListNode <T> newNode     = new DLinkedListNode <T>(dataItem);

                currentNode = this._firstNode;
                for (int i = 0; i < index - 1; ++i)
                {
                    currentNode = currentNode.Next;
                }

                var oldNext = currentNode.Next;

                if (oldNext != null)
                {
                    currentNode.Next.Previous = newNode;
                }

                newNode.Next     = oldNext;
                currentNode.Next = newNode;
                newNode.Previous = currentNode;

                // Increment the count
                _count++;
            }
        }
Exemple #8
0
 public void Dispose()
 {
     _current          = null;
     _doublyLinkedList = null;
 }
Exemple #9
0
 public void Reset()
 {
     _current = _doublyLinkedList.Head;
 }
Exemple #10
0
 public DLinkedListEnumerator(DLinkedList <T> list)
 {
     this._current          = list.Head;
     this._doublyLinkedList = list;
 }
Exemple #11
0
        /// <summary>
        /// Remove the specified dataItem.
        /// </summary>
        public virtual void RemoveFirstMatch(Predicate <T> match)
        {
            // Handle index out of bound errors
            if (IsEmpty())
            {
                throw new IndexOutOfRangeException();
            }

            if (match(_firstNode.Data))
            {
                _firstNode = _firstNode.Next;

                if (_firstNode != null)
                {
                    _firstNode.Previous = null;
                }
            }
            else if (match(_lastNode.Data))
            {
                _lastNode = _lastNode.Previous;

                if (_lastNode != null)
                {
                    _lastNode.Next = null;
                }
            }
            else
            {
                // Remove
                var currentNode = _firstNode;

                // Get currentNode to reference the element at the index.
                while (currentNode.Next != null)
                {
                    if (match(currentNode.Data))
                    {
                        break;
                    }

                    currentNode = currentNode.Next;
                }//end-while

                // If we reached the last node and item was not found
                // Throw exception
                if (!match(currentNode.Data))
                {
                    throw new Exception("Item was not found!");
                }

                // Remove element
                DLinkedListNode <T> newPrevious = currentNode.Previous;
                DLinkedListNode <T> newNext     = currentNode.Next;

                if (newPrevious != null)
                {
                    newPrevious.Next = newNext;
                }

                if (newNext != null)
                {
                    newNext.Previous = newPrevious;
                }

                currentNode = newPrevious;
            }

            // Decrement count.
            _count--;
        }
Exemple #12
0
 public DLinkedListNode(T dataItem, DLinkedListNode <T> next, DLinkedListNode <T> previous)
 {
     Data     = dataItem;
     Next     = next;
     Previous = previous;
 }