{"record":{"id":"c19aef027f4f0216","repo":"TheAlgorithms/C-Sharp","slug":"list-is-empty","errorCode":null,"errorMessage":"List is empty.","messagePattern":"List is empty\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs","lineNumber":88,"sourceCode":"            }\n            else\n            {\n                newNode.Next = tail!.Next;\n                tail.Next = newNode;\n                tail = newNode;\n            }\n        }\n\n        /// <summary>\n        /// Inserts a new node after a specific value in the list.\n        /// </summary>\n        /// <param name=\"value\">The value to insert the node after.</param>\n        /// <param name=\"data\">The data to insert into the new node.</param>\n        public void InsertAfter(T value, T data)\n        {\n            if (IsEmpty())\n            {\n                throw new InvalidOperationException(\"List is empty.\");\n            }\n\n            var current = tail!.Next;\n            do\n            {\n                if (current!.Data!.Equals(value))\n                {\n                    var newNode = new CircularLinkedListNode<T>(data);\n                    newNode.Next = current.Next;\n                    current.Next = newNode;\n\n                    return;\n                }\n\n                current = current.Next;\n            }\n            while (current != tail.Next);\n        }","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs#L70-L106","documentation":"CircularLinkedList.InsertAfter(value, data) throws InvalidOperationException(\"List is empty.\") when IsEmpty() is true. Inserting after an existing node requires traversing from the tail's next node; with no nodes there is nothing to traverse or anchor to.","triggerScenarios":"Calling InsertAfter on a freshly constructed CircularLinkedList, or after all nodes were deleted via DeleteNode/Clear.","commonSituations":"Seeding a circular list from a data pipeline where the first batch was empty; code assuming InsertAfter works like Add on an empty list; reusing a list instance after draining it.","solutions":["Check list.IsEmpty() first and use the list's insert-at-head/add method for the first element","Catch InvalidOperationException and fall back to inserting the first node normally","Never call InsertAfter as the initial population method of an empty list"],"exampleFix":"// before\nlist.InsertAfter(anchor, value); // list may be empty\n// after\nif (list.IsEmpty()) list.Add(value);\nelse list.InsertAfter(anchor, value);","handlingStrategy":"validation","validationCode":"if (!list.IsEmpty()) { list.InsertAfter(value, data); } else { /* add first node via Add/Insert */ }","typeGuard":null,"tryCatchPattern":"try { list.InsertAfter(value, data); } catch (InvalidOperationException) { /* list empty: insert first node normally */ }","preventionTips":["Never use InsertAfter to populate an empty list","Check IsEmpty() before anchor-based operations","After DeleteNode calls that may empty the list, re-check before further anchored inserts"],"tags":["data-structures","linked-list","empty-collection"],"backgroundTag":"empty-result-set","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}