Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Foreach Item in LinkedList give items in order?

Does Foreach Item in LinkedList give items in strict order?

Is the strict order First=>Next=>Next=>...=>Last respected in foreach or maybe is better to use while (item != null) ... item = item.Next?

like image 257
serhio Avatar asked Feb 16 '10 10:02

serhio


People also ask

Does foreach go in order?

Yes. you can do that. For arrays, it will access elements in order. For other types (IEnumerable, or having GetEnumerator), it accesses elements in the order provided, through alternating MoveNext and Current calls.

Does order matter in linked list?

Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.

How do you traverse a linked list in C#?

Traversing through a linked list is very easy. It requires creating a temp node pointing to the head of the list. If the temp node is not null, display its content and move to the next node using temp next. Repeat the process till the temp node becomes null.


1 Answers

Yes, it does. See http://msdn.microsoft.com/en-us/library/aa664754(VS.71).aspx and how LinkedList enumerator respects the order.

like image 71
tonio Avatar answered Nov 09 '22 22:11

tonio