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 +
                '}';
    }
}

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值