从java 编程思想中阅读到, 作一下笔记
- /**
- * @author zhouming
- * @date 2018\5\26 0026 12:39
- *
- */
- public class MyLinkedList<T> {
- /**
- * 链式存储类
- * @param <U>
- */
- private static class Node<U>{
- // 当前这个节点的值
- private U nowData;
- // 下一个链式节点
- private Node<U> next;
- // 使用无惨构造初始化当前值与下一个节点
- public Node() {
- nowData = null;
- next = null;
- }
- // 使用有参构造来设置值
- public Node(U u, Node<U> next) {
- this.nowData = u;
- this.next = next;
- }
- // 判断是否还有下一个值
- public boolean end(){
- return nowData == null && next == null;
- }
- }
- // 初始化链头
- private Node<T> node = new Node<>();
- public void add(T t){
- // 将旧的 node 存入到新的 node 中并将新的 node 返回,
- // 下次再存入值时又将这个新的 node 存入即将生成新的 node 中
- // 再次返回一个新的 node
- node = new Node<T>(t,node);
- }
- // 取值
- public T get(){
- // 获取到当前值
- T value = node.nowData;
- // 先进行判断是否容纳了下一个, 如果有则将 top 的引用指向其本身所容纳的下一个 Node
- if(!node.end()){
- node = node.next;
- }
- return value;
- }
- public static void main(String[] args) {
- MyLinkedList<String> list = new MyLinkedList<>();
- list.add("111");
- list.add("222");
- list.add("333");
- String s = null;
- while((s = list.get()) !=null){
- System.out.println(s);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2618150.html