数据结构---链表(二)【不带头双向非循环】-双向链表的模拟实现

时间:2024-10-28 09:01:26
import linkedlistdemo.MySingleList;

import java.util.List;

public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    //打印链表
    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val +" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //查找是否包含key
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //计算链表长度size
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //任意位置插入,第一个结点为0号下标
    public void addIndex(int index,int data)  {
        if(index < 0 || index > size()) {
            return;
        }
        if(index == 0) {
            addFirst(data);
        }
        if (index == size()) {
            addLast(data);
        }
        ListNode node = new ListNode(data);
        ListNode cur = searchNode(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode searchNode(int index) {
        ListNode cur = head;
        int count = 0;
        while(count != index) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //删除val值为key的结点
    public void remove(int key) {
        if(head == null) return;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    if(cur.next == null) {
                        cur = null;
                    }else {
                        head = head.next;
                        head.prev = null;
                    }
                    return;
                }
                if(cur == last) {
                    cur.prev.next = null;
                    last = cur.prev;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的结点
    public void removeAll(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    if(cur.next == null) {
                        cur = null;
                        return;
                    }else {
                        cur.next.prev = null;
                        head = head.next;
                    }
                }else if(cur == last) {
                    cur.prev.next = null;
                    last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }
    //清除链表
    public void clear() {
        ListNode cur = head;
        ListNode curN = null;
        while(cur != null) {
            curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        head = null;
        last = null;
    }
}