示例#1
0
        /// <summary>
        /// Inserts given element at given index
        /// </summary>
        /// <param name="element">Element</param>
        /// <param name="index">insertion index</param>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the index is either lesser
        /// than 0 or greater than the length of the collection</exception>
        public override void InsertAt(T element, int index)
        {
            if (index < 0 || index > Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (index == 0)
            {
                if (_head == null)
                {
                    Add(element);
                    return;
                }
                var prevHead = _head;
                _head         = new DoubleLinkedElement <T>(element, null, prevHead);
                prevHead.Prev = _head;
                ++Length;
                return;
            }

            var current = _head;

            for (var i = 0; i < index - 1; i++)
            {
                current = current.Next;
            }
            var previousNext = current.Next;

            current.Next      = new DoubleLinkedElement <T>(element, current, previousNext);
            previousNext.Prev = current.Next;
            ++Length;
        }
示例#2
0
        /// <summary>
        /// Removes element of the collection
        /// </summary>
        /// <param name="index">Deletion index</param>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the index is either lesser
        /// than 0 or greater or equal to the length of the collection</exception>
        public void RemoveAt(int removeIndex)
        {
            if (_head == null)
            {
                return;
            }

            if (removeIndex < 0 || removeIndex >= Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (removeIndex == 0)
            {
                _head = _head.Next;
                --Length;
                return;
            }

            var current = _head;

            for (var i = 0; i < removeIndex; i++)
            {
                current = current.Next;
            }
            current.Prev.Next = current.Next;
            current           = null;
            --Length;
        }
示例#3
0
        /// <summary>
        /// Returns the last element of the collection
        /// </summary>
        /// <returns>
        /// The last element of the collection
        /// </returns>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the collection is empty</exception>
        public T Pop()
        {
            if (_last == null)
            {
                throw new IndexOutOfRangeException();
            }

            --Length;
            var currentLast = _last;

            _last = currentLast.Prev;
            return(currentLast.Value);
        }
示例#4
0
        /// <summary>
        /// Adds element to the end of the collection
        /// </summary>
        /// <param name="element">Element that will be added</param>
        public void Add(T element)
        {
            ++Length;
            if (_head == null)
            {
                _head = new DoubleLinkedElement <T>(element);
                return;
            }

            var current = _head;

            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new DoubleLinkedElement <T>(element, current);
        }
示例#5
0
        /// <summary>
        /// Adds element to collection
        /// </summary>
        /// <param name="element">Element</param>
        public void Push(T element)
        {
            ++Length;
            if (_last == null)
            {
                _last = new DoubleLinkedElement <T>(element);
                if (_first == null)
                {
                    _first = _last;
                }
                else
                {
                    _first.Next = _last;
                    _last.Prev  = _first;
                }
                return;
            }

            _last.Next = new DoubleLinkedElement <T>(element, _last);
            _last      = _last.Next;
        }
示例#6
0
 public DoubleLinkedElement(T value, DoubleLinkedElement <T> prev = null, DoubleLinkedElement <T> next = null)
 {
     Value = value;
     Prev  = prev;
     Next  = next;
 }
示例#7
0
 public DoubleLinkedList()
 {
     _head = null;
 }