Пример #1
0
        // Сортировка по убыванию.
        public void SortNumberDown()
        {
            if (Length == 0)
            {
                throw new ArgumentException("Пустое множества.");
            }

            DoubleLNode current = _root;
            int         tmp     = 0;

            for (int i = 0; i < Length; i++)
            {
                DoubleLNode node = current.Next;

                for (int j = i + 1; j < Length; j++)
                {
                    if (current.Value < node.Value)
                    {
                        tmp           = current.Value;
                        current.Value = node.Value;
                        node.Value    = tmp;
                    }

                    node = node.Next;
                }

                current = current.Next;
            }
        }
Пример #2
0
        public override string ToString()
        {
            DoubleLNode current = this;
            string      s       = current.Value + "";

            return(s);
        }
Пример #3
0
        // Добавление списка по индексу.
        public void AddListByIndex(int index, DoubleLinkedList list)
        {
            if ((index < 0) || (index > Length))
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            int[]       arr     = list.ToArray();
            int         count   = 0;
            DoubleLNode current = _root;

            if (Length != 0)
            {
                while (count == index)
                {
                    current = current.Next;
                    count++;
                }
                for (int i = 0; i < arr.Length; i++)
                {
                    AddNumberByIndex(index, arr[i]);
                    index++;
                }
            }
            else
            {
                AddListAtFront(list);
            }
        }
Пример #4
0
        private DoubleLNode GetNodeByIndex(int index)
        {
            if ((index < 0) || (index > Length))
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            DoubleLNode root = _root;
            DoubleLNode tail = _tail;

            if (index <= Length / 2)
            {
                for (int i = 1; i < index; i++)
                {
                    root = root.Next;
                }

                return(root);
            }
            else
            {
                for (int i = Length - 1; i >= index; i--)
                {
                    tail = tail.Previous;
                }

                return(tail);
            }
        }
Пример #5
0
        public DoubleLinkedList(int[] values)
        {
            if (values is null)
            {
                throw new NullReferenceException("На данное множество нет ссылки.");
            }

            Length = values.Length;

            if (values.Length != 0)
            {
                _root = new DoubleLNode(values[0]);
                _tail = _root;

                for (int i = 1; i < values.Length; i++)
                {
                    _tail.Next          = new DoubleLNode(values[i]);
                    _tail.Next.Previous = _tail;
                    _tail = _tail.Next;
                }
            }
            else
            {
                _root = null;
                _tail = _root;
            }
        }
Пример #6
0
        // Реверс
        public void ReverseItems()
        {
            if (Length == 0)
            {
                throw new ArgumentException("Пустое множество.");
            }

            DoubleLNode current = _root;
            DoubleLNode tmp     = null;

            _tail = _root;

            while (!(current is null))
            {
                tmp = current.Previous;
                current.Previous = current.Next;
                current.Next     = tmp;
                current          = current.Previous;
            }

            if (tmp != null)
            {
                _root = tmp.Previous;
            }
        }
Пример #7
0
        // Удаления по индексу N элементов.
        public void RemoveByIndexElements(int index, int items)
        {
            if (index > Length || index < 0)
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            if (Length - index < items)
            {
                throw new IndexOutOfRangeException("Длина множества после индекса меньше количества удаляемых элементов.");
            }

            if (items < 0)
            {
                throw new ArgumentException("Не существует отрицательное количество элементов.");
            }

            if ((index == 0) && (items == 1))
            {
                RemoveFirstItem();
            }
            else if ((items != 0) && (items == 1) && (index == Length - 1))
            {
                RemoveLastItem();
            }
            else if ((index + items) == Length)
            {
                RemoveSomeItemsAtLast(items);
            }
            else if ((items != 0) && (index != Length - 1))
            {
                DoubleLNode tmp = _root;

                if (index + items == Length)
                {
                    for (int i = 0; i < index + items - 1; i++)
                    {
                        tmp = tmp.Next;
                    }
                }
                else
                {
                    for (int i = 0; i < index + items; i++)
                    {
                        tmp = tmp.Next;
                    }
                }

                DoubleLNode current = GetNodeByIndex(index);
                current.Next = tmp;
                tmp.Previous = current;

                Length -= items;
            }
        }
Пример #8
0
        // Изменение по индексу.
        public void ChangeItemByIndex(int index, int value)
        {
            if ((index < 0) || (index > Length))
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            DoubleLNode current = GetNodeByIndex(index + 1);

            current.Value = value;
        }
Пример #9
0
        // Доступ по индексу.
        public int GetByIndex(int index)
        {
            if ((index < 0) || (index > Length))
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            DoubleLNode current = GetNodeByIndex(index + 1);

            return(current.Value);
        }
Пример #10
0
        public int[] ToArray()
        {
            int[]       newArray = new int[Length];
            DoubleLNode node     = _root;
            int         i        = 0;

            while (node != null)
            {
                newArray[i++] = node.Value;
                node          = node.Next;
            }

            return(newArray);
        }
Пример #11
0
        public override bool Equals(object obj)
        {
            DoubleLinkedList list = (DoubleLinkedList)obj;

            if (this.Length != list.Length)
            {
                return(false);
            }

            if (this.Length == 0)
            {
                return(true);
            }

            if (this._tail.Value != list._tail.Value)
            {
                return(false);
            }

            if (!(this._root.Previous is null) || !(list._root.Previous is null))
            {
                return(false);
            }

            if (!(this._tail.Next is null) || !(list._tail.Next is null))
            {
                return(false);
            }

            DoubleLNode currentThis = this._root;
            DoubleLNode currentList = list._root;

            while (!(currentThis.Next is null))
            {
                if (currentThis.Value != currentList.Value)
                {
                    return(false);
                }
                currentList = currentList.Next;
                currentThis = currentThis.Next;
            }

            if (currentThis.Value != currentList.Value)
            {
                return(false);
            }

            return(true);
        }
Пример #12
0
        // Первый индекс по значению.
        public int GetIndexByItem(int value)
        {
            DoubleLNode current = _root;

            for (int i = 0; i < Length; i++)
            {
                if (current.Value == value)
                {
                    return(i);
                }

                current = current.Next;
            }

            return(-1);
        }
Пример #13
0
 // Удаление из конца одного элемента.
 public void RemoveLastItem()
 {
     if (_root == _tail)
     {
         Length = 0;
         _root  = null;
         _tail  = null;
     }
     else
     {
         DoubleLNode tmp = null;
         tmp      = _tail.Previous;
         _tail    = tmp;
         tmp.Next = null;
         Length--;
     }
 }
Пример #14
0
        // Добавление значения по индексу.
        public void AddNumberByIndex(int index, int value)
        {
            if ((index < 0) || (index > Length))
            {
                throw new IndexOutOfRangeException("Индекс вне множества.");
            }

            if (index == 0)
            {
                AddNumberAtFront(value);
            }
            else if (index != Length)
            {
                DoubleLNode currentNode  = _root;
                DoubleLNode previousNode = _root;
                int         indexThis    = 0;

                while (currentNode != null)
                {
                    if (indexThis == index)
                    {
                        DoubleLNode node = new DoubleLNode(value);

                        if (currentNode == _root)
                        {
                            _root = node;
                        }
                        else
                        {
                            previousNode.Next = node;
                        }
                        node.Next = currentNode;
                        Length++;

                        return;
                    }
                    previousNode = currentNode;
                    currentNode  = currentNode.Next;
                    indexThis++;
                }
            }
            else
            {
                Add(value);
            }
        }
Пример #15
0
        // Добавление значения в начало.
        public void AddNumberAtFront(int value)
        {
            DoubleLNode node = new DoubleLNode(value);

            if (Length == 0)
            {
                _root = node;
                _tail = _root;
            }
            else
            {
                node.Next = _root;
                _root     = node;
            }

            Length++;
        }
Пример #16
0
        // Удаление из конца N элементов.
        public void RemoveSomeItemsAtLast(int items)
        {
            if (Length > 1)
            {
                DoubleLNode current = GetNodeByIndex(Length - items);

                _tail        = current;
                current.Next = null;

                Length -= items;
            }
            else
            {
                _root  = null;
                _tail  = null;
                Length = 0;
            }
        }
Пример #17
0
        // Добавление значения в конец.
        public void Add(int value)
        {
            DoubleLNode node = new DoubleLNode(value);

            if (Length == 0)
            {
                _root = node;
                _tail = _root;
            }
            else
            {
                _tail.Next    = node;
                node.Previous = _tail;
                _tail         = node;
            }

            Length++;
        }
Пример #18
0
        // Удаление из начала одного элемента.
        public void RemoveFirstItem()
        {
            if (_root == null)
            {
                throw new NullReferenceException("Нет ссылки на лист.");
            }

            if (_root == _tail)
            {
                Length = 0;
                _root  = null;
                _tail  = null;
            }
            else
            {
                _root          = _root.Next;
                _root.Previous = null;
                Length--;
            }
        }
Пример #19
0
        public int this[int index]
        {
            get
            {
                if ((index < 0) || (index > Length))
                {
                    throw new IndexOutOfRangeException("Индекс вне множества.");
                }

                DoubleLNode current = GetNodeByIndex(index);

                return(current.Value);
            }

            set
            {
                DoubleLNode current = GetNodeByIndex(index);

                current.Value = value;
            }
        }
Пример #20
0
        // Удаление из начала N элементов.
        public void RemoveSomeItemsAtFront(int items)
        {
            if (items == 1)
            {
                RemoveFirstItem();
            }
            else if (Length - 1 == items)
            {
                for (int i = 0; i < items; i++)
                {
                    RemoveFirstItem();
                }
            }
            else
            {
                DoubleLNode current = GetNodeByIndex(Length - items);

                current        = _root;
                _root.Previous = null;
                Length        -= items;
            }
        }
Пример #21
0
        // Поиск значения минимального элемента.
        public int FindMinimumNumber()
        {
            if (Length == 0)
            {
                throw new ArgumentException("Пустое множества.");
            }

            DoubleLNode node    = new DoubleLNode(0);
            DoubleLNode current = _root;

            for (int i = 0; i < Length; i++)
            {
                if (node.Value > current.Value)
                {
                    node.Value = current.Value;
                }

                current = current.Next;
            }

            return(node.Value);
        }
Пример #22
0
        public override string ToString()
        {
            // возращаем пустую строку

            if (Length != 0)
            {
                DoubleLNode current = _root;

                string s = current.Value + " ";

                // смотрит на следущшийц желемент
                while (!(current.Next is null))
                {
                    current = current.Next;
                    s      += current.Value + " ";
                }

                return(s);
            }
            else
            {
                return(String.Empty);
            }
        }
Пример #23
0
        // Удаление по значению всех.(Вернуть кол-во)
        public int RemoveAllByValue(int value)
        {
            DoubleLNode current = _root;
            int         count   = 0;

            for (int i = 0; current.Next != null; i++)
            {
                if (current.Value == value)
                {
                    RemoveByIndex(i - count);
                    count++;
                }

                current = current.Next;
            }

            if (_tail.Value == value)
            {
                RemoveLastItem();
                count++;
            }

            return(count);
        }
Пример #24
0
        // Поиск индекс минимального элемента.
        public int FindIndexOfMinimumNumber()
        {
            if (Length == 0)
            {
                throw new ArgumentException("Пустое множества.");
            }

            DoubleLNode node    = new DoubleLNode(0);
            DoubleLNode current = _root;

            for (int i = 0; i < Length; i++)
            {
                if (node.Value >= current.Value)
                {
                    node.Value = current.Value;
                }

                current = current.Next;
            }

            DoubleLNode currentVal = _root;
            int         index      = 0;

            for (int i = 0; i < Length; i++)
            {
                if (currentVal.Value == node.Value)
                {
                    index = i;
                    break;
                }

                currentVal = currentVal.Next;
            }

            return(index);
        }
Пример #25
0
 public DoubleLNode(int value)
 {
     Value    = value;
     Next     = null;
     Previous = null;
 }
Пример #26
0
 public DoubleLinkedList()
 {
     Length = 0;
     _root  = null;
     _tail  = null;
 }
Пример #27
0
 public DoubleLinkedList(int value)
 {
     Length = 1;
     _root  = new DoubleLNode(value);
     _tail  = _root;
 }