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

            return(this.Data.CompareTo(other.Data));
        }
Esempio n. 2
0
            public bool MoveNext()
            {
                if (_current.Next != null)
                {
                    _current = _current.Next;
                }
                else
                {
                    return(false);
                }

                return(true);
            }
Esempio n. 3
0
            public bool MovePrevious()
            {
                if (_current.Previous != null)
                {
                    _current = _current.Previous;
                }
                else
                {
                    return(false);
                }

                return(true);
            }
Esempio n. 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);
            }

            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);
        }
Esempio n. 5
0
        public virtual void Prepend(T dataItem)
        {
            DLinkedListNode <T> newNode = new DLinkedListNode <T>(dataItem);

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


            _count++;
        }
Esempio n. 6
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++;
        }
Esempio n. 7
0
        /// <summary>
        /// Sets the value of the element at the specified index
        /// </summary>
        /// <param name="index">Index of element to update.</param>
        /// <returns>Element</returns>
        protected virtual void _setElementAt(int index, T value)
        {
            if (IsEmpty() || index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException("List is empty.");
            }

            if (index == 0)
            {
                _firstNode.Data = value;
            }
            else if (index == (Count - 1))
            {
                _lastNode.Data = value;
            }
            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;
                    }
                }

                currentNode.Data = value;
            }
        }
Esempio n. 8
0
        public virtual DLinkedList <T> GetRange(int index, int countOfElements)
        {
            DLinkedListNode <T> currentNode = null;
            DLinkedList <T>     newList     = new DLinkedList <T>();


            if (Count == 0)
            {
                return(newList);
            }

            if (index < 0 || index > Count)
            {
                throw new IndexOutOfRangeException();
            }
            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;
                }
            }


            while (currentNode != null && newList.Count <= countOfElements)
            {
                newList.Append(currentNode.Data);
                currentNode = currentNode.Next;
            }

            return(newList);
        }
Esempio n. 9
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++;
            }
        }
Esempio n. 10
0
        protected virtual void _setElementAt(int index, T value)
        {
            if (IsEmpty() || index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException("List is empty.");
            }

            if (index == 0)
            {
                _firstNode.Data = value;
            }
            else if (index == (Count - 1))
            {
                _lastNode.Data = value;
            }
            else
            {
                DLinkedListNode <T> currentNode = null;

                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;
                    }
                }

                currentNode.Data = value;
            }
        }
Esempio n. 11
0
        protected virtual T _getElementAt(int index)
        {
            if (IsEmpty() || index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException("List is empty.");
            }

            if (index == 0)
            {
                return(First);
            }

            if (index == (Count - 1))
            {
                return(Last);
            }

            DLinkedListNode <T> currentNode = null;

            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);
        }
Esempio n. 12
0
 public void Dispose()
 {
     _current          = null;
     _doublyLinkedList = null;
 }
Esempio n. 13
0
 public void Reset()
 {
     _current = _doublyLinkedList.Head;
 }
Esempio n. 14
0
        public virtual void RemoveFirstMatch(Predicate <T> match)
        {
            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
            {
                var currentNode = _firstNode;


                while (currentNode.Next != null)
                {
                    if (match(currentNode.Data))
                    {
                        break;
                    }

                    currentNode = currentNode.Next;
                }


                if (!match(currentNode.Data))
                {
                    throw new Exception("Item was not found!");
                }
                DLinkedListNode <T> newPrevious = currentNode.Previous;
                DLinkedListNode <T> newNext     = currentNode.Next;

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

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

                currentNode = newPrevious;
            }


            _count--;
        }
Esempio n. 15
0
 public DLinkedListEnumerator(DLinkedList <T> list)
 {
     this._current          = list.Head;
     this._doublyLinkedList = list;
 }
Esempio n. 16
0
 public DLinkedListNode(T dataItem, DLinkedListNode <T> next, DLinkedListNode <T> previous)
 {
     Data     = dataItem;
     Next     = next;
     Previous = previous;
 }
Esempio n. 17
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--;
        }