Java数据结构(四):线性表之双向链表

本文详细介绍了使用Java实现双向链表的方法,包括节点结构定义、基本操作如插入、删除及获取元素等。通过具体代码展示了如何维护双向链表的数据结构。

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

java实现简单的双向链表,代码如下:

package datastructure.linear.linked;

import datastructure.exception.StructureException;
import datastructure.linear.AbstractList;

/** * @Description 双向链表实现 * @author mastery * @Date 2015年6月30日下午9:11:51 */
public class BidirectionalLinkedList<T> extends AbstractList<T> {

    class Node<T1> {
        /** * 数据域 */
        T1 element;

        /** * 指向前驱结点的指针 */
        Node<T1> prior;

        /** * 指向后继结点的指针 */
        Node<T1> next;

        public Node(Node<T1> next) {
            super();
            this.prior = next;
            this.next = next;
        }

        public Node(T1 element, Node<T1> prior, Node<T1> next) {
            super();
            this.element = element;
            this.prior = prior;
            this.next = next;
        }

    }

    private Node<T> head;

    private Node<T> currentNode;

    public BidirectionalLinkedList() {
        head = currentNode = new Node<T>(null);
        size = 0;
    }

    /** * 得到当前下标对应的结点 * * @param index * @throws StructureException */
    public void indexNodeToCurrent(int index) throws StructureException {
        currentNode = head;
        if (index < -1 || index > size - 1) {
            throw new StructureException("index参数异常!");
        }
        if (index == -1) {
            return;
        }
        currentNode = head.next;
        int j = 0;
        while (currentNode != null && j < index) {
            currentNode = currentNode.next;
            j++;
        }
    }

    @Override
    public void insert(int index, T t) throws StructureException {
        if (index < 0 || index > size) {
            throw new StructureException("index参数异常!");
        }
        // 得到当前下标的上一个结点
        indexNodeToCurrent(index - 1);
        Node<T> insertNode = new Node<T>(t, currentNode, currentNode.next);
        if (currentNode.next != null) {
            // 将新元素生成结点插入到当前结点下
            currentNode.next.prior = insertNode;
        }
        currentNode.next = insertNode;
        size++;
    }

    @Override
    public void delete(int index) throws StructureException {
        if (isEmpty()) {
            throw new StructureException("链表为空");
        }
        if (index < 0 || index > size) {
            throw new StructureException("index参数异常");
        }
        indexNodeToCurrent(index - 1);
        Node<T> twoNextNode = currentNode.next.next;
        if (twoNextNode != null) {
            twoNextNode.prior = currentNode;
        }
        currentNode.next = twoNextNode;
        size--;
    }

    @Override
    public T get(int index) throws StructureException {
        if (isEmpty()) {
            throw new StructureException("链表为空");
        }
        if (index < 0 || index > size) {
            throw new StructureException("index参数异常!");
        }
        indexNodeToCurrent(index);
        return currentNode.element;
    }

}

转载于:https://my.oschina.net/zouziwen/blog/713295

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值