Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding nodes to LinkedList<T> in foreach

Can I safely add nodes to LinkedList container inside foreach statement? Is there any difference if I used while loop? Or it is never allowed and can cause some problems?

foreach(var node in myList)
{
    if(condition)
        myList.AddLast(new MyNode());
}

Will it always work?

like image 310
Marc Andreson Avatar asked Aug 08 '10 19:08

Marc Andreson


People also ask

What is t in Linked list?

T. Specifies the element type of the linked list.

How to add new node in Linked list c#?

Inserting a new node at the end of the Linked List is very easy. First, a new node with given element is created. It is then added at the end of the list by linking the last node to the new node. The function push_back is created for this purpose.

How do you add an item to a linked list in Python?

Inserting Items at the End The following function is used to add an item at the end of the linked list. def insert_at_end(self, data): new_node = Node(data) if self. start_node is None: self. start_node = new_node return n = self.

What is a Linked list c#?

Linked List is a linear data structure which consists of a group of nodes in a sequence. Each node contains two parts. Data− Each node of a linked list can store a data. Address − Each node of a linked list contains an address to the next node, called "Next".


3 Answers

You can't modify a collection while you're enumerating over it.

From the docs for LinkedList<T>.GetEnumerator:

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.

In practice I believe it will always throw an InvalidOperationException, despite the behaviour officially being undefined.

EDIT: You asked in a comment whether a while loop would help... a while loop using GetEnumerator/MoveNext/Current wouldn't, but this will:

LinkedListNode<MyNode> current = myList.First;
while (current != null)
{
    if (condition) // use current.Value to get the value
    {
        myList.AddLast(new MyNode());
    }
    current = current.Next;
}

As far as I'm aware, that's entirely safe and predictable. You can always ask a node for its next node. If you happen to be looking at the tail node and add another one, you'll get the new tail node when you ask for "next".

If that doesn't help, please give us more details about what you're trying to achieve.

like image 117
Jon Skeet Avatar answered Nov 14 '22 03:11

Jon Skeet


No, enumerator object remembers internal version of owning collection. After collection is modified - version changed, so foreach will fail.

like image 45
desco Avatar answered Nov 14 '22 02:11

desco


While iterating over a collection with the foreach statement, you cannot modify it. Thus, adding items will result in a compiler error.

like image 24
Marius Schulz Avatar answered Nov 14 '22 03:11

Marius Schulz