Ejemplo n.º 1
0
        public void Add(string value)
        {
            var newNode = new DoubleNode(value, lastElement);

            if (lastElement != null)
            {
                lastElement.NextNode = newNode;
            }
            lastElement = newNode;
            length++;

            if (length == 1)
            {
                firstElement = newNode;
            }
        }
Ejemplo n.º 2
0
        public override string ToString()
        {
            string s = "";

            if (Length != 0)
            {
                DoubleNode current = _head;
                s += current.Value;
                while (!(current.Next is null))
                {
                    current = current.Next;
                    s      += " " + current.Value;
                }
                return(s);
            }
            return(s);
        }
Ejemplo n.º 3
0
        //удаление из конца N элементов
        public void RemoveLastValues(int count)
        {
            //переиспользовать RemoveValuesByIndex
            CheckCountArgumentIsValid(count);
            CheckListHasEnoughElementsOrNotEmpty(count);

            if (Length == count)
            {
                CreateEmptyList();
            }
            else if (count > 0) //если count = 0 - ничего менять не нужно
            {
                int index = Length - count;
                _tail      = GetNodeByIndex(index).Prev;
                _tail.Next = null;
                Length    -= count;
            }
        }
Ejemplo n.º 4
0
        //реверс (123 -> 321)
        public void Reverse()
        {
            //_tail = _head;
            DoubleNode current = _head;
            DoubleNode tmp     = null;

            while (!(current is null))
            {
                tmp          = current.Prev;
                current.Prev = current.Next;
                current.Next = tmp;
                current      = current.Prev;
            }
            if (!(tmp is null))
            {
                _tail = GetNodeByIndex(0);
                _head = tmp.Prev;
            }
        }
Ejemplo n.º 5
0
 private DoubleNode GetNodeByIndex(int index)
 {
     if (index < Length / 2) //если индекс ближе к началу, идем сначала
     {
         DoubleNode current = _head;
         for (int i = 1; i <= index; i++)
         {
             current = current.Next;
         }
         return(current);
     }
     else
     {
         DoubleNode current = _tail;
         for (int i = Length - 2; i >= index; i--)
         {
             current = current.Prev;
         }
         return(current);
     }
 }
Ejemplo n.º 6
0
        //добавление списка по индексу
        public void AddListByIndex(ILinkedList list, int index)
        {
            CheckArgumentIsNotNull(list);

            if (index >= Length + 1 || index < 0)
            {
                throw new IndexOutOfRangeException();
            }

            DoubleLinkedList copyList = CopyList(list);

            if (this.Length == 0) //добавление к пустому листу
            {
                this._head = copyList._head;
                this._tail = copyList._tail;
            }
            else if (list.GetLength() > 0) //если в добавляемом массиве есть элементы
            {
                if (index == 0)            //добавление в начало
                {
                    copyList._tail.Next = this._head;
                    this._head.Prev     = copyList._tail;
                    _head = copyList._head;
                }
                else if (index == Length) //добавление в конец
                {
                    _tail.Next          = copyList._head;
                    copyList._head.Prev = this._tail;
                    _tail = copyList._tail;
                }
                else //добавление в середину
                {
                    copyList._tail.Next = this.GetNodeByIndex(index);
                    this.GetNodeByIndex(index).Prev.Next = copyList._head;
                    copyList._head.Prev             = this.GetNodeByIndex(index).Prev;
                    this.GetNodeByIndex(index).Prev = copyList._tail;
                }
            }
            this.Length += copyList.Length;
        }
Ejemplo n.º 7
0
        //добавление значения по индексу
        public void AddAtIndex(int value, int index)
        {
            if (index > Length || index < 0)
            {
                throw new IndexOutOfRangeException();
            } //отдельная проверка, так как тут можно добавить в пустой массив на индекс Length
            DoubleNode tmp = new DoubleNode(value);

            if (Length == 0)
            {
                CreateListWithOneElement(value);
            }
            else if (index == 0)
            {
                tmp.Prev   = null;
                tmp.Next   = _head;
                _head.Prev = tmp;
                _head      = tmp;
                Length++;
            }
            else if (index == Length)
            {
                tmp.Next   = null;
                tmp.Prev   = _tail;
                _tail.Next = tmp;
                _tail      = tmp;
                Length++;
            }
            else
            {
                tmp.Next      = GetNodeByIndex(index);
                tmp.Prev      = GetNodeByIndex(index).Prev;
                tmp.Next.Prev = tmp;
                tmp.Prev.Next = tmp;
                Length++;
            }
        }
Ejemplo n.º 8
0
        //удаление по индексу N элементов
        public void RemoveValuesByIndex(int index, int count)
        {
            CheckCountArgumentIsValid(count);
            CheckIndex(index);
            CheckListHasEnoughElementsOrNotEmpty(count);

            if (Length - index < count) //проверка subarray
            {
                throw new Exception($"Removing more elements than left in array from index {index}");
            }

            if (Length == count) //удаление сначала до конца
            {
                CreateEmptyList();
            }
            else if (index == 0) //удаление сначала не до конца
            {
                _head      = GetNodeByIndex(count);
                _head.Prev = null;
                Length    -= count;
            }
            else //если удаление с 1го индекса
            {
                if (Length - index == count) //если удаляются элементы до конца
                {
                    _tail      = GetNodeByIndex(index).Prev;
                    _tail.Next = null;
                }
                else if (count > 0) //если удаляются элементы не до конца
                {
                    int lastIndex = index + count;
                    GetNodeByIndex(lastIndex).Prev  = GetNodeByIndex(index).Prev;
                    GetNodeByIndex(index).Prev.Next = GetNodeByIndex(lastIndex);
                }
                Length -= count;
            }
        }
Ejemplo n.º 9
0
        public void Delete(string value)
        {
            length--;
            var nodeToDelete = Find(value);
            var previousNode = nodeToDelete.PreviousNode;
            var nextNode     = nodeToDelete.NextNode;

            if (previousNode != null)
            {
                previousNode.NextNode = nextNode;
            }
            else
            {
                firstElement = nextNode;
            }
            if (nextNode != null)
            {
                nextNode.PreviousNode = previousNode;
            }
            else
            {
                lastElement = previousNode;
            }
        }
Ejemplo n.º 10
0
 public DoubleNode(string value, DoubleNode previousNode)
 {
     Value        = value;
     PreviousNode = previousNode;
     NextNode     = null;
 }
Ejemplo n.º 11
0
 private void CreateListWithOneElement(int value)
 {
     Length = 1;
     _head  = new DoubleNode(value);
     _tail  = _head;
 }
Ejemplo n.º 12
0
 private void CreateEmptyList()
 {
     Length = 0;
     _head  = null;
     _tail  = _head;
 }