java实现链表

  单链表

  1 package com.voole.linkedlist;
  2 
  3 public class Test {
  4     public static void main(String[] args) {
  5         LinkedList linkedList = new LinkedList();
  6         linkedList.insert(new Node(null, null));
  7         linkedList.insert(new Node(null, null));
  8         linkedList.insert(new Node(null, null));
  9         linkedList.insert(new Node(null, null));
 10         linkedList.insert(new Node(null, null));
 11         linkedList.insert(new Node(null, null),2);
 12         Node node = new Node(null, null);
 13         linkedList.update(node,2);
 14         System.out.println(linkedList.select(2));
 15         System.out.println(node);
 16     }
 17 }
 18 
 19 
 20 package com.voole.linkedlist;
 21 /**
 22  * 链表类(单链表)
 23  * @author TMAC-J
 24  *
 25  */
 26 public class LinkedList {
 27     /**
 28      * 定义头结点
 29      */
 30     private Node head = null;
 31     /**
 32      * 定义链表长度
 33      */
 34     private int size = 0;
 35     /**
 36      * 定义指针,当为0时,代表指向头结点
 37      */
 38     private int point = 0;
 39     /**
 40      * 构造方法
 41      */
 42     public LinkedList(){
 43         head = new Node(null, null);//初始化头结点
 44     }
 45     /**
 46      * 增(在末尾)
 47      */
 48     public void insert(Node node){
 49         Node currentNode = null;
 50         while(point!=size){
 51             if(point == 0){
 52                 currentNode = head.next; 
 53             }
 54             else{
 55                 currentNode = currentNode.next;
 56             }
 57             point++;
 58         }
 59         /**
 60          * if-else防止currentNode.next的空指针异常
 61          */
 62         if(currentNode!=null){
 63             currentNode.next = node;
 64         }
 65         else{
 66             head.next = node;
 67         }
 68         point = 0;//将指针指向头结点
 69         size++;
 70         LinkedListLog.getInstance().insert();
 71     }
 72     /**
 73      * 增(在任意位置)
 74      */
 75     public void insert(Node node,int position){
 76         checkPosition(position);
 77         Node currentNode = null;
 78         while(point!=position-1){
 79             if(point == 0){
 80                 currentNode = head.next; 
 81             }
 82             else{
 83                 currentNode = currentNode.next;
 84             }
 85             point++;
 86         }
 87         if(currentNode!=null){
 88             node.next = currentNode.next;
 89             currentNode.next = node;
 90         }
 91         else{
 92             head.next = node;
 93         }
 94         point = 0;//将指针指向头结点
 95         size++;
 96         LinkedListLog.getInstance().insert();
 97     }
 98     /**
 99      * 删
100      */
101     public void delete(int position){
102         checkPosition(position);
103         Node currentNode = null;
104         while(point!=position-1){
105             if(point == 0){
106                 currentNode = head.next; 
107             }
108             else{
109                 currentNode = currentNode.next;
110             }
111             point++;
112         }
113         if(size == 1){
114             currentNode.next = null;
115         }
116         else{
117             currentNode.next = currentNode.next.next;
118         }    
119         point = 0;//将指针指向头结点
120         size--;
121         LinkedListLog.getInstance().delete();
122     }
123     /**
124      * 改
125      */
126     public void update(Node node,int position){
127         checkPosition(position);
128         Node currentNode = null;
129         while(point!=position-1){
130             if(point == 0){
131                 currentNode = head.next; 
132             }
133             else{
134                 currentNode = currentNode.next;
135             }
136             point++;
137         }
138         if(size == 1){
139             currentNode.next = node;
140         }
141         else{
142             node.next = currentNode.next.next;
143             currentNode.next = node;
144         }
145         point = 0;//将指针指向头结点
146         LinkedListLog.getInstance().update();
147     }
148     /**
149      * 查
150      */
151     public Node select(int position){
152         checkPosition(position);
153         Node currentNode = null;
154         while(point!=position-1){
155             if(point == 0){
156                 currentNode = head.next; 
157             }
158             else{
159                 currentNode = currentNode.next;
160             }
161             point++;
162         }
163         point = 0;
164         return currentNode.next;
165     }
166     /**
167      * 检查位置是否正确
168      */
169     public void checkPosition(int position){
170         if(position>size+1||position<=0){
171             LinkedListLog.getInstance().error();
172             return;
173         }
174     }
175 }
176 
177 package com.voole.linkedlist;
178 /**
179  * @description 链表节点
180  * @author TMAC-J
181  *
182  */
183 public class Node {
184     /**
185      * 定义指针域
186      */
187     public Node next = null;
188     /**
189      * 定义数据域
190      */
191     public Data data = null;
192     /**
193      * @description 构造方法
194      */
195     public Node(Node next,Data data){
196         this.next = next;
197         this.data = data;
198     }
199 }
200 
201 
202 package com.voole.linkedlist;
203 
204 import java.io.Serializable;
205 
206 public class Data implements Serializable{
207 
208     /**
209      * 
210      */
211     private static final long serialVersionUID = 1L;
212 
213 }
214 
215 
216 package com.voole.linkedlist;
217 /**
218  * 单例日志类(饿汉)
219  * @author TMAC-J
220  *
221  */
222 public class LinkedListLog {
223     private static final LinkedListLog instance = new LinkedListLog();
224     
225     private LinkedListLog(){}
226     
227     public static LinkedListLog getInstance(){
228         return instance;
229     }
230     
231     public void insert(){
232         System.out.println("插入成功!");
233     }
234     
235     public void delete(){
236         System.out.println("删除成功!");
237     }
238     
239     public void update(){
240         System.out.println("修改成功!");
241     }
242     
243     public void select(){
244         System.out.println("查询成功!");
245     }
246     
247     public void error(){
248         System.out.println("错误!");
249     }
250 }


插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
修改成功!
com.voole.linkedlist.Node@7cb8f891
com.voole.linkedlist.Node@7cb8f891

 

以上就是java代码实现单链表的过程,注意,单链表的效率其实是很低的,读者看代码也可以知道,每一次操作都需要从表头开始遍历节点,更高的效率可以在此基础上改一部分,改成双链表,这样就可以从两头查询,并且可以进一步优化,让指针不用每一次都操作完都置为0,很多方式都可以提升效率,并且可以添加同步的方法提升安全性,在此,我就不去完善了,这里只是一个最基础功能的链表。



posted @ 2016-11-23 14:51  麦子TMAC  阅读(259)  评论(0编辑  收藏  举报