• JAVA 实现链表


      1 package com;
      2 
      3 public class Link {
      4     
      5     private class Node{
      6         private Object node;
      7         private Node next;
      8 
      9         public Node(Object node){
     10             this.node = node;
     11         }
     12         public void addNode(Node newNode){
     13             if(this.next == null){
     14                 this.next = newNode;
     15             }else{
     16                 this.next.addNode(newNode);
     17             }
     18         }
     19         public boolean containsNode(Object data){
     20             boolean isHas = false;
     21             if(this.node.equals(data)){
     22                 isHas = true ;
     23             }else{
     24                 if(this.next != null ){
     25                     isHas = this.next.containsNode(data);
     26                 }else{
     27                     isHas = false;
     28                 }
     29             }
     30             return isHas;
     31         }
     32         public Object getNode(){
     33             return this.node;
     34         }
     35         public void toArray(){
     36             Link.this.resArr [Link.this.foot ++ ] = this.getNode();
     37             if(this.next != null ){
     38                 this.next.toArray();
     39             }
     40         }
     41         public Object getIndex(int index){
     42             if(Link.this.foot ++ == index){
     43                 return this.node;
     44             }else{
     45                 return this.next.getIndex(index);
     46             }
     47         }
     48         public boolean updateIndex(Object newData,int index){
     49             if(Link.this.foot ++ == index){
     50                 this.node = newData;
     51                 return true;
     52             }else{
     53                 return this.next.updateIndex(newData, index);
     54             }
     55         }
     56         public boolean removeData(Node node, int index){
     57             if(Link.this.foot ++ == index){
     58                 node.next = node.next.next;
     59                 Link.this.count --;
     60                 return true;
     61             }else{
     62                 return this.removeData(node.next,index);
     63             }
     64         }
     65         public boolean removeObject(Node node,Object remObj){
     66             if(this.node.equals(remObj)){
     67                 node.next = this.next;
     68                 Link.this.count --;
     69                 return true;
     70             }else{
     71                 return this.next.removeObject(this, remObj);
     72             }
     73         }
     74     }
     75     private Node root;//链表的根节点
     76     private int count = 0;
     77     private Object [] resArr;
     78     private int foot =0 ;
     79     
     80     public void add(Object node){
     81         if(node == null ){
     82             return ;
     83         }
     84         Node dataNode = new Node(node);
     85         if(root == null ){
     86             root = dataNode;
     87         }else{
     88             root.addNode(dataNode);
     89         }
     90         count ++;
     91     }
     92     public boolean contains(Object data){
     93         if(data == null || this.root == null){
     94             return false;
     95         }else {
     96             return this.root.containsNode(data);
     97         }
     98     }
     99     public boolean isEmpty(){
    100         return this.root == null ? true :false;
    101     }
    102     public int size(){
    103         return this.count;
    104     }
    105     public Object [] toArray(){
    106         if(this.count == 0){
    107             return null;
    108         }
    109         this.resArr = new Object[this.count]; //实例化数组长度
    110         this.foot = 0;//进行清零处理,需要进行脚标操作
    111         this.root.toArray();
    112         return this.resArr;
    113     }
    114     public Object get(int index){
    115         if(index >= this.count){
    116             return null;
    117         }
    118         if(this.root != null){
    119             this.foot = 0;
    120             return this.root.getIndex(index);
    121         }
    122         return null;
    123     }
    124     public boolean update(Object obj,int index){
    125         if(this.root == null || index > this.count){
    126             return false;
    127         }
    128         this.foot = 0;//初始化 脚标
    129         return this.root.updateIndex( obj, index);
    130     }
    131     public boolean remove(int index){
    132         if(this.root == null || index > this.count){
    133             return false;
    134         }
    135         if(index == 0){
    136             this.root = this.root.next;
    137             return true;
    138         }else{
    139             this.foot = 1;//初始化 脚标
    140             return this.root.removeData(this.root,index);
    141         }
    142     }
    143     public boolean removeObject(Object remData){
    144         if(!this.contains(remData)){
    145             return false;
    146         }
    147         if(this.root.node.equals(remData)){
    148             this.root = this.root.next;
    149             this.count -- ;
    150             return true;
    151         }else{
    152             return this.root.next.removeObject(this.root,remData);
    153         }
    154     }
    155 //------------------------------------------------------------------    
    156     public static void main(String[] args) {
    157         Link lk = new Link();
    158         
    159         lk.add("12232");
    160         lk.add("23");
    161         lk.add("24");
    162         lk.add("25");
    163         lk.add("26");
    164         System.out.println("----------------修改前-------------------");
    165         Object[] array = lk.toArray();
    166         for(int i=0;i<array.length;i++){
    167             System.out.println(array[i]);
    168         }
    169         
    170         lk.removeObject("23");
    171         System.out.println("----------------修改后-------------------");
    172         Object[] array1 = lk.toArray();
    173         for(int i=0;i<array1.length;i++){
    174             System.out.println(array1[i]);
    175         }
    176     }
    177 }
  • 相关阅读:
    Solution -「Gym 102798I」Sean the Cuber
    Solution -「Gym 102798K」Tree Tweaking
    Solution -「Gym 102798E」So Many Possibilities...
    Solution -「Gym 102759I」Query On A Tree 17
    Solution -「Gym 102759G」LCS 8
    Solution -「Gym 102759F」Interval Graph
    Solution -「Gym 102759C」Economic One-way Roads
    Solution -「ABC 213G」Connectivity 2
    Solution -「ABC 213H」Stroll
    @WebFilter注入失败
  • 原文地址:https://www.cnblogs.com/shiyalong/p/7488532.html
Copyright © 2020-2023  润新知