首页 > 编程语言 > 详细

java实现简单LinkedList

时间:2014-11-24 23:59:43      阅读:547      评论:0      收藏:0      [点我收藏+]
 1 package LinkedListClass;
 2 
 3 public class Node {
 4     Node parents;
 5     Object myself;
 6     Node child;
 7 
 8     public Node() {
 9 
10     }
11 
12     public Node(Node parents, Object myself, Node child) {
13         super();
14         this.parents = parents;
15         this.myself = myself;
16         this.child = child;
17     }
18 
19     public Node getParents() {
20         return parents;
21     }
22 
23     public void setParents(Node parents) {
24         this.parents = parents;
25     }
26 
27     public Object getMyself() {
28         return myself;
29     }
30 
31     public void setMyself(Object myself) {
32         this.myself = myself;
33     }
34 
35     public Node getChild() {
36         return child;
37     }
38 
39     public void setChild(Node child) {
40         this.child = child;
41     }
42 
43 }
44 
45 public class MyLinkedList {
46     private Node first;
47     private Node last;
48     private int size;
49 
50     public void add(Object obj) {
51         Node n = new Node();
52         if (first == null) {
53             n.setChild(null);
54             n.setMyself(obj);
55             n.setParents(null);
56             first = n;
57             last = n;
58         } else {
59             n.setParents(last);
60             n.setMyself(obj);
61             n.setChild(null);
62 
63             last.setChild(n);
64             last = n;
65         }
66         size++;
67     }
68 
69     public int size() {
70         return size;
71     }
72 
73     public Object get(int index) {
74         Node temp = null;
75         if (first != null) {
76             temp = first;
77             for (int i = 0; i < index; i++) {
78                 temp = temp.child;
79             }
80         }
81         return temp.myself;
82     }
83 
84     public static void main(String[] args) {
85         MyLinkedList list = new MyLinkedList();
86         list.add("aaa");
87         list.add("bbb");
88         list.add("ccc");
89         list.add("ddd");
90         System.out.println(list.get(3));
91         System.out.println(list.size());
92     }
93 }

 

java实现简单LinkedList

原文:http://www.cnblogs.com/Ouyangan/p/4119829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!