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;
}

}
原文地址:https://www.cnblogs.com/baoyingying/p/11794369.html