zoukankan      html  css  js  c++  java
  • Java数据结构之单链表

    public class Node {
    int data;
    Node next;

    public Node(int data)
    {
    this.data = data;
    }

    //为节点追加节点
    public Node append(Node node) //返回值改成Node的好处:n1.append(n2).append(n3).append(n4);//可无限追加
    {
    //当前节点
    Node currentNode = this;
    while(true)
    {
    Node nextNode = currentNode.next;
    if(nextNode == null)
    {
    currentNode.next = node;
    break;
    }
    else
    {
    currentNode = nextNode;
    }

    }
    return this;
    }

    //获取下一个节点
    public Node next()
    {
    return this.next;
    }

    //获取节点中的数据
    public int getData()
    {
    return this.data;
    }

    //判断当前节点是否是最后一个节点
    public boolean isLast()
    {
    return this.next == null;
    }

    //显示所有节点的值
    public void show()
    {
    Node current = this;
    while(true)
    {
    if(current == null)
    {
    break;
    }
    else
    {
    System.out.print(current.data+" ");
    current = current.next;
    }
    }
    System.out.println();

    }

    //删除单链表中的一个节点
    public void deleteNext()
    {
    this.next = this.next.next;
    }

    //插入一个节点,插入的位置为当前节点的next
    public void insertNext(Node node)
    {
    node.next = this.next;
    this.next = node;
    }

    }
  • 相关阅读:
    Jar包管理规范
    Base64编码原理与应用
    MySQL 5.7.14安装说明,解决服务无法启动
    idea注册
    Oracle 如何对中文字段进行排序
    SVN错误:Attempted to lock an already-locked dir
    排序算法
    设计模式
    分层
    阿里云
  • 原文地址:https://www.cnblogs.com/baoyingying/p/11794369.html
Copyright © 2011-2022 走看看