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;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;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

Recommended Answers

All 3 Replies

You can use the extension method .Concat() to combine 2 linked lists together. But yeah, no pointer magic like you are looking for in c#. All of the properties are readonly. I don't imagine it would be that hard to write your own linked list, however.

As for accessing items by index in the linked list... Why would you want to do that?! Linked lists are NOT optimized for indexed access. They are only optimized for adding and removing elements dynamically. A regular List<> is much more suited to accessing items by index.

Also, your algorithm isn't finding a linked list node by index. It is writing each item to the console and incrementing i, then doing nothing with it.

I threw this together out of boredom. Haven't tested it, but should be pretty much all you need! Of course, it has about a quarter the functionality of the LinkedList you are using, but can be expanded on. But it IS capable of adding a whole list to the end instead of just a node.

namespace Daniweb
{
    class SkatamaticLinkedList<T>
    {
        int iNodeCount = 0;
        public int NodeCount
        {
            get 
            {
                return iNodeCount;
            }
        }
        SkatamaticNode<T> _First = null;
        SkatamaticNode<T> First
        {
            get
            {
                return _First;
            }
            set
            {
                _First = value;
            }
        }

        SkatamaticNode<T> Last
        {
            get
            {
                SkatamaticNode<T> current = _First;
                int iCount = 0;
                while (current != null && iCount++ < iNodeCount)
                    current = current.Next;
                return current;
            }
        }

        public SkatamaticLinkedList()
        { }

        public void AddLast(SkatamaticNode<T> node)
        {
            Last.Next = node;
            iNodeCount++;
        }
        public void AddLast(SkatamaticLinkedList<T> list)
        {
            Last.Next = list.First;
            iNodeCount+=list.NodeCount;
        }

        public void AddFirst(SkatamaticNode<T> node)
        {
            node.Next = First;
            First = node;
            iNodeCount++;
        }
        public void DeleteNode(SkatamaticNode<T> node)
        {
            int iCount = 0;

            if (node == null) throw new System.InvalidOperationException("Cannot lookup a null node");

            SkatamaticNode<T> current = _First;
            while (current != null && iCount++ < iNodeCount)
                if (current.Next.Equals(node))
                {
                    //In case the one to delete is at the end of the list
                    if (node.Next != null)
                    {
                        //Set the 'next' node to be the one after the one to delete
                        current.Next = node.Next;
                        //set this new nodes previous to be the current one
                        current.Next.Previous = current;
                    }
                    else
                    {
                        //Since its at the end of the list, just null the references to it
                        current.Next.Previous = null;
                        current.Next = null;
                    }

                    iNodeCount--;
                    //Null the references so the GC can take over
                    node.Next = null;
                    node.Previous = null;
                    return;
                }
            throw new System.InvalidOperationException("Node not found in list");
        }
    }

    class SkatamaticNode<T>
    {
        public T Value { get; set; }
        private SkatamaticNode<T> _Next = null;
        public SkatamaticNode<T> Next 
        {
            get
            {
                return _Next;
            }
            set
            {
                _Next = value;
            }
        }
        private SkatamaticNode<T> _Previous = null;
        public SkatamaticNode<T> Previous 
        {
            get
            {
                return _Previous;
            }
            set
            {
                _Previous = value;
            }
        }
        public SkatamaticNode(T Data)
        {
            Value = Data;
        }
    }
}

If you want all the elements of L2 added to L1 you can add them individually:

    foreach(LinkedListNode<String> item in L2) {
        L1.AddLast(item);
    }

Or you can create a new LinkedList with all of them in it:

    LinkedList<String> L3 = new LinkedList(L1.Concat(L2));
commented: Concise +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.