1.单链表
链表的形成基于节点,和指向下一个节点的索引
package math;
public class Node {
private int data ;
private Node nextNode ;
public Node(int data){
this.data = data ;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
//获取当前节点的下一个节点
public Node getNextNode(){
return this.nextNode;
}
//追加节点,同时考虑如果此节点已经包含子节点,需要向后找到至无子节点处添加
//返回的是最后那个节点
public Node append(Node node){
//拿到当前节点
Node currentNode = this ;
//开始向后遍历
while(true){
Node next = currentNode.nextNode;
if(next==null){ //此时currentNode为最后一个节点
break;
}
currentNode=next;
}
currentNode.nextNode= node ;
return currentNode ;
}
//打印所有节点信息
public void show(){
Node currentNode = this ;
while(true){
Node next = currentNode.nextNode;
System.out.println(currentNode.getData());
if(next==null){
break;
}
currentNode=next ;
}
}
//删除一个节点
public void delete(){
Node newnext = this.nextNode.nextNode;
this.nextNode= newnext ;
}
}
单链表插入节点
//插入
public void insert(Node node){
node.nextNode = this.nextNode ;
this.nextNode=node;
}
3.循环链表在单链表基础上
把nextNode = this ;
下一个节点,指向自己本身