示例#1
0
        public void RemoveValuesFromEnd(int number)
        {
            if (number > Length)
            {
                throw new ArgumentOutOfRangeException("Ваш список меньше количества удаляемых элементов");
            }
            if (number <= 0)
            {
                throw new ArgumentOutOfRangeException("колличество удаляемых элементов должно быть больше 0");
            }

            if (number < Length)
            {
                DoubleNode current = _tail;
                for (int i = 1; i <= number; i++)
                {
                    current = current.Previous;
                }
                _tail        = current;
                current.Next = null;
                Length      -= number;
            }
            else
            {
                Empty();
            }
        }
示例#2
0
        public override bool Equals(object obj)
        {
            DoubleLinkedList list = (DoubleLinkedList)obj;

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

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

            if (currentList is null && currentThis is null)
            {
                return(true);
            }

            while (!(currentThis.Next is null))
            {
                if (currentThis.Value != currentList.Value)
                {
                    return(false);
                }
                currentList = currentList.Next;
                currentThis = currentThis.Next;
            }
            if (currentList.Value != currentThis.Value)
            {
                return(false);
            }
            return(true);
        }
示例#3
0
 public void AddFirst(int value)
 {
     _root.Previous      = new DoubleNode(value);
     _root.Previous.Next = _root;
     _root = _root.Previous;
     Length++;
 }
示例#4
0
        public void RemoveFirst()
        {
            if (Length == 0)
            {
                throw new ArgumentException("net elementov");
            }

            _root = _root.Next;
            Length--;
        }
示例#5
0
        public void AddListFromEnd(DoubleLinkedList list)
        {
            DoubleNode currentList = list._root;

            for (int i = 1; i <= list.Length; i++)
            {
                Add(currentList.Value);
                currentList = currentList.Next;
            }
        }
示例#6
0
        public void AddListFromBeginning(DoubleLinkedList list)
        {
            DoubleNode currentList = list._tail;

            for (int i = 1; i <= list.Length; i++)
            {
                AddFirst(currentList.Value);
                currentList = currentList.Previous;
            }
        }
        public override string ToString()
        {
            string     Str = "";
            DoubleNode now = _head;

            while (now != null)
            {
                Str += now.Data;
                now  = now.Next;
            }
            return(Str);
        }
示例#8
0
        public void RemoveLastElement()
        {
            if (Length == 0)
            {
                throw new ArgumentException("net elementov");
            }

            DoubleNode tmpDoubleNode = DoubleNodeByIndex(Length - 2);

            tmpDoubleNode.Next = null;
            _tail = tmpDoubleNode;
            Length--;
        }
示例#9
0
 public void AddListByIndex(int index, DoubleLinkedList list)
 {
     if (index >= Length || index < 0)
     {
         throw new IndexOutOfRangeException("Неверный индекс элемента");
     }
     else if (index == 0)
     {
         AddListFromBeginning(list);
     }
     else if (index <= Length / 2)
     {
         DoubleNode currentThis = _root;
         for (int i = 1; i < index; i++)
         {
             currentThis = currentThis.Next;
         }
         DoubleNode tmp1        = currentThis.Next;
         DoubleNode currentList = list._root;
         for (int i = 0; i < list.Length; i++)
         {
             DoubleNode tmp2 = currentThis.Next;
             currentThis.Next      = new DoubleNode(currentList.Value);
             currentThis.Next.Next = tmp2;
             currentList           = currentList.Next;
             currentThis           = currentThis.Next;
         }
         currentThis.Next = tmp1;
         Length          += list.Length;
     }
     else
     {
         DoubleNode currentThis = _tail;
         for (int i = 1; i <= Length - index; i++)
         {
             currentThis = currentThis.Previous;
         }
         DoubleNode tmp1        = currentThis.Next;
         DoubleNode currentList = list._root;
         for (int i = 0; i < list.Length; i++)
         {
             DoubleNode tmp2 = currentThis.Next;
             currentThis.Next      = new DoubleNode(currentList.Value);
             currentThis.Next.Next = tmp2;
             currentList           = currentList.Next;
             currentThis           = currentThis.Next;
         }
         currentThis.Next = tmp1;
         Length          += list.Length;
     }
 }
示例#10
0
        public int SearchIndexByValue(int value)
        {
            DoubleNode current = _root;

            for (int i = 0; i < Length; i++)
            {
                if (current.Value == value)
                {
                    return(i);
                }
                current = current.Next;
            }
            return(-1);
        }
示例#11
0
        public void Revers()
        {
            DoubleNode currentOne = _root;
            DoubleNode currentTwo = _tail;


            for (int i = 0; i < Length / 2; i++)
            {
                int tmp = currentOne.Value;
                currentOne.Value = currentTwo.Value;
                currentTwo.Value = tmp;
                currentOne       = currentOne.Next;
                currentTwo       = currentTwo.Previous;
            }
        }
        //构造方法
        public DoubleList(string Str)
        {
            str   = Str;
            _head = new DoubleNode(str[0]);
            int        i   = 1;
            DoubleNode now = _head;

            while (i < str.Length)
            {
                now = new DoubleNode(str[i]);
                i++;
                now.Next    = _head;
                _head.Prior = now;
                _head       = now;
            }
        }
示例#13
0
        public void SortDescending()
        {
            int[] firstElement = new int[2];
            firstElement = SearchIndexAndValueMaximumElement();
            RemoveByIndex(firstElement[0]);
            DoubleLinkedList tmpList = new DoubleLinkedList(firstElement[1]);

            while (Length != 0)
            {
                int[] maximum = SearchIndexAndValueMaximumElement();
                RemoveByIndex(maximum[0]);
                tmpList.Add(maximum[1]);
            }
            this._root = tmpList._root;
            Length     = tmpList.Length;
        }
示例#14
0
        private int[] SearchIndexAndValueMinimumElement()
        {
            int        indexMinElement = 0;
            int        minElement      = _root.Value;
            DoubleNode current         = _root;

            for (int i = 0; i < Length; i++)
            {
                if (current.Value < minElement)
                {
                    minElement      = current.Value;
                    indexMinElement = i;
                }
                current = current.Next;
            }
            return(new int[] { indexMinElement, minElement });
        }
        public void Insert(string Str)
        {
            int        i = 0;
            DoubleNode HeadTemp;

            while (i < Str.Length)
            {
                HeadTemp   = _head;
                _head      = new DoubleNode(Str[i]);
                _head.Next = HeadTemp;
                if (HeadTemp != null)
                {
                    HeadTemp.Prior = _head;
                }
                i++;
            }
        }
示例#16
0
        public void Revers()
        {
            DoubleNode current       = _root;
            DoubleNode tmpDoubleNode = new DoubleNode(0);

            while (current.Next != null)
            {
                tmpDoubleNode      = current.Next;
                current.Next       = tmpDoubleNode.Next;
                tmpDoubleNode.Next = _root;
                _root = tmpDoubleNode;
            }

            DoubleLinkedList tmpdoubleLinkedList = new DoubleLinkedList(DoubleLinkedListTomMassInt());

            _root = tmpdoubleLinkedList._root;
            _tail = tmpdoubleLinkedList._tail;
        }
示例#17
0
 public void Add(int value)
 {
     if (Length != 0)
     {
         Length++;
         //DoubleNode tmp = _tail;
         _tail.Next          = new DoubleNode(value);
         _tail.Next.Previous = _tail;
         _tail = _tail.Next;
         //Console.WriteLine($"значение рута = {_root.Value} значение первиуса = {_tail.Previous.Value} значение тейла = {_tail.Value}");
     }
     else
     {
         Length++;
         _tail = new DoubleNode(value);
         _root = _tail;
     }
 }
示例#18
0
        public void RemoveXElementsByStart(int x)
        {
            if (x > Length)
            {
                throw new ArgumentException("удаление больше элементов чем в массиве");
            }

            for (int i = 0; i < x; i++)
            {
                RemoveFirst();
            }

            if (Length == 0)
            {
                _root = null;
                _tail = null;
            }
        }
示例#19
0
 public void AddByIndex(int index, int value)
 {
     if (index == Length)
     {
         Add(value);
     }
     else if (index == 0)
     {
         AddFirst(value);
     }
     else if (index < 0 || index >= Length)
     {
         throw new IndexOutOfRangeException("Неверный индекс элемента");
     }
     else if (index <= Length / 2)
     {
         DoubleNode current = _root;
         for (int i = 1; i < index; i++)
         {
             current = current.Next;
         }
         current.Next.Previous      = new DoubleNode(value);
         current.Next.Previous.Next = current.Next;
         current.Next          = current.Next.Previous;
         current.Next.Previous = current;
         Length++;
     }
     else
     {
         if (index > Length / 2)
         {
             DoubleNode current = _tail;
             for (int i = Length - 1; i > index; i--)
             {
                 current = current.Previous;
             }
             current.Previous.Next          = new DoubleNode(value);
             current.Previous.Next.Previous = current.Previous;
             current.Previous      = current.Previous.Next;
             current.Previous.Next = current;
             Length++;
         }
     }
 }
示例#20
0
        public DoubleLinkedList(int[] values)
        {
            if (values.Length != 0)
            {
                Length = 1;
                _root  = new DoubleNode(values[0]);
                _tail  = _root;

                for (int i = 1; i < values.Length; i++)
                {
                    Add(values[i]);
                }
            }
            else
            {
                _root = null;
                _tail = null;
            }
        }
示例#21
0
        public void Add(int value)
        {
            DoubleNode tmpDoubleNode     = new DoubleNode(value);
            DoubleNode tmpPrevDoubleNode = _tail;

            if (Length != 0)
            {
                _tail.Next = tmpDoubleNode;
                _tail      = _tail.Next;
                _tail.Prev = tmpPrevDoubleNode;
            }
            else
            {
                _root = tmpDoubleNode;
                _tail = _root;
            }

            Length++;
        }
示例#22
0
        public void AddLinkedListByIndex(int index, DoubleLinkedList insertLinkedList)
        {
            Length += insertLinkedList.Length;
            DoubleNode tmpNode = DoubleNodeByIndex(index);

            int[] tmpArray = new int[Length];
            DoubleNodeByIndex(index - 1).Next = insertLinkedList._root;

            DoubleNodeByIndex(index + insertLinkedList.Length - 1).Next = tmpNode;

            for (int i = 0; i < Length; i++)
            {
                tmpArray[i] = DoubleNodeByIndex(i).Value;
            }
            DoubleLinkedList tmpLinkedList = new DoubleLinkedList(tmpArray);

            _root = tmpLinkedList._root;
            _tail = tmpLinkedList._tail;
        }
示例#23
0
        public void RemoveByIndex(int index)
        {
            DoubleNode tmpNode = DoubleNodeByIndex(index);

            if (index == 0)
            {
                RemoveFirst();
            }
            else if (index == Length - 1)
            {
                RemoveLastElement();
            }
            else
            {
                DoubleNodeByIndex(index - 1).Next = DoubleNodeByIndex(index + 1);
                DoubleNodeByIndex(index + 1).Prev = DoubleNodeByIndex(index - 1);
                Length--;
            }
        }
示例#24
0
        public override string ToString()
        {
            if (Length != 0)
            {
                DoubleNode current = _root;
                string     s       = current.Value + " ";

                while (!(current.Next is null))
                {
                    current = current.Next;
                    s      += current.Value + " ";
                }
                return(s);
            }
            else
            {
                return(String.Empty);
            }
        }
示例#25
0
        public void AddByZeroIndex(int value)
        {
            DoubleNode tmpDoubleNode     = new DoubleNode(value);
            DoubleNode tmpPrevDoubleNode = _root;

            if (Length != 0)
            {
                tmpDoubleNode.Next = tmpPrevDoubleNode;
                _root = tmpDoubleNode;
                tmpPrevDoubleNode.Prev = tmpDoubleNode;
            }
            else
            {
                _root = tmpDoubleNode;
                _tail = _root;
            }

            Length++;
        }
示例#26
0
        public void RemoveValuesFromBeginning(int number)
        {
            if (number > Length)
            {
                throw new ArgumentOutOfRangeException("Ваш список меньше количества удаляемых элементов");
            }
            if (number <= 0)
            {
                throw new ArgumentOutOfRangeException("колличество удаляемых элементов должно быть больше 0");
            }
            DoubleNode current = _root;

            for (int i = 1; i <= number; i++)
            {
                current = current.Next;
            }
            _root            = current;
            current.Previous = null;
            Length          -= number;
        }
        public void Delete(string Str)
        {
            int        Times = 0, pTimes = 0;
            DoubleNode HeadTemp, PointTemp, preTemp;

            preTemp      = new DoubleNode('0');
            HeadTemp     = preTemp;
            preTemp.Next = _head;
            PointTemp    = preTemp.Next;
            while (PointTemp != null)
            {
                for (int j = 0; PointTemp != null && j < Str.Length; j++)
                {
                    if ((char)PointTemp.Data != Str[j])
                    {
                        continue;
                    }
                    preTemp.Next = PointTemp.Next;
                    PointTemp    = PointTemp.Next;
                    if (PointTemp != null)
                    {
                        PointTemp.Prior = preTemp;
                    }
                    Times++; break;
                }
                if (Times > pTimes)
                {
                    pTimes = Times; continue;
                }
                preTemp   = preTemp.Next;
                PointTemp = PointTemp.Next;
            }
            if (Times == 0)
            {
                MessageBox.Show("不存在所要删除的元素.", "提示",
                                MessageBoxButtons.OK, MessageBoxIcon.None);
            }
            _head = HeadTemp.Next;
        }
示例#28
0
        public int this[int index]
        {
            get
            {
                DoubleNode current = _root;
                for (int i = 1; i <= index; i++)
                {
                    current = current.Next;
                }
                return(current.Value);
            }

            set
            {
                DoubleNode current = _root;
                for (int i = 1; i <= index; i++)
                {
                    current = current.Next;
                }
                current.Value = value;
            }
        }
示例#29
0
        public DoubleLinkedList(int[] values)
        {
            Length = values.Length;

            if (values.Length != 0)
            {
                _root = new DoubleNode(values[0]);
                _tail = _root;
                DoubleNode tmpDoubleNode = _root;
                for (int i = 1; i < values.Length; i++)
                {
                    tmpDoubleNode = _tail;
                    _tail.Next    = new DoubleNode(values[i]);
                    _tail         = _tail.Next;
                    _tail.Prev    = tmpDoubleNode;
                }
            }
            else
            {
                _root = null;
                _tail = null;
            }
        }
示例#30
0
        public void AddByIndex(int index, int value)
        {
            if (index == 0)
            {
                AddByZeroIndex(value);
            }
            else if (index == Length - 1)
            {
                Add(value);
            }
            else
            {
                DoubleNode firstDoubleNode  = DoubleNodeByIndex(index - 1);
                DoubleNode endDoubleNode    = DoubleNodeByIndex(index);
                DoubleNode insertDoubleNode = new DoubleNode(value);

                insertDoubleNode.Next = endDoubleNode;
                insertDoubleNode.Prev = firstDoubleNode;
                firstDoubleNode.Next  = insertDoubleNode;
                endDoubleNode.Prev    = insertDoubleNode;
                Length++;
            }
        }