Ejemplo n.º 1
0
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            DoubleNode <T> current = head;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Next;
            }
        }
Ejemplo n.º 2
0
        //проверка, содержится ли узел в списке
        public bool Contains(T data)
        {
            DoubleNode <T> current = head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    return(true);
                }
                current = current.Next;
            }
            return(false);
        }
Ejemplo n.º 3
0
        //поиск элемента в списке
        static DoubleNode <int> Find(DoubleLinkedList <int> list, int data)
        {
            DoubleNode <int> found = list.Find(data);

            if (found != null)
            {
                Console.WriteLine("Элемент найден");
                return(found);
            }
            else
            {
                Console.WriteLine("Элемент не найден");
                return(found);
            }
        }
Ejemplo n.º 4
0
        //добавление в середину списка (в место, где следующий элемнт больше текущего
        public void AddInTheMiddle(T data)
        {
            DoubleNode <T> node = new DoubleNode <T>(data);

            if (count == 0)
            {
                head = node;
                tail = head;
            }
            else
            {
                DoubleNode <T> temp = head;

                if (count == 1)//неизвестно, был элемент положительный или отрицательный (добавим в конец)
                {
                    AddInTheEnd(data);
                }
                else
                {
                    while (temp.Data.CompareTo(temp.Next.Data) == 1)//находим место для вставки
                    {
                        if (temp.Next != null)
                        {
                            temp = temp.Next;
                        }
                        else
                        {
                            AddInTheEnd(data);
                        }
                    }

                    if (temp == head)
                    {
                        AddInTheBeginning(data);
                    }
                    else
                    {
                        DoubleNode <T> next = temp.Next;
                        temp.Next     = node;
                        node.Previous = temp;
                        node.Next     = next;
                        next.Previous = node;
                    }
                }
            }
            count++;
        }
Ejemplo n.º 5
0
        //добавление в конец списка
        public void AddInTheEnd(T data)
        {
            DoubleNode <T> node = new DoubleNode <T>(data);

            if (count == 0)
            {
                head = node;
                tail = head;
            }
            else
            {
                tail.Next     = node;
                node.Previous = tail;
                tail          = node;
            }
            count++;
        }
Ejemplo n.º 6
0
        }                                         //количество элементов

        //добавление в начало списка
        public void AddInTheBeginning(T data)
        {
            DoubleNode <T> node = new DoubleNode <T>(data);
            DoubleNode <T> temp = head;

            node.Next = temp;
            head      = node;
            if (count == 0)
            {
                tail = head;
            }
            else
            {
                temp.Previous = node;
            }
            count++;
        }
Ejemplo n.º 7
0
 //поиск узла по значению
 public DoubleNode <T> Find(T data)
 {
     if (Contains(data))
     {
         DoubleNode <T> current = head;
         while (current != null)
         {
             if (current.Data.Equals(data))
             {
                 break;
             }
             current = current.Next;
         }
         return(current);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 8
0
        //удаление элемента
        public bool Remove(T data)
        {
            DoubleNode <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);
        }