第一版
package com.zzp.demo.myCollection; public class Node { Node previous; //上一节点 Node next; //下一节点 Object element; //数据 public Node(Object element) { super(); this.element = element; } public Node(Node previous, Node next, Object element) { super(); this.previous = previous; this.next = next; this.element = element; } }
package com.zzp.demo.myCollection; /** * * 自定义链表 * @author java * */ public class LinkedList01 { private Node first; private Node last; private int size; //添加元素 public void add(Object obj){ Node node = new Node(obj); if(first == null){ first = node; last = node; }else{ node.previous = last; node.next = null; last.next = node; last = node; } } //打印字符串 @Override public String toString() { StringBuilder sb = new StringBuilder("["); Node temp = first; while(temp != null){ sb.append(temp.element + ","); temp = temp.next; } sb.setCharAt(sb.length() - 1, ‘]‘); return sb.toString(); } public static void main(String[] args) { LinkedList01 ls = new LinkedList01(); ls.add("sa"); ls.add("sdsf"); ls.add("kajs"); System.out.println(ls.toString()); } }
原文:https://www.cnblogs.com/zhangzhipeng001/p/9508840.html