Пример #1
0
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            DoublyNode <T> current = head;

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

                    current = current.Next;
                }
            } while (current != head);
        }
Пример #2
0
        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++;
        }
Пример #3
0
        public bool Contains(T data)
        {
            DoublyNode <T> current = head;

            if (current == null)
            {
                return(false);
            }

            do
            {
                if (current.Data.Equals(data))
                {
                    return(true);
                }
                current = current.Next;
            } while (current != head);

            return(false);
        }
Пример #4
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);
        }
Пример #5
0
 public void Clear()
 {
     head  = null;
     count = 0;
 }