Linked-List in C#????

  • Thread starter Thread starter bern11
  • Start date Start date
B

bern11

Can anyone show me how to do a linked-list in C#? All I need is the
basics - add an element and set the next & prev references. Show me
that and I can figure out the rest. I know how to do it in C++, but I'm
new to C#.
 
Hi bern,
you should look at the System.Collections.Generic.LinkedList type, it has
everything you need. It is pretty easy to use:

using System;
using System.Collections.Generic;

namespace ConsoleApplication
{
class Program
{
public static void Main(string[] args)
{
//Just some example
LinkedList<int> list = new LinkedList<int>();
LinkedListNode<int> n1 = list.AddFirst(1);
list.AddAfter(n1, 2);
}
}
}

Mark.
 
LinkedList<string> links = new LinkedList<string>();
LinkedListNode<string> first = links.AddLast("First");
LinkedListNode<string> last = links.AddFirst("Last");
LinkedListNode<string> second = links.AddBefore(last,
"second");
links.AddAfter(second, "Third");

LinkedListNode<string> third = second.Next;
string TheThirdString = third.Value;

Is that helpful for you?

Davey

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
Awesome, Thanks!
LinkedList<string> links = new LinkedList<string>();
LinkedListNode<string> first = links.AddLast("First");
LinkedListNode<string> last = links.AddFirst("Last");
LinkedListNode<string> second = links.AddBefore(last,
"second");
links.AddAfter(second, "Third");

LinkedListNode<string> third = second.Next;
string TheThirdString = third.Value;

Is that helpful for you?

Davey

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top