Hello, I'm a newbie C#er...
I have just one question: I'm trying to code my own linked list;
I tried to write in C# and after I'll explain my doubt:
class Node {
public Node (int v) { next = null; val =n; }
Node next;
int val;
}
class LinkedList {
LinkedList () {start=null;}
Node start;
public addNode( Node n) { if (start = null) start = n; else ......}
}
//main
LinkedList l = new LinkedList();
l.add (new Node (10));
...........................
Now: we focus on insert of first Node..... start node will take '10'
but I don't initialized anywhere 'start' object. Is it right? Shouldn't
be something like start = new Node () anywhere?
I hope you'll understand....
thanks...
Loading
AlanPosted Nov 1, 2007, 8:08 AM
I was thinking some more about this and you don't in fact need a 'count' variable as tail will be null when the list has just been created and tail.Next will be null when you reach the end of the list. So you only need to test for null when adding new nodes or traversing the list.
You could therefore replace the code in the LinkedList class with the following:
class LinkedList
{
Node start;
Node tail;
public LinkedList()
{
start = new Node(10);
}
public void addNode(Node n)
{
if (tail == null)
{
start.Next = n;
}
else
{
tail.Next= n;
}
tail = n;
}
public void listNodes()
{
int num = 1;
Node n = start;
do
{
Console.WriteLine("Value of Node {0} = {1}", num, n.Value);
n = n.Next;
num++;
}
while (n != null);
}
}
AlanPosted Oct 31, 2007, 8:24 PM
When you add a Node as an argument to the addNode() method, it's a reference to the Node object that gets copied, not the Node itself.
As well as a start node, you also need a 'tail' node to hold a reference to the last node added, plus a 'count' variable to hold the number of nodes added.
The following code seems to work OK and should give you a more solid base on which to develop your LinkedList class:
using System;
class Test
{
static void Main()
{
LinkedList li = new LinkedList();
Node n = new Node(11);
li.addNode(n);
n = new Node(12);
li.addNode(n);
n = new Node(13);
li.addNode(n);
n = new Node(14);
li.addNode(n);
li.listNodes();
Console.ReadKey();
}
}
class Node
{
Node next = null;
int val;
public Node (int v)
{
val = v;
}
public Node Next
{
get { return next; }
set { next = value;}
}
public int Value
{
get { return val; }
}
}
class LinkedList
{
Node start;
Node tail;
int count = 1;
public LinkedList()
{
start = new Node(10);
}
public void addNode(Node n)
{
if (count == 1)
{
start.Next = n;
tail = n;
}
else
{
tail.Next = n;
tail = n;
}
count++;
}
public void listNodes()
{
int num = 1;
Node n = start;
while (num <= count)
{
Console.WriteLine("Value of Node {0} = {1}", num, n.Value);
n = n.Next;
num++;
}
}
}
MarcoPosted Oct 31, 2007, 5:47 PM
I insert the element from out LinkedList......and inside linkedlist I don't know how Node must be initialized.
Question : Listd.Add(node), does it copy the node doens't it? thanks
AlanPosted Oct 31, 2007, 10:59 AM
Yes, I think it would be more natural and convenient to initialize your 'start' node when you create a new LinkedList:
class LinkedList
{
public LinkedList() {start = new Node(10);}
Node start;
public addNode(Node n) { // code to add new nodes}
}