在定义类的时候,类中可以嵌套定义指向自身的引用(C、C++、C#)或指针(C、C++)。详见代码:
Node类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ClassEmbeddedTest
{
public class Node
{
public int value;
public Node prior;
public Node next;
}
}
主函数所在的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ClassEmbeddedTest
{
class Program
{
static void Main(string[] args)
{
List<Node> list = new List<Node>();
Node element1 = new Node();
element1.value = ;
element1.prior = null; Node element2 = new Node();
element2.value = ;
element2.prior = element1; Node element3 = new Node();
element3.value = ;
element3.prior = element2;
element3.next = null; element1.next = element2;
element2.next = element3; list.Add(element1);
list.Add(element2);
list.Add(element3); System.Console.WriteLine("列表中的元素数量为:{0}", list.Count);
}
}
}