//AddAfter
        public void AddAfterIndex(int index, T itemToAdd)
        {
            if (IsEmpty())
            {
                AddToFront(itemToAdd);
            }
            else
            {
                CircularLinkedListNode <T> currentNode = Head;
                int count = 0;
                while (count < index)
                {
                    currentNode = currentNode.Next;
                    count++;
                }

                if (currentNode.Next == Head)
                {
                    AddToEnd(itemToAdd);
                }
                else
                {
                    CircularLinkedListNode <T> nodeToAdd = new CircularLinkedListNode <T>(itemToAdd, currentNode.Next, currentNode);
                    currentNode.Next          = nodeToAdd;
                    currentNode.Next.Previous = nodeToAdd;
                }
            }
        }
        //RemoveAt
        public bool RemoveAt(int index)
        {
            if (Head == null || index > Count)
            {
                return(false);
            }

            if (index == 0)
            {
                RemoveFirst();
                return(true);
            }
            else
            {
                CircularLinkedListNode <T> currentNode = Head;
                int count = 0;
                while (count < index)
                {
                    currentNode = currentNode.Next;
                    count++;
                }
                currentNode.Previous.Next = currentNode.Next;
                currentNode.Next.Previous = currentNode.Previous;
                Count--;
                return(true);
            }
        }
        public IEnumerator <CircularLinkedListNode <T> > GetEnumerator()
        {
            CircularLinkedListNode <T> currentNode = Head;

            for (int i = 0; i < Count; i++)
            {
                yield return(currentNode);

                currentNode = currentNode.Next;
            }
        }
 //AddEnd
 public void AddToEnd(T itemToAdd)
 {
     if (IsEmpty())
     {
         AddToFront(itemToAdd);
     }
     else
     {
         Tail.Next          = new CircularLinkedListNode <T>(itemToAdd, Head, Tail);
         Tail.Next.Previous = Tail;
         Tail          = Tail.Next;
         Tail.Next     = Head;
         Head.Previous = Tail;
         Count++;
     }
 }
        //AddFirst
        public void AddToFront(T itemToAdd)
        {
            if (IsEmpty())
            {
                Head = new CircularLinkedListNode <T>(itemToAdd);
                Tail = Head;
            }
            else
            {
                CircularLinkedListNode <T> nodeToAdd = new CircularLinkedListNode <T>(itemToAdd);

                nodeToAdd.Next = Head;
                Head.Previous  = nodeToAdd;
                Head           = nodeToAdd;
                Head.Previous  = Tail;
                Tail.Next      = Head;
            }
            Count++;
        }
        //RemoveLast
        public bool RemoveLast()
        {
            if (Head == null)
            {
                return(false);
            }

            if (Tail == Head)
            {
                Head = null;
                Count--;
                return(true);
            }
            else
            {
                Tail          = Tail.Previous;
                Tail.Next     = Head;
                Head.Previous = Tail;
                Count--;
                return(true);
            }
        }
Esempio n. 7
0
 public CircularLinkedListNode(T ItemToAdd, CircularLinkedListNode <T> next = null, CircularLinkedListNode <T> previous = null)
 {
     Value    = ItemToAdd;
     Next     = next;
     Previous = previous;
 }