java实现简单LinkedList

 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 }

 

转载于:https://www.cnblogs.com/Ouyangan/p/4119829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值