单链表的基本操作

 1 public class Node{
 2     public long data;
 3     public Node next;
 4     
 5     public Node(long value){
 6         this.data = value;
 7     }
 8     
 9     public void display(){
10         System.out.println(data+"");
11     }
12 }
13 //链表操作
14 public class LinkList{
15     private Node first;
16     public LinkList(){
17         first = null;
18     }
19     public void insertFirst(long value){
20         Node node = new Node(value);
21         if(first == null){
22             first=node;
23         }
24         node.next = first.next;
25         first = node;
26     }
27     public Node deleteFirst(){
28         Node tmp = first;
29         first = tmp.next;
30         return tmp;
31     }
32     public void show(){
33         Node current = first;
34         while(current!= null){
35             current.display();
36             current = current.next;
37         }
38     }
39     public Node find(long value){
40         Node current = first;
41         while(current.data!=value){
42             if(current.next==null){
43                 return null;
44             }
45             current=current.next;
46         }
47         return current;
48     }
49     public Node delete(long value){
50         Node current = first;
51         Node previous = first;
52         while(current.data!=value){
53             if(current.next==null){
54                 return null;
55             }
56             previos = current;
57             current = current.next;
58         }
59         if(current == first){
60             first = first.next;
61         }else{
62             previous.next = current.next;
63         }
64         return current;
65     }
66 }

 

posted @ 2015-06-02 19:32  疯狂的蜗牛仔  阅读(165)  评论(0编辑  收藏  举报