How to implement this structure as a class without pointers in C #?

Node of the list, in which each element points to the next element, and the head of the list will look like this:

typedef struct Node {
   int value;
   Node* next;
   Node** head;
} Node;

the head can change, so we used Node ** head. I know that classes are passed as a reference, so I can make the first 2 attributes as follows:

class Node {
  int value;
  Node next;
  ???? 
}

How to create a head attribute?

+5
source share
2 answers

Make the wrapper class a replacement for the double pointer:

class Reference<T>
{
    public T Value {get; set;}
}
+5
source

This is usually handled by passing a reference to the containing object. If this is for a linked list, for example, you can do:

class Node
{
    int Value { get; set; }
    Node Next { get; set; }
    LinkedList list;

    Node Head { get { return list.Head; } }

    public Node(LinkedList parent)
    {
       this.list = parent;
    }
}

, "head" , node, , .

+5

All Articles