码迷,mamicode.com
首页 > 其他好文 > 详细

单链表的基本操作

时间:2015-06-02 21:39:15      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

单链表的基本操作

标签:

原文地址:http://www.cnblogs.com/fxust/p/4547492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!