Example #1
0
        public IEnumerable <T> BackEnumerator()
        {
            DoublyNode <T> current = tail;

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

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

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

                current = current.Next;
            }
        }
Example #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);
        }
Example #4
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++;
        }
        public void RemoveFirst()
        {
            if (Count != 0)
            {
                Head = Head.Next;
                Count--;

                if (Count == 0)
                {
                    Tail = null;
                }
                else
                {
                    Head.Prev = null;
                }
            }
        }
        public void AddLast(DoublyNode <T> node)
        {
            if (Count == 0)
            {
                Head = node;
            }
            else
            {
                Tail.Next = node;

                node.Prev = Tail;
            }

            Tail = node;

            Count++;
        }
Example #7
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++;
        }
        public void RemoveLast()
        {
            if (Count != 0)
            {
                if (Count == 1)
                {
                    Head = null;
                    Tail = null;
                }
                else
                {
                    Tail.Prev.Next = null;
                    Tail           = Tail.Prev;
                }

                Count--;
            }
        }
Example #9
0
        // удаление элемента
        public bool Remove(T data)
        {
            DoublyNode <T> current = head;

            DoublyNode <T> removedItem = null;

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

            // поиск удаляемого узла
            do
            {
                if (current.Data.Equals(data))
                {
                    removedItem = current;
                    break;
                }
                current = current.Next;
            }while (current != head);

            if (removedItem != null)
            {
                // если удаляется единственный элемент списка
                if (count == 1)
                {
                    head = null;
                }
                else
                {
                    // если удаляется первый элемент
                    if (removedItem == head)
                    {
                        head = head.Next;
                    }
                    removedItem.Previous.Next = removedItem.Next;
                    removedItem.Next.Previous = removedItem.Previous;
                }
                count--;
                return(true);
            }
            return(false);
        }
Example #10
0
        public void AddFirst(DoublyNode <T> node)
        {
            var temp = Head;

            Head = node;

            Head.Next = temp;

            Count++;

            if (Count == 1)
            {
                Tail = Head;
            }
            else
            {
                temp.Prev = Head;
            }
        }
        public virtual void Add(T value)
        {
            if (head == null)
            {
                head = new DoublyNode <T>(value);
                return;
            }
            DoublyNode <T> temp = (DoublyNode <T>)head;

            while (temp.Next != null)
            {
                temp = (DoublyNode <T>)temp.Next;
            }
            temp.Next = new DoublyNode <T>(value);
            DoublyNode <T> temp2 = temp;

            temp          = (DoublyNode <T>)temp.Next;
            temp.Previous = temp2;
        }
Example #12
0
        int count;                                              // количество элементов в списке

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

            if (head == null)
            {
                head          = node;
                head.Next     = node;
                head.Previous = node;
            }
            else
            {
                node.Previous      = head.Previous;
                node.Next          = head;
                head.Previous.Next = node;
                head.Previous      = node;
            }
            count++;
        }
Example #13
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);
        }
Example #14
0
        public bool Remove(T item)
        {
            DoublyNode <T> prev    = null;
            DoublyNode <T> current = Head;

            while (current != null)
            {
                if (current.Value.Equals(item))
                {
                    if (prev != null)
                    {
                        prev.Next = current.Next;

                        if (current.Next == null)
                        {
                            Tail = prev;
                        }
                        else
                        {
                            current.Next.Prev = prev;
                        }

                        Count--;
                    }
                    else
                    {
                        RemoveFirst();
                    }

                    return(true);
                }

                prev    = current;
                current = current.Next;
            }

            return(false);
        }
Example #15
0
 public void Clear()
 {
     head  = null;
     tail  = null;
     count = 0;
 }
 public DoublyLinkedList(T initialValue)
 {
     head = new DoublyNode <T>(initialValue);
 }
Example #17
0
 public void Clear()
 {
     Head  = null;
     Tail  = null;
     Count = 0;
 }