手写一个阉割版的链表

2022-10-27 16:05:39 浏览数 (1)

不多BB 上代码。

代码语言:javascript复制
package com.jmy.list;

public class MyLinkedList {
    /*
    链表由一个又一个节点连接而成节点分为prev data next
    链表只需要保存头节点和尾节点即可
     */
    public Node first;
    public Node last;
    int size = ;
    class Node{
        Node prev;
        String data;
        Node next;

        public Node(Node prev, String data, Node next) {
            this.prev = prev;
            this.data = data;
            this.next = next;
        }
    }

    /**
     *
     * 尾插法向链表中添加元素
     * @param str 添加的元素
     */
    public void add(String str){
        Node node = new Node(null, str, null);

        if (isNull(str)) {
            throw new IllegalArgumentException("兄弟醒醒!");
        }
        if (size == ) {
            this.first = this.last = node;
        } else {
            node.prev = this.last;
            this.last.next = node;
            this.last = node;
        }
        size  ;
    }

    /**
     * 向链表的任意位置添加指定元素
     * @param str 被添加元素
     * @param index 添加位置
     */
    public void add(String str, int index) {
        if (isNull(str) || argument(index)) {
            throw new IllegalArgumentException("兄弟醒醒!");
        }
        Node node = new Node(null, str, null);
        Node no = this.first;
        if (index == ) {
            this.first.prev = node;
            node.next = this.first;
            this.first = node;
        } else if (index >  && index < size) {
            for (int i = ; i < index; i  ) {
                no = no.next;
            }
            node.prev = no.prev;
            node.next = no;
            no.prev.next = node;
            no.prev = node;
        }else {
            this.add(str);
        }

        size  ;
    }

    /**
     * 指定下标元素
     * @param index
     */
    public void remove(int index){

        if (argument(index)) {
            throw new IllegalArgumentException("兄弟 醒醒!!");
        }

        Node node = this.first;
        for (int i = ; i < index; i  ) {
            node = node.next;
        }

        node.prev.next= node.next;

        size--;
    }

    /**
     * 删除指定元素
     * @param str
     */
    public void remove(String str){
        if (isNull(str)) {
            throw new IllegalArgumentException("兄弟醒醒!");
        }

        Node node = this.first;
        for (int i = ; i < size; i  ) {
            if (node.data.equals("str")) {
                this.remove(i);
                size--;
                break;
            }

            node = node.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node node = this.first;
        sb.append("[");
        for (int i = ; i < size; i  ) {
            if (i < size - ) {
                sb.append(node.data   ",");
            } else {
                sb.append(node.data);
            }
            node = node.next;
        }
        sb.append("]");

        return sb.toString();
    }

    /*
       判断传入的元素是否为空
        */
    private boolean isNull(String str){
        return str == null ? true : false;
    }
    /*
    判断下标是否符合逻辑
     */
    private boolean argument(int index){
        return index <  || index > size ? true : false;
    }

    public static void main(String[] args) {
        MyLinkedList mll = new MyLinkedList();

        mll.add("张三");
        mll.add("李四");
        mll.add("王五");
        mll.add("赵六");
        mll.add("一",);
        mll.remove();
        System.out.println(mll);
    }
}

0 人点赞