bakfietsland
bakfietsland

Reputation: 51

How do I set the Next property of a node in a LinkedList to another node in another LinkedList?

In C#, I am currently trying to link one node from a certain LinkedList to another node in another LinkedList. I am trying to make this for a game where tiles are connected in levels stacked on top of eachother. However, changing the Next property in the list gives an error. This is the code:

tileList2.First.Next = tileList1.First;

This is the error;

Property or indexer 'LinkedListNode.Next' cannot be assigned to -- it is read only."

How can I (otherwise) set the Next of this node to the First of the other node? What is causing this error? Is there any workaround?

Upvotes: 4

Views: 4549

Answers (2)

BlueMonkMN
BlueMonkMN

Reputation: 25601

Every node in the framework implementation of LinkedList has a reference to the list containing it, which makes transferring elements to another list O(n) instead of O(1), which defeats the purpose of the linked list implementation. If you want to transfer elements to another list in this implementation, you would have to remove and add them one by one (using Remove and AddAfter on the list) so that they each get a reference to the other list.

I suspect this is not what you're after, though. As other comments state, the needs of your list are probably simple enough that you'd be better off making your own much simpler linked list. And that question is already addressed elsewhere on SO (Creating a very simple linked list).

Since that answer doesn't include easy enumeration and initialization, I made my own.

  class ListNode<T> : IEnumerable<T>
  {
     public T data;
     public ListNode<T> Next;
     private ListNode() { }
     public ListNode(IEnumerable<T> init)
     {
        ListNode<T> current = null;
        foreach(T elem in init)
        {
           if (current == null) current = this; else current = current.Next = new ListNode<T>();
           current.data = elem;
        }
     }

     class ListEnum : IEnumerator<T>
     {
        private ListNode<T> first;
        private ListNode<T> current;
        bool more;
        public ListEnum(ListNode<T> first) { this.first = first; more = true; }
        public T Current { get { return current.data; } }
        public void Dispose(){}
        object System.Collections.IEnumerator.Current { get { return current.data; } }
        public void Reset() { current = null; more = true; }
        public bool MoveNext()
        {
           if (!more)
              return false;
           else if (current == null)
              return more = ((current = first) != null);
           else
              return more = ((current = current.Next) != null);
        }
     }

     public IEnumerator<T> GetEnumerator()
     {
        return new ListEnum(this);
     }

     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
     {
        return GetEnumerator();
     }
  }

  static void Main(string[] args)
  {
     ListNode<int> l1 = new ListNode<int>(new int[] {3,1,4,1,5,9});
     ListNode<int> l2 = new ListNode<int>(new int[] { 5 });

     l2.Next = l1.Next;
     foreach (int i in l2)
        Console.WriteLine(i);
  }

Upvotes: 5

gilmishal
gilmishal

Reputation: 1992

you can't set LinkedListNode.Next directly, it is readonly.

you also can't use LinkedList.AddLast because the LinkedlListNode you are trying to add is already in a list.

you actually need to break the lists and create new ones.

or you could implement your own linked list.

Upvotes: 1

Related Questions