public Node selectNode(String data){
if(this.getData().equals(data)){
System.out.println("该节点存在");
return this ;
}else{
if(this.next == null){
System.out.println("该节点不存在");
}else{
this.next.selectNode(data) ;
}
}
此代码是Link类的内部类Node的一部分,该方法旨在获取满足Node.data等于给定data的节点。一下是全部代码:public class Links{
class Node{ //内部类来实现
private String data = null ;
private Node next = null ;
public Node(String data){
this.setData(data) ;
}
public void setData(String data){
this.data = data ;
}
public String getData(){
return this.data ;
}
public void linkNext(Node node){
if(this.next == null){
this.next = node ;
}else{
this.next.linkNext(node) ;
}
}
public Node selectNode(String data){
if(this.getData().equals(data)){
System.out.println("该节点存在");
return this ;
}else{
if(this.next == null){
System.out.println("该节点不存在");
}else{
this.next.selectNode(data) ;
}
}
}
}
private int length = 0 ; //记录链表长度
private Node headNode = null ; //头结点
private Node pointNode = null ; //备用指针
public void resetPointNode(){ //备用指针节点初始化
this.pointNode = null ;
}
public void addNodeToLink(String data){
Node node = new Node(data) ;
if(this.headNode == null){
this.headNode = node ;
}else{
this.headNode.linkNext(node) ;
}
this.length ++ ;
}
public void delNodeFromLink(String data){}
public void sltNodeFromLink(String data){
this.pointNode = this.headNode.selectNode(data) ;
this.resetPointNode() ;
}
public void fixNodeFromLink(String oldData,String newData){
this.pointNode = this.headNode.selectNode(oldData) ;
this.pointNode.setData(newData) ;
this.resetPointNode() ;
}
public int getLinkLength(){ //获取链表长度
return this.length ;
}
public void printLink(){
this.pointNode = this.headNode ;
for(int i=0 ;i < this.length ;i ++){
if(this.pointNode.next == null){
System.out.println(this.pointNode.getData());
}else{
System.out.print(this.pointNode.getData() + "->");
}
this.pointNode = this.pointNode.next ;
}
this.resetPointNode() ;
}
/*
public void delNodeFromLink(int nodeNum){}
public void sltNodeFromLink(int nodeNum){}
public void fixNodeFromLink(int nodeNum){}
//*/
}
添加回答
举报
0/150
提交
取消