Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
public class Node
{ 
  public Node next; 
  public Object data; 
}


What I have tried:

The code sample is of course create a node class in regards to a linked list.
Posted
Updated 15-Mar-17 23:24pm
Comments
Kevin Marois 15-Mar-17 18:09pm    
"Public" is the Access Modifier.

"Node" is a reference to the class you're in. That means this class will have a reference to itself called 'next'.

"next" is a class level variable that you use to reference the class instance of the Node object.
Fred Ochoa 15-Mar-17 18:26pm    
""Node" is a reference to the class you're in. That means this class will have a reference to itself called 'next'."

Thank you.

I have never seen that, how can you "reference" your own class name as a datatype? It seems circular, and it doesn't make sense to me. I only understand that when it's a constructor. But this is not a constructor.
Kevin Marois 15-Mar-17 18:36pm    
There are some situations where you may need to do this..

1) Singleton Pattern. See this http://csharpindepth.com/Articles/General/Singleton.aspx

2) A Hierarchical Data structure. Think of a TreeView.

Google both of these for more info.

A Constructor is a method with no return type that has the save name as the class. It's used to initialize the class. See this https://www.codeproject.com/Articles/7011/An-Intro-to-Constructors-in-C
Fred Ochoa 15-Mar-17 18:47pm    
Okay, Kevin, thank you very much, I will look at those links. Thank you for answering very quick also!!!
Kevin Marois 15-Mar-17 18:52pm    
No problem. Good luck

No.
You are declaring a class called Node:
C#
public class Node
{ 
  ...
}
Within the class, you declare two fields:
C#
public Node next; 
public Object data;
Both fields are declared as public which means that all code outside the class can access them, as well as code inside the class.
You have not declared a constructor for the class, which means that an empty default constructor will be used.

The type of the first one is Node - which means it can refer to another instance of a Node - which is what you need to build a linked list: each Node contains a reference to the next node in the chain.

There are a couple of bad ideas here: next and data should start with an uppercase letter and should really be properties, not fields:
C#
public class Node
{ 
  public Node Next {get; set;} 
  public object Data {get; set;} 
}
You should not expose fields directly, use properties when you can instead.

You also shouldn't really use an object to contain your data, that complicates the code when you want to use the Node as you need to check what kind of data it is, and cast it to an appropriate type before you can use it. It's a much, much better idea to use a specific datatype that is appropriate to the data you are going to store in the node, like int, string, or a class of your construction.
 
Share this answer
 
Comments
Fred Ochoa 16-Mar-17 13:40pm    
Okay, thank you both Bernhard and OriginalGriff, that was very informative. I am getting a much better understanding now.
OriginalGriff 16-Mar-17 14:59pm    
You're welcome!
No, it is not a constructor. Your class rather describes some kind of "linked list": a Node contains a reference to the following/next Node, thus allowing for navigating thru that "list". Object data is the actual content of the Node.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900