java 链表下(八)

//不采用内部类实现
class Node{
 private String name;
 private Node nextNode;
 
 public Node(){
  this.name = "表头";
 }
 public Node(String name){
  this.setName(name);
 }
 
 public void setName(String name){
  this.name = name;
 }
 
 public void setNextNode(Node nextNode){
  this.nextNode = nextNode;
 }
 
 public String getName(){
  return name;
 }
 
 public Node getNextNode(){
  return nextNode;
 }
}

class LinkNode{
 private static Node node;
 private static Node firstNode = new Node(); 
 private static LinkNode linkNode = new LinkNode(firstNode);
 
 private LinkNode(Node node){
  this.setNode(node);
 }
 
 public static LinkNode getLinkNode(){ 
  return linkNode;
 }
 
 public void setNode(Node node){
  this.node = node;
 }
 
 public Node getNode(){
  return node;
 }
 
 public Node getFirstNode(){
  return firstNode;
 }
 
 public void addNode(String name){
  Node node = new Node(name);
  this.getNode().setNextNode(node);
  this.setNode(node); 
 }
 
 public boolean delNode(String name){
  Node temp;
  Node node = this.getFirstNode();
  while(node.getNextNode()!= null){
   temp = node;
   if(node.getName().equals(name)){
    node = node.getNextNode();
    temp.setNextNode(temp.getNextNode().getNextNode());    
    break;
   }
  return true;  
  }
  return false;
 }
 
 
 public String selectNode(String name){
  Node node = this.getFirstNode();
  while(node.getNextNode()!= null){
   if(!node.getName().equals(name)){
    node = node.getNextNode();
    continue;
   }
   return "查找内容在此表中!";  
  }
  return "没有要查找的内容!!";
 }
 
 public String displayLinkNode(){
  String temp = "链表:";
  Node node = this.getFirstNode();
  while(node.getNextNode() != null){
   temp += node.getName();
   temp += "--&gt";
   node = node.getNextNode();
  }
  return temp + "表尾";
 }
 
 public static void main(String args[]){  
  LinkNode ln = LinkNode.getLinkNode();
  ln.addNode("girl");
  ln.addNode("boy");
  ln.addNode("ggg");
  ln.addNode("hhhh");
  ln.delNode("girl");
  System.out.println(ln.displayLinkNode());
  System.out.println(ln.selectNode("bo"));
 }
}
//采用内部类实现:
class Link{
 class Node{
  private String name ; // 保存节点的名字
  private Node next ;  // 保存下一个节点
  public Node(String name){
   this.name = name ;
  }
  public String getName(){
   return this.name ;
  }
  public void addNode(Node newNode){
   if(this.next==null){ // 后面没有东西
    this.next = newNode ;
   }else{
    this.next.addNode(newNode) ; // 向下继续查
   }
  }
  public void printNode(){
   System.out.print(this.name + " --&gt " ) ;
   if(this.next!=null){
    this.next.printNode() ; // 向下继续列出
   }
  }
  public boolean searchNode(String name){
   if(this.name.equals(name)){
    return true ;
   }else{
    if(this.next!=null){
     return this.next.searchNode(name) ;
    }else{
     return false ;
    }
   }
  }
  public void deleteNode(Node preNode,String name){
   if(this.name.equals(name)){
    preNode.next = this.next ;
   }else{
    this.next.deleteNode(this,name) ;
   }
  }
 };
 private Node root ; // 要定义出根节点
 public void add(String name){
  Node newNode = new Node(name) ;
  if(this.root==null){ // 没有根节点,则把第一个作为根节点
   this.root = newNode ;
  }else{
   this.root.addNode(newNode) ;
  }
 }
 public void print(){
  if(this.root!=null){
   this.root.printNode() ;
  }
 }
 public boolean search(String name){ // 指定查找的名字
  if(this.root!=null){
   return this.root.searchNode(name) ;
  }else{
   return false ;
  }
 }
 public void delete(String name){
  if(this.search(name)){ // 判断此节点是否存在
   if(this.root.name.equals(name)){
    if(this.root.next!=null){
     this.root = this.root.next ; // 改变根节点
    }else{
     this.root = null ; // 取消
    }
   }else{
    if(this.root.next!=null){
     this.root.next.deleteNode(root,name) ;
    }
   }
  }
 }
};
public class LinkDemo02{
 public static void main(String args[]){
  Link link = new Link() ;
  link.add("根节点") ;
  link.add("第一节点") ;
  link.add("第二节点") ;
  link.add("第三节点") ;
  link.add("第四节点") ;
  link.add("第五节点") ;
  link.print() ;
  System.out.println() ;
  // System.out.println(link.search("第x节点")) ;
  link.delete("第四节点") ;
  link.delete("根节点") ;
  link.print() ;
 }
};

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15127599/viewspace-666369/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15127599/viewspace-666369/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值