今天在看java版的数据结构 看到链表的时候说链表的定义是自引用结构
public class Node<T>{
  public T nodeValue;
  public Node<T> next;
   
  public Node(){
  nodeValue = null;
  next = null;
  }
}
我不明白的是这句public Node<T> next;
能不能举个例子什么的 我看这个完全绕不过来啊

解决方案 »

  1.   

    next是“指针”,从当前节点中指向链表的下一个节点。
      

  2.   

    class Link
    {
    class Node
    {
    private int data;
    private Node next;
    public Node(int data){
    this.data = data;
    }
    public void add(Node newnode){
    if(this.next == null){
    this.next = newnode;
    }
    else{
    this.next.add(newnode);
    }
    }
    public void print(){
    System.out.print(this.data+"\t");
    if(this.next!=null){
    this.next.print();
    }
    }
    public boolean search(int data){
    if(this.data == data){
    return true;
    }else{
    if(this.next!=null){
    return this.next.search(data);
    }
    else{
    return false;
    }
    }
    } //要先判断下一个节点是否存在才能继续的去寻找
    public void delete(Node prious,int data){
    if(this.data == data){
    prious.next = this.next;
    }
    else{
    if(this.next!=null)
    this.next.delete(this,data);
    }
    }
    };
    private Node root;
    public void addNode(int data){
    Node newnode = new Node(data);
    if(this.root == null){
    this.root = newnode;
    }
    else{
    this.root.add(newnode);
    }
    }
    public void printNode1(){
    if(this.root!=null){
    while(this.root!=null){
    System.out.print(this.root.data+"\t");
    this.root = this.root.next;
    }
    }
    else{
    System.out.println("Link is null!!");
    }
    } //建议不使用这样的打印方式,因为它将改变整个链表的结构
    public void printNode(){
    if(this.root!=null){
    this.root.print();
    }
    else{
    System.out.println("Link is null!!");
    }
    }
    public boolean searchNode(int data){
    return this.root.search(data);
    }
    public void deleteNode(int data){
    if(this.searchNode(data)){
    if(this.root.data == data){
    this.root = this.root.next;
    }else{
    this.root.next.delete(this.root,data);
    }
    }
    else{
    System.out.print("It does not have this node!!!");
    }
    }
    };
    public class LinkDemo01
    {
    public static void main(String args[]){
    Link l = new Link();
    l.addNode(1);
    l.addNode(2);
    l.addNode(3);
    l.addNode(6);
    l.printNode();
    System.out.println(l.searchNode(5));
    l.deleteNode(5);
    l.deleteNode(6);
    l.printNode();
    }
    };这个和那个差不多,你可以参考一下
      

  3.   

    类里保存了一个可以引用该类的类型的成员变量。
    初始值为null,还没有建立实际引用。例
    <code>
    Node<String> node1 = new Node<String>(); //建立第一个节点
    Node1.value = "node1";//第一个节点的值为node1Node<String> node2 = new Node<String>(); //建立第二个节点
    Node2.value = "node2"; //第二个节点的值为node2node1.next = node2; //第一个节点与第二个节点建立连接
    <图说明>  以下图的方式形成了链表
     node1.value:node1
     node1.next-----------> node2.value = node2
                            node2.next = null
     
      

  4.   

    一楼指针加的引号 这个类似c、c++中的指针
    自引用就是自己引用自己 你看看next的类型 还是Node吧