• 手写链表


    package Method;
    
    import org.omg.Messaging.SYNC_WITH_TRANSPORT;
    import serial.MyBaseBean;
    
    /**
     * Created by joyce on 2019/10/25.
     */
    public class MyLink<T> {
    
        private MyNode<T> firstNode;
        private MyNode<T> lastNode;
        private Integer length = 0;
    
        public String toString() {
            MyNode<T> node = firstNode;
            if(node == null)
                System.out.println(node);
            while (node != null) {
                System.out.println(node.getT());
                node = node.nextNode;
            }
            return null;
        }
    
        public T get(int index) {
            if(index >= length)
                throw new RuntimeException();
    
            MyNode<T> node = firstNode;
            for(int j=0; j<index; ++j) {
                node = node.nextNode;
            }
    
            return node.getT();
        }
    
        public static void main(String [] f) {
            MyLink<Integer> myLink = new MyLink<>();
            myLink.add(1);
            myLink.add(2);
            myLink.add(3);
            myLink.add(4);
            myLink.toString();
            // 删尾
            myLink.remove(3);
            myLink.toString();
            // 删中间
            myLink.remove(1);
            myLink.toString();
            // 删头节点
            myLink.remove(0);
            myLink.toString();
            // 删唯一节点
            myLink.remove(0);
            // 没节点
            myLink.toString();
            myLink.remove(0);
        }
    
        public void add(T t) {
            MyNode<T> node = new MyNode<T>(t);
            if(length == 0) {
                firstNode = lastNode = node;
            } else {
                lastNode.setNextNode(node);
                lastNode = node;
            }
            ++length;
        }
    
        public void remove(int index) {
    
            if(index >= length)
                throw new RuntimeException();
    
            if(length == 1) {
                firstNode = null;
                lastNode = null;
            } else if(index == 0) {
                firstNode = firstNode.nextNode;
            } else if(length - 1 == index) {
                MyNode<T> node = firstNode;
                for(int j=0; j<index -1; ++j) {
                    node = node.nextNode;
                }
                lastNode = node;
                node.nextNode = null;
            } else {
                MyNode<T> node = firstNode;
                for(int j=0; j<index -1; ++j) {
                    node = node.nextNode;
                }
                node.nextNode = node.nextNode.nextNode;
    
            }
    
            --length;
        }
    
    
    
        private static class MyNode<T> {
            private T t;
            private MyNode<T> nextNode;
            public MyNode(T t) {
                this.t = t;
            }
    
            public T getT() {
                return t;
            }
    
            public void setT(T t) {
                this.t = t;
            }
    
            public MyNode<T> getNextNode() {
                return nextNode;
            }
    
            public void setNextNode(MyNode<T> nextNode) {
                this.nextNode = nextNode;
            }
        }
    }
    

    输出:

    1
    2
    3
    4
    1
    2
    3
    1
    3
    3
    null
    java.lang.RuntimeException
    at Method.MyLink.remove(MyLink.java:75)
    at Method.MyLink.main(MyLink.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

  • 相关阅读:
    常用正则表达式应用
    iOS 通知推送APNS
    IOS生活圈开发练习记录
    [转]定时器
    JavaScript如何在webView跳转到指点的位置
    用imageNamed加载图片产生的问题
    如何再分类中添加属性
    CADisplayLink定时器
    自定义UIBtton类后,在@implementation重写方法的作用
    CAAnimationGroup(组合动画)创建
  • 原文地址:https://www.cnblogs.com/silyvin/p/11739405.html
Copyright © 2020-2023  润新知