Пример #1
0
        public static int Count(DoublyLinkedList <int> list)   //кол-во элементов в списке
        {
            int count             = 0;
            DoublyNode <int> head = list.head;

            while (head != null)
            {
                count++;
                head = head.Next;
            }
            return(count);
        }
Пример #2
0
        public static int Sum(DoublyLinkedList <int> list)   //сумма всех элементов списка
        {
            int sum = 0;
            DoublyNode <int> head = list.head;

            for (int i = 0; i < list.count; i++)
            {
                sum += head.Data;
                head = head.Next;
            }
            return(sum);
        }
Пример #3
0
        //---------------------------------------------------

        public bool Contains(T data)
        {
            DoublyNode <T> current = head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    return(true);
                }
                current = current.Next;
            }
            return(false);
        }
Пример #4
0
        public int count;           // количество элементов в списке

        // добавление элемента
        public void Add(T data)
        {
            DoublyNode <T> node = new DoublyNode <T>(data);

            if (head == null)
            {
                head = node;
            }
            else
            {
                tail.Next     = node;
                node.Previous = tail;
            }
            tail = node;
            count++;
        }
Пример #5
0
        //---------------------------------------------------

        public static DoublyLinkedList <T> operator *(DoublyLinkedList <T> list1, DoublyLinkedList <T> list2) //объединение двух списков
        {
            DoublyNode <T>       head1 = list1.head;
            DoublyNode <T>       head2 = list2.head;
            DoublyLinkedList <T> list3 = new DoublyLinkedList <T>();

            while (head1 != null)
            {
                list3.Add(head1.Data);
                head1 = head1.Next;
            }
            while (head2 != null)
            {
                list3.Add(head2.Data);
                head2 = head2.Next;
            }
            return(list3);
        }
Пример #6
0
        //---------------------------------------------------
        public static DoublyLinkedList <T> operator +(T data, DoublyLinkedList <T> DLL)  //добавлкение в начало
        {
            DoublyNode <T> node = new DoublyNode <T>(data);
            DoublyNode <T> temp = DLL.head;

            node.Next = temp;
            DLL.head  = node;
            if (DLL.count == 0)
            {
                DLL.tail = DLL.head;
            }
            else
            {
                temp.Previous = node;
            }
            DLL.count++;
            return(DLL);
        }
Пример #7
0
        //---------------------------------------------------

        public static bool operator ==(DoublyLinkedList <T> list1, DoublyLinkedList <T> list2)  //проверка на равенство
        {
            DoublyNode <T> head1 = list1.head;
            DoublyNode <T> head2 = list2.head;

            while (head1 != null && head2 != null)
            {
                if (Equals(head1.Data, head2.Data))
                {
                    head1 = head1.Next;
                    head2 = head2.Next;
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
        public static bool IsPereat(this DoublyLinkedList <int> list)//проверка на повторяющиеся элементы
        {
            DoublyNode <int> head  = list.head;
            DoublyNode <int> head1 = head;

            while (head != null)
            {
                while (head1 != null)
                {
                    if (head1.Data == head.Data && head1 != head)
                    {
                        return(true);
                    }
                    head1 = head1.Next;
                }
                head = head.Next;
            }
            return(false);
        }
Пример #9
0
        // удаление
        public bool Remove(T data)
        {
            DoublyNode <T> current = head;

            // поиск удаляемого узла
            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    break;
                }
                current = current.Next;
            }
            if (current != null)
            {
                // если узел не последний
                if (current.Next != null)
                {
                    current.Next.Previous = current.Previous;
                }
                else
                {
                    // если последний, переустанавливаем tail
                    tail = current.Previous;
                }

                // если узел не первый
                if (current.Previous != null)
                {
                    current.Previous.Next = current.Next;
                }
                else
                {
                    // если первый, переустанавливаем head
                    head = current.Next;
                }
                count--;
                return(true);
            }
            return(false);
        }
Пример #10
0
        //---------------------------------------------------

        public void Clear()
        {
            head  = null;
            tail  = null;
            count = 0;
        }