TheAlgorithms/C-Sharp · error · InvalidOperationException

List is empty.

Error message

List is empty.

What it means

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.

Solutions

  1. Check list.IsEmpty() first and use the list's insert-at-head/add method for the first element
  2. Catch InvalidOperationException and fall back to inserting the first node normally
  3. Never call InsertAfter as the initial population method of an empty list

Example fix

// before
list.InsertAfter(anchor, value); // list may be empty
// after
if (list.IsEmpty()) list.Add(value);
else list.InsertAfter(anchor, value);
Defensive patterns

Strategy: validation

Validate before calling

if (!list.IsEmpty()) { list.InsertAfter(value, data); } else { /* add first node via Add/Insert */ }

Try / catch

try { list.InsertAfter(value, data); } catch (InvalidOperationException) { /* list empty: insert first node normally */ }

Prevention

When it happens

Trigger: Calling InsertAfter on a freshly constructed CircularLinkedList, or after all nodes were deleted via DeleteNode/Clear.

Common situations: 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.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/c19aef027f4f0216. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/LinkedList/CircularLinkedList/CircularLinkedList.cs:88

            }
            else
            {
                newNode.Next = tail!.Next;
                tail.Next = newNode;
                tail = newNode;
            }
        }

        /// <summary>
        /// Inserts a new node after a specific value in the list.
        /// </summary>
        /// <param name="value">The value to insert the node after.</param>
        /// <param name="data">The data to insert into the new node.</param>
        public void InsertAfter(T value, T data)
        {
            if (IsEmpty())
            {
                throw new InvalidOperationException("List is empty.");
            }

            var current = tail!.Next;
            do
            {
                if (current!.Data!.Equals(value))
                {
                    var newNode = new CircularLinkedListNode<T>(data);
                    newNode.Next = current.Next;
                    current.Next = newNode;

                    return;
                }

                current = current.Next;
            }
            while (current != tail.Next);
        }

View on GitHub (pinned to 96e2905cab)