无头双向链表的相关操作(Java实现)

首先介绍一下双向链表:

双向链表也叫双链表,是链表的一种,它的每个Node结点都包含3个信息,分别是:数据域、前驱以及后继。与单链表的不同就是,它多了一个描述当前结点前驱的属性。

下面主要说的是不带头结点的,假设有一组数据(10, 20, 18, 46, 2),它们以双向链表的形式存储 ,那么就是下面这个样子:

(链表中的第一个结点默认是头结点,所以head指向第一个结点;链表的最后一个结点默认是尾结点,所以last指向的总是链表的最后一个结点。)

接下来如果要实现一个双链表,该如何实现呢?

首先,双链表是由一个个的结点构成的,我们可以先构建一个结点类,用来描述每个结点的信息。从图中,每个节点类都有(data, next, prev),所以就在Node中定义这三个属性。同时在构造方法中对产生的对象进行初始化。

class Node{
    private int data;
    Node next = null;
    Node prev = null;
    public Node(int data){
        this.data = data;
    }
}

在有了Node结点之后,就是考虑该如何描述整个双向链表?

我们只需想到,如果要知道一个双向链表,只需要知道它的头结点就可以,所以在描述双链表的类中,有一个属性是头结点,除此之外,因为是双向链表,我们还可以定义一个属性是链表的尾结点。

那么这个双链表类和Node类该怎样联系呢?

这时候可以用到Java中的内部类,让DoubleLinkedList作为外部类,让Node类作为DoubleLinkedList类的成员内部类。

知识点:

  • 成员内部类相当于外部类的成员变量,内部类可以直接访问外部类的元素(包括私有域)),但外部类只能通过内部类的引用间接访问内部类元素。
  • 对于非静态内部类,内部类的创建依赖外部类的实例对象,在没有外部类实例之前是无法创建内部类的。也就是说,外界(其他类)是不知道外部类中内部类的存在性。

 DoubleLinkedList类如下:

public class DoubleLinkedList{
    class Node{
        ...
    }
    //DoubleLinkedList对象可以通过head和last来访问内部类的元素
    private Node head;
    private Node last;
    public DoubleLinkedList(){
        this.head = null;
    }
}

这样大致结构就构造好了,下面就是一下链表常见的操作:增删查改之类的 . . .

只需要按照上面的图,修改引用就可以了,只需要注意:当方法中涉及到多个Node结点的时候,只需要让每个Node结点的prev和next都指向正确的地方就可以。

package singleList;
//无头双向链表
public class DoubleLinkedList {

    class Node{
        private int data;
        private Node next;
        private Node prev;
        public Node(int data){
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }

    private Node head;
    private Node last;
    public DoubleLinkedList(){
        this.head = null;
    }

    //1.头插,head变,last不变
    public void addFirst(int data){
        Node node = new Node(data);
        if(head == null){
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }
    //2.尾插,头不变,last变
    public void addLast(int data){
        Node node = new Node(data);
        if(this.last == null){
            this.last = node;
            this.head = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }
    //3.获取链表长度
    public int getLength(){
        Node cur = this.head;
        int len = 0;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }
    //4.在指定索引位置插入数据,只能在[0,lenth]区间插入
    //如要在: 1->2->3->4->null 的index=2位置插入data=5,则为:1->2->5->3->4->null
    public boolean addIndex(int index, int data){
        if(index < 0 || index > this.getLength()){
            System.out.println("插入位置不合法!");
            return false;
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == this.getLength()) {
            addLast(data);
            return true;
        }
        Node node = new Node(data);
        Node cur = this.head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.prev.next = node;
        node.next = cur;
        node.prev = cur.prev;
        cur.prev = node;
        return true;
    }
    //5.是否包含key
    public boolean contains(int key){
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //6.移除链表中等于key的第一个元素节点
    public boolean remove(int key){
        if(this.head == null){
            return false;
        }
        // 1->2->4
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key){
                if(cur == this.head){
                    //是头节点
                    if(cur.next == null){
                        //只有一个元素
                        this.head = this.last = null;
                    }else {
                        cur.next.prev = null;
                        this.head = cur.next;
                    }
                    return true;
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        //是尾节点
                        this.last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                        return true;
                    }
                }
                return true;
            }
            cur = cur.next;

        }
        return false;
    }
    //7.移除链表中所有等于key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            return ;
        }
        Node cur = this.head;
        while (cur != null){
            if(cur.data == key){
                //移除头节点
                if(cur == this.head){
                    if(cur.next == null){
                        //只有一个元素
                        this.head = this.last = null;
                        return ;
                    }else {
                        cur.next.prev = null;
                        this.head = cur.next;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        this.last = cur.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //8.清除链表
    public void clear(){
        while(this.head != null){
            Node cur = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = cur;
        }
        this.last = null;
    }
    //9.打印双向链表
    public void display(){
        if(this.head == null){
            System.out.println("链表为空!");
        }else {
            Node cur = this.head;
            while(cur != null){
                System.out.print(cur.data);
                System.out.print("->");
                cur = cur.next;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        DoubleLinkedList linkedList = new DoubleLinkedList();

        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addLast(5);
        linkedList.addLast(5);
        linkedList.addLast(6);
        linkedList.display();
        linkedList.clear();
    }
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值