Пример #1
0
        /// <summary>
        /// Реверс списка
        /// </summary>
        public void Reverse()
        {
            if (head == null)
            {
                return;
            }

            MyDoublyNode tmp = head;

            while (tmp.Next != null)
            {
                MyDoublyNode tmp1 = tmp.Next;
                tmp.Next = tmp.Next.Next;

                if (tmp.Previous == null)
                {
                    tmp.Previous = tmp1;
                }

                tmp1.Next     = head;
                tmp1.Previous = null;
                head          = tmp1;

                if (tmp.Next != null)
                {
                    tmp.Next.Previous = tmp;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Вставка элемента по индексу
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="val"></param>
        public void AddAt(int idx, int val)
        {
            MyDoublyNode node = new MyDoublyNode();

            node.Value = val;

            MyDoublyNode prev  = head;
            int          count = 0;

            while (count < idx)
            {
                prev = prev.Next;
                count++;
            }

            if (size == idx)
            {
                tail.Next     = node;
                node.Previous = tail;
                tail          = node;
            }

            node.Next          = prev.Next;
            prev.Next          = node;
            node.Previous      = prev;
            prev.Next.Previous = node;
            size++;
        }
Пример #3
0
        /// <summary>
        /// Удаление первого элемента списка
        /// </summary>
        public void RemoveFirst()
        {
            head          = head.Next;
            head.Previous = null;

            if (head == null)
            {
                tail = null;
            }
            size--;
        }
Пример #4
0
        /// <summary>
        /// Удаление последнего элемента списка
        /// </summary>
        public void RemoveLast()
        {
            if (head == null)
            {
                return;
            }

            tail.Previous.Next = null;
            tail = tail.Previous;

            size--;
        }
Пример #5
0
        /// <summary>
        /// Преобразование списка в массив
        /// </summary>
        /// <returns></returns>
        public int[] ToArray()
        {
            MyDoublyNode current = head;

            int[] arr   = new int[size];
            int   count = 0;

            while (current != null)
            {
                arr[count] = current.Value;
                current    = current.Next;
                count++;
            }
            return(arr);
        }
Пример #6
0
        /// <summary>
        /// Индекс элемента равного val (первый)
        /// </summary>
        /// <param name="val"></param>
        public int IndexOf(int val)
        {
            MyDoublyNode node = head;
            int          idx  = 0;

            while (node != null)
            {
                if (node.Value == val)
                {
                    return(idx);
                }
                node = node.Next;
                idx++;
            }

            return(-1);
        }
Пример #7
0
        /// <summary>
        /// Перезапись элемента по индексу на значение
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="val"></param>
        public void Set(int idx, int val)
        {
            if (idx > size)
            {
                return;
            }

            MyDoublyNode prev  = head;
            int          count = 0;

            while (count < idx)
            {
                prev = prev.Next;
                count++;
            }
            prev.Value = val;
        }
Пример #8
0
        /// <summary>
        /// Возращает элемент списка по индексу
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public int Get(int idx)
        {
            if (idx < 0 || idx > size)
            {
                return(-1);
            }

            MyDoublyNode prev  = head;
            int          count = 0;

            while (count < idx)
            {
                prev = prev.Next;
                count++;
            }

            return(prev.Value);
        }
Пример #9
0
        /// <summary>
        /// Вставка элемента в конец списка
        /// </summary>
        /// <param name="val"></param>
        public void AddLast(int val)
        {
            MyDoublyNode node = new MyDoublyNode();

            node.Value = val;

            if (head == null)
            {
                head = node;
            }
            else
            {
                tail.Next     = node;
                node.Previous = tail;
            }
            tail = node;
            size++;
        }
Пример #10
0
        /// <summary>
        /// Вставка элемента в начало списка
        /// </summary>
        /// <param name="val"></param>
        public void AddFirst(int val)
        {
            MyDoublyNode node = new MyDoublyNode();

            node.Value = val;
            node.Next  = head;
            head       = node;

            if (size == 0)
            {
                tail = head;
            }
            else
            {
                head.Previous = node;
            }

            size++;
        }
Пример #11
0
        /// <summary>
        /// Проверка наличия элемента в списке
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool Contains(int val)
        {
            MyDoublyNode node = head;

            if (GetSize() == 0)
            {
                return(false);
            }

            while (node.Next != null)
            {
                if (node.Value == val)
                {
                    return(true);
                }
                node = node.Next;
            }

            return(false);
        }
Пример #12
0
        /// <summary>
        /// Удаление всех элементов со значением val
        /// </summary>
        /// <param name="val"></param>
        public void RemoveAll(int val)
        {
            if (head == null)
            {
                return;
            }

            MyDoublyNode current = head, prev = null;

            while (current != null)
            {
                if (current.Value.Equals(val))
                {
                    if (prev == null)
                    {
                        head          = head.Next;
                        head.Previous = null;
                    }
                    else
                    {
                        if (current.Next == null)
                        {
                            tail      = prev.Previous;
                            prev.Next = null;
                        }
                        else
                        {
                            prev.Next             = current.Next;
                            current.Next.Previous = prev;
                        }
                    }

                    size--;
                }
                prev    = current;
                current = current.Next;
            }
        }
Пример #13
0
        /// <summary>
        /// Удаление по индексу
        /// </summary>
        /// <param name="idx"></param>
        public void RemoveAt(int idx)
        {
            if (head == null)
            {
                return;
            }

            MyDoublyNode current = head, prev = null;
            int          count = 0;

            while (count < idx)
            {
                prev    = current;
                current = current.Next;
                count++;
            }

            if (prev == null)
            {
                head          = head.Next;
                head.Previous = null;
            }
            else
            {
                if (current.Next == null)
                {
                    tail      = prev.Previous;
                    prev.Next = null;
                }
                else
                {
                    prev.Next             = current.Next;
                    current.Next.Previous = prev;
                }
            }
            size--;
        }
Пример #14
0
        public MyDoublyLL(int[] array)
        {
            MyDoublyNode prev = null;

            for (int i = 0; i < array.Length; i++)
            {
                MyDoublyNode node = new MyDoublyNode();
                node.Value = array[i];

                if (i == 0)
                {
                    head = node;
                    tail = node;
                }
                else
                {
                    prev.Next     = node;
                    tail          = node;
                    tail.Previous = prev;
                }
                prev = node;
            }
            size = array.Length;
        }
Пример #15
0
 public MyDoublyLL()
 {
     head = null;
     tail = null;
     size = 0;
 }