Exemplo n.º 1
0
        public int this[int index]
        {
            get
            {
                DblNode current = _root;

                for (int i = 1; i <= index; i++)
                {
                    current = current.Next;
                }
                return(current.Value);
            }

            set
            {
                DblNode current = _root;
                if (index < Length / 2)
                {
                    for (int i = 1; i <= index; i++)
                    {
                        current = current.Next;
                    }
                }
                else
                {
                    for (int i = Length; i >= index; i--)
                    {
                        current = current.Previous;
                    }
                }
                current.Value = value;
            }
        }
Exemplo n.º 2
0
        public override bool Equals(object obj)
        {
            DoubleLinkedList list = (DoubleLinkedList)obj;

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

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

            if ((this._root is null) && (list._root is null))
            {
                return(true);
            }

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

                currentList = currentList.Next;
                currentThis = currentThis.Next;
            } while (!(currentThis.Next is null));

            return(true);
        }
Exemplo n.º 3
0
 public void RemoveRangeFromLast(int count) //удаляет из списка count элементов, начиная с конца
 {
     if (count > Length || count < 0)
     {
         throw new ArgumentException("Count must be greater then 0 or less then list Length");
     }
     if (count != 0)
     {
         if (Length <= count)
         {
             _root = null;
             _tail = null;
         }
         else
         {
             int     tempIndex = Length - count;
             DblNode current   = _root;
             for (int i = 0; i < tempIndex - 1; ++i)
             {
                 current = current.Next;
             }
             _tail        = current;
             current.Next = null;
             Length      -= count;
         }
     }
 }
Exemplo n.º 4
0
        public void RemoveByIndex(int index)
        {
            if (index > (Length - 1) || index < 0)
            {
                throw new IndexOutOfRangeException();
            }
            DblNode current = _root;

            if (index == 0)
            {
                RemoveFirst();
            }
            else if ((index > 0) && (index <= Length))
            {
                for (int i = 1; i < index; i++)
                {
                    current = current.Next;
                }
                Length--;
                current.Next = current.Next.Next;
            }
            if (Length == 0)
            {
                _root = null;
                _tail = _root;
            }
        }
Exemplo n.º 5
0
 public void AddListByIndex(int index, IList list) //добавление списка по индексу
 {
     if (list is DoubleLinkedList)
     {
         DoubleLinkedList linkedList = (DoubleLinkedList)list;
         if (index > Length || index < 0)
         {
             throw new IndexOutOfRangeException();
         }
         if (index == 0)
         {
             AddListFirst(linkedList);
         }
         if (index == this.Length)
         {
             AddListLast(linkedList);
         }
         else
         {
             DblNode current = this._root;
             for (int i = 1; i < index; i++)
             {
                 current = current.Next;
             }
             DblNode temp = current.Next;
             current.Next          = linkedList._root;
             linkedList._tail.Next = temp;
             Length += linkedList.Length;
         }
     }
 }
Exemplo n.º 6
0
        public void RemoveRangeFromIndex(int index, int count) //удаляет из списка count элементов, начиная с индекса index
        {
            if (index > (Length - 1) || index < 0)
            {
                throw new IndexOutOfRangeException();
            }

            if (count > (Length - index) || count < 0)
            {
                throw new ArgumentException("List has less elements then You want to Remove");
            }

            if (count == Length)
            {
                Length -= count;
                _root   = null;
                _tail   = null;
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    RemoveByIndex(index);
                }
            }
        }
Exemplo n.º 7
0
        public void Merge(DoubleLinkedList leftl, DoubleLinkedList rightl)
        {
            _root  = null;
            Length = 0;
            DblNode l = leftl._root;
            DblNode r = rightl._root;

            while (!(l is null) || !(r is null))
            {
                if (l is null)
                {
                    Add(r.Value);
                    r = r.Next;
                }
                else if (r is null)
                {
                    Add(l.Value);
                    l = l.Next;
                }
                else
                {
                    if (l.Value < r.Value)
                    {
                        Add(l.Value);
                        l = l.Next;
                    }
                    else
                    {
                        Add(r.Value);
                        r = r.Next;
                    }
                }
            }
        }
Exemplo n.º 8
0
        public void AddFirst(int value)
        {
            Length++;
            DblNode first = new DblNode(value);

            first.Previous = null;
            first.Next     = _root;
            _root          = first;
        }
Exemplo n.º 9
0
        private DblNode GetNodeByIndex(int index)
        {
            DblNode current = _root;

            for (int i = 1; i < index; i++)
            {
                current = current.Next;
            }
            return(current);
        }
Exemplo n.º 10
0
 public void AddListFirst(IList list) //добавление списка в начало
 {
     if (list is DoubleLinkedList)
     {
         DoubleLinkedList linkedList = (DoubleLinkedList)list;
         linkedList._tail.Next = this._root;
         this._root            = linkedList._root;
         Length += linkedList.Length;
     }
 }
Exemplo n.º 11
0
 public void RemoveRangeFromFirst(int count) //удаляет из списка count элементов, начиная с начала
 {
     if (count > Length || count < 0)
     {
         throw new ArgumentException("Count must be greater then 0 or less then list Length");
     }
     for (int i = 0; i < count; i++)
     {
         _root = _root.Next;
         Length--;
     }
 }
Exemplo n.º 12
0
 public void AddListLast(IList list) //добавление списка (вашего самодельного) в конец
 {
     if (list is DoubleLinkedList)
     {
         DoubleLinkedList linkedList = (DoubleLinkedList)list;
         DblNode          current    = this._tail;
         DblNode          temp       = current.Next;
         current.Next          = linkedList._root;
         linkedList._tail.Next = temp;
         Length += linkedList.Length;
     }
 }
Exemplo n.º 13
0
        public int GetFirstIndexByValue(int value) //Возврат первый индекс по значению
        {
            DblNode current = _root;

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

            return(-1);
        }
Exemplo n.º 14
0
 public void Add(int value)
 {
     if (_root is null)
     {
         AddFirst(value);
         _tail = _root;
     }
     else
     {
         Length++;
         _tail.Next          = new DblNode(value);
         _tail.Next.Previous = _tail;
         _tail = _tail.Next;
     }
 }
Exemplo n.º 15
0
        public int FindIndexOfMinValue() //поиск индекс максимального элемента
        {
            int     arrayIndexMinValue = 0;
            DblNode current            = _root;
            int     MinValue           = current.Value;

            for (int i = 0; i < Length; i++)
            {
                if (current.Value < MinValue)
                {
                    MinValue           = current.Value;
                    arrayIndexMinValue = i;
                }
                current = current.Next;
            }

            return(arrayIndexMinValue);
        }
Exemplo n.º 16
0
 public void RemoveFirst()
 {
     if (Length == 0)
     {
         throw new IndexOutOfRangeException();
     }
     if (Length == 1)
     {
         Length--;
         _root = null;
         _tail = null;
     }
     else
     {
         Length--;
         _root = _root.Next;
     }
 }
Exemplo n.º 17
0
        public override string ToString()
        {
            if (Length != 0)
            {
                DblNode current = _root;
                string  s       = current.Value + " ";

                while (!(current.Next is null))
                {
                    current = current.Next;
                    s      += current.Value + " ";
                }

                return(s);
            }
            else
            {
                return(String.Empty);
            }
        }
Exemplo n.º 18
0
        public void AddByIndex(int index, int value)
        {
            DblNode addValue = new DblNode(value);

            if ((index > 0) && (index < Length / 2))
            {
                Length++;
                DblNode current = _root;
                for (int i = 0; i < index - 1; i++)
                {
                    current = current.Next;
                }
                current.Next.Previous = new DblNode(value);
                addValue.Next         = current.Next;
                current.Next          = addValue;
            }
            else if ((index > Length / 2) && (index < Length))
            {
                Length++;
                DblNode current = _tail;
                for (int i = 0; i < Length - index; i++)
                {
                    current = current.Previous;
                }
                addValue.Next     = current.Next;
                current.Next      = addValue;
                addValue.Previous = current;
            }
            else if (index == Length - 1)
            {
                Add(value);
            }
            else if (index == 0)
            {
                AddFirst(value);
            }
            else
            {
                throw new IndexOutOfRangeException();
            }
        }
Exemplo n.º 19
0
        public void Reverse() //реверс (123 -> 321)
        {
            if (Length > 0)
            {
                DblNode previous  = null;
                DblNode current   = _root;
                DblNode following = _root;

                while (!(current is null))
                {
                    following    = following.Next;
                    current.Next = previous;
                    previous     = current;
                    current      = following;
                }

                current = _root;
                _root   = _tail;
                _tail   = current;
            }
        }
Exemplo n.º 20
0
 public void RemoveEnd()
 {
     if (Length == 0)
     {
         throw new IndexOutOfRangeException();
     }
     if (Length == 0)
     {
         throw new ArgumentException("List has less elements then You want to Remove");
     }
     else
     {
         Length--;
         DblNode current = _root;
         for (int i = 0; i < Length - 1; i++)
         {
             current = current.Next;
         }
         current.Next = null;
     }
 }
Exemplo n.º 21
0
        public DoubleLinkedList(int[] values)
        {
            Length = values.Length;

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

                DblNode current = _root;

                for (int i = 1; i < values.Length; i++, current = current.Next)
                {
                    current.Next          = new DblNode(values[i]);
                    current.Next.Previous = current;
                    _tail = _tail.Next;
                }
            }
            else
            {
                _root = null;
                _tail = _root;
            }
        }
Exemplo n.º 22
0
 public DoubleLinkedList()
 {
     Length = 0;
     _root  = null;
     _tail  = _root;
 }
Exemplo n.º 23
0
 public DblNode(int value)
 {
     Value    = value;
     Next     = null;
     Previous = null;
 }
Exemplo n.º 24
0
 public DoubleLinkedList(int value)
 {
     Length = 1;
     _root  = new DblNode(value);
     _tail  = _root;
 }