首页 > 代码库 > java实现链表模拟LinkedList类

java实现链表模拟LinkedList类

LinkedList类底层数据结构

模拟:

  1 package Collection;
  2 
  3 public class MyLinkedList {
  4     Node first;
  5     Node last;
  6     private int size;
  7     public MyLinkedList(){
  8         
  9     }
 10     /*
 11      * 添加一个新对象
 12      * */
 13     public boolean add(Object obj){
 14         Node node = new Node();
 15         if(first == null){
 16             first = node;
 17             last = node;
 18             node.prev = null;
 19             node.next = null;
 20             node.obj = obj;
 21         }else{
 22             node.prev = last;//下一个关联上一个
 23             node.next = null;
 24             node.obj = obj;
 25             last.next = node;//上一个关联下一个
 26             last = node;
 27         }
 28         size++;
 29         return true;
 30     }
 31     /*
 32      * 在index处添加一个新元素,作为index对应的元素
 33      * */
 34     public boolean add(int index, Object obj){
 35         checkBound(index);
 36         Node node = this.first;
 37         Node nodeTemp = new Node();
 38         if(index == 0){
 39             nodeTemp.obj = obj;
 40             nodeTemp.prev = null;
 41             nodeTemp.next = this.first;
 42             this.first.prev = nodeTemp;
 43             this.first = nodeTemp;
 44             size++;
 45         }else if(index == size - 1){
 46             add(obj);
 47             size++;
 48         }else{
 49             nodeTemp.obj = obj;
 50             for(int i = 0; i < index; i++)
 51                 node = node.next;
 52             nodeTemp.next = node;
 53             nodeTemp.prev = node.prev;
 54             node.prev.next = nodeTemp;
 55             node.prev = nodeTemp;
 56             size++;
 57         }
 58         return true;
 59     }
 60     /*
 61      * 获得元素个数
 62      * */
 63     public int size(){
 64         return this.size;
 65     }
 66     /*
 67      * 按照index获得元素
 68      * */
 69     public Object get(int index){
 70         checkBound(index);
 71         Node node = this.first;
 72         for(int i = 0; i < index; i++)
 73             node = node.next;
 74         if(node != null)
 75             return node.obj;
 76         else
 77             return null;            
 78     }
 79     /*
 80      * 移除index处的元素
 81      * */
 82     public boolean remove(int index){
 83         checkBound(index);
 84         Node node = this.first;
 85         if(index == 0){
 86             this.first = this.first.next;
 87             size--;
 88         }else if(index == size - 1){
 89             for(int i = 0; i < index; i++)
 90                 node = node.next;
 91             node.prev.next = null;
 92             size--;
 93         }else{
 94             for(int i = 0; i < index; i++)
 95                 node = node.next;
 96             node.prev.next = node.next;
 97             node.next.prev = node.prev;
 98             size--;
 99         }
100         return true;
101     }
102     /*
103      * 移除obj对象
104      * */
105     public boolean remove(Object obj){
106         remove(indexOf(obj));
107         return true;
108     } 
109     /*
110      * 检索对象obj,返回index
111      * */
112     private int indexOf(Object obj){
113         Node node = this.first;
114         for(int i = 0; i < this.size; i++){
115             if(node.obj.equals(obj))
116                 return i;
117             node = node.next;
118         }
119         return -1;
120     }
121     /*
122      * 倒序检索
123      * */
124     public int lastIndexof(Object obj){
125         Node node = this.last;
126         for(int i = 0; i < this.size; i++){
127             if(node.obj.equals(obj))
128                 return size - i - 1;
129             node = node.prev;
130         }
131         return -1;
132     }
133     /*
134      * 检查边界
135      * */
136     private void checkBound(int index){
137         if(index < 0 || index > this.size)
138             try {
139                 throw new Exception();
140             } catch (Exception e) {
141                 // TODO Auto-generated catch block
142                 e.printStackTrace();
143             }
144     }
145 }
146 /*
147  * Node类
148  * */
149 class Node{
150     Node prev;
151     Node next;
152     Object obj;
153     public Node(){
154         
155     }
156 }

 

java实现链表模拟LinkedList类