其他分享
首页 > 其他分享> > 手写链表

手写链表

作者:互联网

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)

标签:myLink,node,java,链表,MyNode,firstNode,手写,public
来源: https://www.cnblogs.com/silyvin/p/11739405.html