Hello,
I just need some help with linkedlists in C#, linked-list in C++ is kinda easy with pointers but im facing some problems in C#
I read the examples provided on http://msdn.microsoft.com but I couldn't figure out how to link two different linked lists
the efficitent way seems to be having LinkedListNode into a linked list , so lets say I have two linked lists
LinkedList<String> L1 = new LinkedList<String>();
LinkedList<String> L2 = new LinkedList<String>();
and then lets say I have the following nodes
LinkedListNode<String> Ln1 = new LinkedListNode<String>("Orange");
LinkedListNode<String> Ln2 = new LinkedListNode<String>("Banana");
LinkedListNode<String> Ln3 = new LinkedListNode<String>("Apple");
LinkedListNode<String> Ln4 = new LinkedListNode<String>("Strawberry");
I simply added them to the lists I have :
L1.AddLast(Ln1);
L1.AddLast(Ln2);
L2.AddLast(Ln3);
L2.AddLast(Ln4);
Ok now lets say I want to link the last element of L1 to the first one in L2 , is that possible ?
I tried this first :
L1.Last.Next = L2.First;
I totally failed with an error : Property or indexer 'System.Collections.Generic.LinkedListNode&amp;lt;string>.Next' cannot be assigned to -- it is read only
alright I tried this then :
`Ln2.Next = Ln3;`
I failed again
my last attempt was
LinkedListNode<String> node1=L1.Last;
LinkedListNode<String> node2 = L2.First;
node1.Next = node2;
with an error :Property or indexer 'System.Collections.Generic.LinkedListNode&amp;lt;string>.Next' cannot be assigned to -- it is read only
so any help please ? how to link them ?
an addition questions :
is there away to reach an element in a linked list by its index for example ?
I came out with this simple algorithm and it works :
int i = 0;
foreach (var item in L2)
{
Console.WriteLine(item);
i++;
}
is there an automatic way ?
thank you for your help