Exemplo n.º 1
0
        /// <summary>
        /// Marks this data as the new reference node after insertion.
        /// Like insert first assuming that current reference node as head.
        /// Time complexity: O(1).
        /// </summary>
        public CircularLinkedListNode <T> Insert(T data)
        {
            var newNode = new CircularLinkedListNode <T>(data);

            //if no item exist
            if (ReferenceNode == null)
            {
                //attach the item after reference node
                newNode.Next     = newNode;
                newNode.Previous = newNode;
            }
            else
            {
                //attach the item after reference node
                newNode.Previous = ReferenceNode;
                newNode.Next     = ReferenceNode.Next;

                ReferenceNode.Next.Previous = newNode;
                ReferenceNode.Next          = newNode;
            }

            ReferenceNode = newNode;

            return(newNode);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Time complexity:  O(1).
        /// </summary>
        public void Clear()
        {
            if (ReferenceNode == null)
            {
                throw new Exception("Empty list");
            }

            ReferenceNode = null;
        }
Exemplo n.º 3
0
        //O(1) always
        public void DeleteAll()
        {
            if (ReferenceNode == null)
            {
                throw new Exception("Empty list");
            }

            ReferenceNode = null;
        }
Exemplo n.º 4
0
        //search and delete
        //cost O(n) in worst case O(nlog(n) average?
        public void Delete(T data)
        {
            if (ReferenceNode == null)
            {
                throw new Exception("Empty list");
            }

            //only one element on list
            if (ReferenceNode.Next == ReferenceNode)
            {
                if (ReferenceNode.Data.Equals(data))
                {
                    ReferenceNode = null;
                    return;
                }
                throw new Exception("Not found");
            }

            //atleast two elements from here
            var current = ReferenceNode;
            var found   = false;

            while (true)
            {
                if (current.Data.Equals(data))
                {
                    current.Prev.Next = current.Next;
                    current.Next.Prev = current.Prev;

                    //match is a reference node
                    if (current == ReferenceNode)
                    {
                        ReferenceNode = current.Next;
                    }

                    found = true;
                    break;
                }

                //terminate loop if we are about to cycle
                if (current.Next == ReferenceNode)
                {
                    break;
                }

                ///move to next item
                current = current.Next;
            }

            if (found == false)
            {
                throw new Exception("Not found");
            }
        }
Exemplo n.º 5
0
        public bool MoveNext()
        {
            if (referenceNode == null)
            {
                return(false);
            }

            if (currentNode == null)
            {
                currentNode = referenceNode;
                return(true);
            }

            if (currentNode.Next != null && currentNode.Next != referenceNode)
            {
                currentNode = currentNode.Next;
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        //O(1) delete this item
        public void Delete(CircularLinkedListNode <T> current)
        {
            if (ReferenceNode.Next == ReferenceNode)
            {
                if (ReferenceNode == current)
                {
                    ReferenceNode = null;
                    return;
                }
                throw new Exception("Not found");
            }

            current.Prev.Next = current.Next;
            current.Next.Prev = current.Prev;

            //match is a reference node
            if (current == ReferenceNode)
            {
                ReferenceNode = current.Next;
            }
        }
Exemplo n.º 7
0
 public void Dispose()
 {
     referenceNode = null;
     currentNode   = null;
 }
Exemplo n.º 8
0
 public void Reset()
 {
     currentNode = referenceNode;
 }
Exemplo n.º 9
0
 internal CircularLinkedListEnumerator(ref CircularLinkedListNode <T> referenceNode)
 {
     this.referenceNode = referenceNode;
 }