c++ to C#
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...
[855 byte] By [
mickey0] at [2007-11-20 11:45:24]

# 1 Re: c++ to C#
You are doing the new when you are calling the .Add method at the bottom of the code.
Out of curiosity, is this a homework assignment? If not, I just want to point out that there are already collection classes built in C#. Lists, vectors, etc are already available under the System.Collection and System.Collection.Generic (typesafe) namespaces.
Arjay at 2007-11-9 11:37:00 >

# 2 Re: c++ to C#
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...
There shouldn't be a start = new Node() anywhere. If start is null, the LL is empty. If you reach the node pointing to null, you've reached the end of your LL.
That is how I usually implement it, at least.
(Note: You can implement a linked list with an empty node in start and end - I've seen it done)
# 3 Re: c++ to C#
yes, I know that there are LIst and other but I code it for exercize:
Furthermode if I do:
List <Node> list = new List>Node ();
void myMethod () {
int x = 10;
list.Add(new Node(x));
}
// list.COunt == 1 ???
I create the Node inside a method. Is it all OK. Or outside method it'll be destroyed??
thanks.