Java实现双向链表

该博客详细介绍了双链表的数据结构,包括节点定义、链表初始化、添加节点(头尾插入、指定位置插入)、查找节点、删除节点以及判断是否包含特定节点等基本操作。此外,还提供了清空链表和显示链表内容的方法,适用于理解和操作双链表的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//节点
class ListNode {
    public int data;//数据
    public ListNode prev;//指向上一个节点
    public ListNode next;//指向下一个节点

    public ListNode(int data) {
        this.data = data;
    }
}
//双链表
public class DoubleLinkedList {
    public ListNode dummyHead;//头指针
    public ListNode last;//尾指针
    
    public DoubleLinkedList() {
        this.dummyHead = new ListNode(-1);
    }
    //返回index位置的节点
    public ListNode findIndex(int index) {
        ListNode cur = dummyHead.next;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //向链表头添加节点
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        if(this.dummyHead.next==null) {
            node.prev=this.dummyHead;
            this.dummyHead.next=node;
            this.last=node;
            return;
        }else {
            node.next=this.dummyHead.next;
            node.prev=this.dummyHead;
            this.dummyHead.next.prev=node;
            this.dummyHead.next=node;
        }
    }
    //向链表尾添加节点
    public void addLast(int data) {
        ListNode node=new ListNode(data);
        if(this.dummyHead.next==null&&this.last==null) {
            node.prev=this.dummyHead;
            this.dummyHead.next=node;
            this.last=node;
            return;
        }else {
            this.last.next=node;
            node.prev=this.last;
            this.last=node;
        }
    }
    //向链表index位置添加节点
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("输入的位置不合法");
            return;
        } else {
            if (index == 0) {
                addFirst(data);
                return;
            }
            if (index == size()) {
                addLast(data);
            }else {
                ListNode node = new ListNode(data);
                ListNode cur;
                cur = findIndex(index);
                cur.prev.next = node;
                node.prev = cur.prev;
                cur.prev = node;
                node.next = cur;
            }
        }
    }
    //判断链表中是否有值为key的节点
    public boolean contains(int key) {
        ListNode cur = this.dummyHead.next;
        while (cur != null) {
            if (cur.data == key) return true;
            cur=cur.next;
        }
        return false;
    }
    //删除链表中首个值为key的节点
    public void remove(int key) {
        ListNode cur = this.dummyHead.next;

        while(cur != null) {
            if(cur.data == key) {
                if(cur == this.dummyHead.next) {
                    this.dummyHead.next = this.dummyHead.next.next; 
                    this.dummyHead.next.prev = this.dummyHead;
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.last = cur.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }
    //删除链表中所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = dummyHead.next;
        while (cur != null) {
            //
            if (cur.data == key) {
                if (cur == this.dummyHead.next) {
                    this.dummyHead.next=this.dummyHead.next.next;
                    this.dummyHead.next.prev=null;

                } else {
                    cur.prev.next = cur.next;
                    //判断是不是最后一个节点
                    if (cur.next == null) {
                        this.last = cur.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //返回链表中节点个数
    public int size() {
        ListNode cur =this.dummyHead.next;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
	//显示链表中所有的数据
    public void display() {

        ListNode cur = this.dummyHead.next;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;

        }
        System.out.println();

    }
	//清空链表
    public void clear() {
        ListNode cur =  this.dummyHead.next;
        ListNode curNext;
        while (cur != null) {
            curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.dummyHead = null;
        this.last = null;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值