コード例 #1
0
        public IEnumerable <T> BackEnumerator()
        {
            DoublyNode <T> current = tail;

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

                current = current.Previous;
            }
        }
コード例 #2
0
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            DoublyNode <T> current = head;

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

                current = current.Next;
            }
        }
コード例 #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 void AddFirst(T data)
        {
            DoublyNode <T> node = new DoublyNode <T>(data);
            DoublyNode <T> temp = head;

            node.Next = temp;
            head      = node;
            if (count == 0)
            {
                tail = head;
            }
            else
            {
                temp.Previous = node;
            }
            count++;
        }
コード例 #5
0
                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++;
        }
コード例 #6
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);
        }
コード例 #7
0
 //очищаем список
 public void Clear()
 {
     head  = null;
     tail  = null;
     count = 0;
 }