1

I asked a question about linked lists... I think I got misunderstood... I am currently studying Linked List and I get the concept but I am a little confused in the implementation for the nodes.

public class Node
{
public Node Next;
public object Value;
}

How can you use the "public Node Next;" inside the class node. In other words how is it possible to use the object Next of type Node when you are declaring the Node itself?.... It is like recursion... And yes i know that recursion is a method calling itself ... But in this situation it is a object of the same class being created inside the class itself... That is why i say it is recursion-like

8
  • 4
    Yes, but at a certain point, Next == null and it stops. Commented Mar 17, 2017 at 11:47
  • 1
    "How can you use the "public Node Next;" inside the class node" what do you mean by that? Recursion only happens when you enable it by linking them together one after the next. So what is the question? Commented Mar 17, 2017 at 11:48
  • Recall that in C#, all class variables are references to objects on the heap (or null). Commented Mar 17, 2017 at 11:50
  • Recursion is optional, you can process a linked list quite easily with an iterative loop. It's really not clear what you are asking here. Commented Mar 17, 2017 at 11:50
  • 1
    I guess there is a misunderstanding. If you access the Next property you get a reference to the next Node instance. But this will not automatically call it's Next-property. So have to call it yourself, for example: object val = node.Next?.Next?.Value; Commented Mar 17, 2017 at 11:51

2 Answers 2

4

Reference types in C# store only a reference to the object, not the object itself. Thus, a linked list with two entries looks like this:

+----------+
|  Node    |
+----------+
| Value: 1 |     +------------+
| Next: -------> |  Node      |
+----------+     +------------+
                 | Value: 2   |
                 | Next: NULL |
                 +------------+

rather than like this:

+---------------------+
|  Node               |
+---------------------+
| Value: 1            |
| Next: +-----------+ |
|       |  Node     | |
|       +-----------+ |
|       | ...       | |
|       +-----------+ |
+---------------------+

In a nutshell, public Node Next; means: Store a reference to an object, but have the compiler ensure that this object is of type Node or a subtype thereof.

Sign up to request clarification or add additional context in comments.

4 Comments

check out Lee's comment question... That was the thing I wanted to ask
@Peter: Does the last sentence (which I just added) answer your question?
@Heinzi -- I'm curious, how do you make those ASCII diagrams? I've been googling all over but can't nail it down.
@rory.ap: I draw them manually: I indent the section with four spaces, so that it is printed in a monospace font, and then use the +, - and | keys on my keyboard. Back in the old days of usenet, this was the only way to have graphics, and some habits stick.
0

The term "recursion" refers to calling a function or method from itself. You are not calling anything (yet), so recursion is not the right term here.

What you are refering to is that the Node class contains another Node. This is possible in C#. The reason is that Node is a class, which is a reference type. A field or variable of a reference type does not contain the type itself, but a reference to the type. The reference can also be a null reference, which means the reference is empty. Since your Next property continas a reference to the next list node and not the node itself, the Node instances always have the same size and thus memory can be allocated for them without problem.

If your Node were a struct, it would be a value type, which means Next would not contain a reference to the next node, but the node itself, which would also contain the next node and the next one after that. Value types can also not be null and so this would lead to a circular layout, which is why this kind of struct layout is forbidden.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.