java 单向链表

来源:互联网 发布:购买淘宝零信誉店铺 编辑:程序博客网 时间:2024/05/19 15:41

简单单向链表

[java] view plaincopyprint?
  1. class Node{  
  2.     private String data; //存储当前节点内容  
  3.     private Node   next=null//存储下一下节点  
  4.     public Node(String data){  
  5.         this.setDate(data);   
  6.     }  
  7.     public void setDate(String data){  
  8.         this.data = data;  
  9.     }  
  10.     public void setNext(Node next){  
  11.         this.next = next;  
  12.     }  
  13.     public String getDate(){  
  14.         return this.data;  
  15.     }  
  16.     public Node getNext(){  
  17.         return this.next;  
  18.     }  
  19. }  
  20. public class LinkDemo01  
  21. {  
  22.     public static void main(String args[]){  
  23.         Node n1 = new Node("节点-A");  
  24.         Node n2 = new Node("节点-B");  
  25.         Node n3 = new Node("节点-C");  
  26.         Node n4 = new Node("节点-D");  
  27.         n1.setNext(n2);  
  28.         n2.setNext(n3);  
  29.         n3.setNext(n4);  
  30.         printNode(n1);  
  31.     }  
  32.     public static void printNode(Node node){  
  33.         System.out.println(node.getDate());  
  34.         if(node.getNext()!=null){  
  35.         printNode(node.getNext());  
  36.         }  
  37.     }  
  38. }  

单向链表整合内部类

[java] view plaincopyprint?
  1. class Link  
  2. {  
  3.     class Node  
  4.     {  
  5.         private String  data;  
  6.         private Node    next=null;  
  7.         public Node(String data){  
  8.             this.setData(data);  
  9.         }  
  10.         public void setData(String data){  
  11.             this.data = data;  
  12.         }  
  13.         public void setNext(Node next){  
  14.             this.next = next;  
  15.         }  
  16.         public String getData(){  
  17.             return this.data;  
  18.         }  
  19.         public Node getNext(){  
  20.             return this.next;  
  21.         }  
  22.         public void add(Node node){  
  23.             if(this.next==null){  
  24.                 this.next = node;  
  25.             }else{  
  26.                 this.next.add(node);  
  27.             }  
  28.         }  
  29.         public void print(){  
  30.             if(this.next==null){  
  31.                 System.out.println(this.getData());  
  32.             }else{  
  33.                 System.out.println(this.getData());  
  34.                 this.next.print();  
  35.             }  
  36.         }  
  37.         public boolean search(String data){//内部搜索方法  
  38.             if(data.equals(this.data)){  
  39.                 return true;  
  40.             }else{//向下继续判断  
  41.                 if(this.next!=null){  
  42.                     return this.next.search(data);  
  43.                 }else{  
  44.                     return false;  
  45.                 }  
  46.             }  
  47.         }  
  48.         public void delete(Node previous,String data){  
  49.             if(data.equals(this.data)){  
  50.                 previous.next = this.next;//空出当前节点            
  51.             }else{  
  52.                 if(this.next!=null){  
  53.                     this.next.delete(this,data);    //继续查找  
  54.                 }  
  55.             }  
  56.         }  
  57.   
  58.     }  
  59.   
  60.     private Node root;      //根节点  
  61.     public void addNode(String data){  
  62.         Node newNode = new Node(data);      //创建新节点  
  63.         if(this.root==null){  
  64.             this.root = newNode;  
  65.         }else{  
  66.             this.root.add(newNode);  
  67.         }  
  68.     }  
  69.     public void printNode(){  
  70.         if(this.root!=null){  
  71.             this.root.print();//调用Node类中的输出操作  
  72.         }     
  73.     }  
  74.     public boolean contains(String name){  
  75.         return this.root.search(name);      //调用Node类的查找方法    
  76.     }  
  77.     public void deleteNode(String data){  
  78.         if(this.contains(data)){        //判断节点是否存在  
  79.             if(this.root.getData().equals(data)){  
  80.                 this.root = this.root.next;     //修改根节点  
  81.             }else{  
  82.                 this.root.next.delete(root,data);   //把下一个节点的前节点和数据一起传入进去  
  83.             }  
  84.         }  
  85.     }  
  86.   
  87. }  
  88.   
  89. public class LinkDemo02  
  90. {  
  91.     public static void main(String args[]){  
  92.         Link l = new Link();  
  93.         l.addNode("节点-A");  
  94.         l.addNode("节点-B");  
  95.         l.addNode("节点-C");  
  96.         l.addNode("节点-D");  
  97.         //增加之后的内容  
  98.         l.printNode();  
  99.         //判断是否包含节点  
  100.         System.out.println(l.contains("节点-X"));  
  101.         l.deleteNode("节点-B");  
  102.         //删除之后的内容  
  103.         l.printNode();  
  104.     }  
  105. }  


总结:

1.类的职能不同,LinkDemo01是基础链表类,而LinkDemo02的内部类为基础链表类,外部类为链表操作类。

2.基础链表类中存在一个this.next指向下一个链表对象。

0 0
原创粉丝点击