java实现双向链表(带哑结点)

双向链表双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。
在这里插入图片描述

package cn.tnt.test;

/**
 * @Author TNT-df
 * @Date 2021/1/10 15:05
 * @Description
 */
public class DoubleLinedList {
     
    Node head = new Node();
    Node tail = new Node();

    public DoubleLinedList() {
     

    }
//头插
    public void addFirst(int val) {
     
        Node node = new Node(val);
        if (head.next == null) {
     
            node.next = tail;
            tail.prev = node;
        } else {
     
            node.next = head.next;
            head.next.prev = node;
        }
        head.next = node;
        node.prev = head;
    }
//尾插
    public void addLast(int val) {
     
        Node node = new Node(val);
        if (head.next == null) {
     
            node.next = tail;
            tail.prev = node;
            head.next = node;
            node.prev = head;
        } else {
     
            node.next = tail;
            node.prev = tail.prev;
            tail.prev.next = node;
            tail.prev = node;
        }
    }
//在指定下标处插入
    public void addIdx(int index, int key) {
     
        if (contians(key) && (index > 0 && index < getLength())) {
     
            Node node = new Node(key);
            Node cur = head.next;
            for (int i = 1; i <= index - 1; i++) {
     
                cur = cur.next;
            }
            node.next = cur.next;
            cur.next.prev = node;
            cur.next = node;
            node.prev = cur;
        }
    }

//是否包含当前值
    public boolean contians(int key) {
     
        Node tep = head.next;
        while (tep != tail) {
     
            if (tep.val == key)
                return true;
            tep = tep.next;
        }
        return false;
    }
//移除第一个包含key的节点
    public void remove(int key) {
     
        if (contians(key)) {
     
            Node cur = head.next;
            while (cur != tail) {
     
                if (cur.val == key) {
     
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                } else {
     
                    cur = cur.next;
                }
            }
        }
    }
//移除所有包含key的节点
    public void removeALLKey(int key) {
     
        if (contians(key)) {
     
            Node cur = head.next;
            while (cur != tail) {
     
                if (cur.val == key) {
     
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                cur = cur.next;
            }
        } else {
     
            return;
        }
    }
//得到链表中的结点个数
    public int getLength() {
     
        int len = 0;
        Node cur = head.next;
        while (cur != tail) {
     
            len++;
            cur = cur.next;
        }
        return len;
    }
//打印链表
    public void dispaly() {
     
        Node cur = head.next;
        while (cur != tail) {
     
            System.out.print(cur + " ");
            cur = cur.next;
        }
        System.out.println();
    }
//清空链表
    public void clear() {
     
        Node cur = head.next;
        while (cur != tail) {
     
            Node next = cur.next;
            cur.next = null;
            cur.prev = null;
        }
        head.next = null;
        head = null;
        tail.prev = null;
        tail = null;
    }

    public static void main(String[] args) {
     
        DoubleLinedList list = new DoubleLinedList();
        list.addFirst(1);
        list.addFirst(3);
        list.addFirst(2);
        list.addFirst(4);
        list.dispaly();
        list.addLast(5);
        list.addLast(3);
        list.addLast(15);
        list.addLast(15);
        list.dispaly();
        System.out.println(list.contians(3));
        list.remove(3);
        list.dispaly();
        list.removeALLKey(3);
        list.dispaly();
        System.out.println(list.getLength());
    }
}

class Node {
     
    int val;
    Node next;

    public Node() {
     
    }

    Node prev;

    public Node(int val) {
     
        this.val = val;
    }


    @Override
    public String toString() {
     
        return "Node{" +
                "val=" + val +
                '}';
    }
}

java实现双向链表(带哑结点)_第1张图片

你可能感兴趣的:(java)