模拟实现LinkedList

LinkedList

LinkedList是一个Java中定义的链表,它是一个双向链表,因为他的每个结点中,可以指向前驱和后继,还具有头节点和尾结点。
如下图:
在这里插入图片描述

模拟实现MyLinkedList

MyListNode

public class MyListNode {
    
    
    public String val;

    public MyListNode prev;

    public MyListNode next;

    public MyListNode(String val) {
    
    
        this.val = val;
        this.prev = null;
        this.next = null;
    }
}

MyIterator

public class MyIterator {
    
    
    public MyListNode cur;

    public MyIterator(MyListNode head) {
    
    
        this.cur = head;
    }

    public boolean hasNext() {
    
    
        return cur != null;
    }

    public String next() {
    
    
        String e = cur.val;
        cur = cur.next;
        return e;
    }
}

MyLinkedList

public class MyLinkedList {
    
    
    public MyListNode head;
    public MyListNode last;
    public int size;

    public MyLinkedList() {
    
    
        head = null;
        last = null;
        size = 0;
    }
    //尾插
    public boolean add(String e) {
    
    
        MyListNode node = new MyListNode(e);
        if (head == null) {
    
    
            head = node;
            last = node;
        } else {
    
    
            last.next = node;
            node.prev = last;
            last = node;
        }
        size++;
        return true;
    }
    //根据下标插入
    public void add(int index, String e) {
    
    
        if (index < 0 || index > size()) {
    
    
            throw new RuntimeException("下标不合法!");
        }
        MyListNode node = new MyListNode(e);
        if (head == null) {
    
    
            head = node;
            last = node;
        } else if (index == 0) {
    
    
            head.prev = node;
            node.next = head;
            head = node;
        } else if (index == size()) {
    
    
            last.next = node;
            node.prev = last;
            last = node;
        } else {
    
    
            MyListNode cur = head;
            for (int i = 0; i < index - 1; i++) {
    
    
                cur = cur.next;
            }
            MyListNode next = cur.next;

            node.prev = cur;
            node.next = next;
            cur.next = node;
            next.prev = node;
        }
        size++;
    }
    //根据下标删除
    public String remove(int index) {
    
    
        if (index < 0 || index >= size()) {
    
    
            throw new RuntimeException("下标不合法!");
        }
        String e;
        if (size() == 1) {
    
    
            e = head.val;
            head = null;
            last = null;
        } else if (index == 0) {
    
    
            e = head.val;
            head = head.next;
            head.prev = null;
        } else if (index == size() - 1) {
    
    
            e = last.val;
            last = last.prev;
            last.next = null;
        } else {
    
    
            MyListNode cur = head;
            for (int i = 0; i < index; i++) {
    
    
                cur = cur.next;
            }
            e = cur.val;
            MyListNode prev = cur.prev;
            MyListNode next = cur.next;
            prev.next = next;
            next.prev = prev;
        }
        size--;
        return e;
    }
    //删除第一个为e的元素
    public boolean remove(String e) {
    
    
        int i = indexOf(e);
        if (i < 0) {
    
    
            return false;
        }
        remove(i);
        return true;

    }
    //拿到下标为index的元素
    public String get(int index) {
    
    
        if (index < 0 || index >= size()) {
    
    
            throw new RuntimeException("下标不合法");
        }
        MyListNode cur = head;
        for (int i = 0; i < index; i++) {
    
    
            cur = cur.next;
        }
        return cur.val;
    }
    //将下标为index的元素替换,并返回
    public String set(int index, String e) {
    
    
        if (index < 0 || index >= size()) {
    
    
            throw new RuntimeException("下标不合法");
        }
        String old;
        MyListNode cur = head;
        for (int i = 0; i < index; i++) {
    
    
            cur = cur.next;
        }
        old = cur.val;
        cur.val = e;
        return old;
    }
    //查找为e的下标
    public int indexOf(String e) {
    
    

        int count = 0;
        for (MyListNode cur = head; cur != null; cur = cur.next) {
    
    

            if (cur.val.equals(e)) {
    
    
                return count;
            }
            count++;
        }
        return -1;
    }
    //从后往前找为e的下标
    public int lastIndexOf(String e) {
    
    
        int count = 0;
        for (MyListNode cur = last; cur != null; cur = cur.prev) {
    
    

            if (cur.val.equals(e)) {
    
    
                return count;
            }
            count--;
        }
        return -1;
    }
    //判断是否存在e
    public boolean contains(String e) {
    
    
        return indexOf(e) >= 0;
    }
    //长度
    public int size() {
    
    
        return size;
    }
    //LinkedList是否为空
    public boolean isEmpty() {
    
    
        return size == 0;
    }
    //请空
    public void clear() {
    
    
        last = null;
        head = null;
        size = 0;
    }

    @Override
    public String toString() {
    
    
        MyListNode cur = head;
        StringBuilder str = new StringBuilder();
        while (cur != null) {
    
    
            str.append(cur.val);
            str.append(" ");
            cur = cur.next;
        }

        return str.toString();
    }

    MyIterator iterator() {
    
    
        return new MyIterator(head);
    }
}

总结:
其实还是真的得多练习,多画图形去理解,这样才能学好,不要偷懒,哈哈哈

猜你喜欢

转载自blog.csdn.net/weixin_52142731/article/details/115036126