Java 自定义双向链表

package com.example.mydemo.myapi;


/**
 * @Author zmz
 * @Class_Name:LinkNode
 * @Create_Date:2019/6/26
 * @Des: 自定义双向链表结构
 */
public class LinkdNode {
    //根节点
    private Node mNode;
    //链表大小
    private int mSize = 0;

    /**
     * 添加数据
     *
     * @param data
     */
    public void addNode(String data) {
        Node node = new Node(data);
        //根节点如果为null那么就将新增的节点添加到根节点中
        if (null == mNode) {
            node.mPrevious = null;
            mNode = node;
        } else {             //如果存在根节点,那么进行递归调用类中的方法进行添加节点
            mNode.addNode(node);
        }
        mSize++;
    }

    /**
     * 头部添加节点
     *
     * @param data
     */
    public void addHeadNode(String data) {
        //创建新节点
        Node newNode = new Node(data);
        //如果根节点为空,就把新创建的节点赋值给根节点
        if (mNode == null) {
            mNode = newNode;
            mNode.mPrevious = null;
        } else {//根节点不为空,则在根节点的左边添加节点
            //根节点赋值给新的对象
            Node oldNode = mNode;
            //根节点重新赋值
            mNode = newNode;
            //新节点的Next指向以前的根节点
            mNode.mNext = oldNode;
            oldNode.mPrevious = mNode;

        }
        mSize++;
    }

    /**
     * 在指定位置添加节点
     *
     * @param index
     * @param data
     */
    public void addIndexNode(int index, String data) {
        if (index >= mSize) {
            throw new RuntimeException("插入越界异常");
        }
        //头部插入节点
        if (0 == index) {
            addHeadNode(data);
            return;
        }
        //尾部插入数据
        if (index == mSize - 1) {
            addNode(data);
            return;
        }
        //创建新节点
        Node newNode = new Node(data);
        Node currentNode = mNode;

        int i = 0;
        while (currentNode != null) {
            if (i == index - 1) {
                //将要插入位置的节点赋值给另一个对象
                Node nextNode = currentNode.mNext;
                //插入节点
                currentNode.mNext = newNode;
                //重新关联下一个节点
                newNode.mNext = nextNode;
                break;
            }
            currentNode = currentNode.mNext;
            i++;
        }
        mSize++;
    }

    /**
     * 根据下边替换节点
     *
     * @param index
     * @param data
     */
    public void replaceNode(int index, String data) {

        if (index >= mSize) {
            throw new RuntimeException("下标越界异常");
        }
        //创建新节点
        Node newNode = new Node(data);
        Node currentNode = mNode;
        int i = 0;
        while (currentNode != null) {
            if (i == index - 1) {
                //将要插入位置的节点赋值给另一个对象
                Node nextNode = currentNode.mNext.mNext;
                nextNode.mPrevious = currentNode.mPrevious;
                //插入节点
                currentNode.mNext = null;
                currentNode.mNext = newNode;
                //重新关联下一个节点
                newNode.mNext = nextNode;
                newNode.mPrevious = null;
                newNode.mPrevious = nextNode.mPrevious;
                break;
            }
            currentNode = currentNode.mNext;
            i++;
        }
        System.gc();
    }

    /**
     * 根据下标删除节点
     *
     * @param index
     */
    public void deleteNode(int index) {

        if (index >= mSize) {
            throw new RuntimeException("下标越界异常");
        }
        //创建新节点
        Node currentNode = mNode;
        int i = 0;
        while (currentNode != null) {
            if (i == index - 1) {
                //重新关联节点
                Node nextNode = currentNode.mNext.mNext;
                currentNode.mNext = null;
                currentNode.mNext = nextNode;
                currentNode.mPrevious = null;
                currentNode.mPrevious = nextNode.mPrevious;
                break;
            }
            currentNode = currentNode.mNext;
            i++;
        }
        System.gc();
    }

    /**
     * 删除一个节点
     *
     * @param data
     */
    public void deleteNode(String data) {
        //如果包含这个节点在进行删除操作
        if (contains(data)) {
            //根节点是这个节点--改变根节点的引用
            if (mNode.mData.equals(data)) {
                Node currentNode = mNode.mNext;
                mNode = currentNode.mNext;
                mNode.mPrevious = currentNode.mPrevious;
            } else {            //不是根节点--递归的查找 改变引用
                mNode.deleteNode(data);
            }
            mSize--;
        }
    }

    /**
     * 根据下标获取节点
     *
     * @param index
     * @return
     */
    public String getNode(int index) {
        if (index >= mSize) {
            throw new RuntimeException("下标越界异常");
        }
        int i = 0;
        while (mNode != null) {
            if (i == index - 1) {
                return mNode.mData;
            }
            i++;
        }
        return "";
    }

    /**
     * 是否包含指定节点数据
     *
     * @param data
     * @return
     */
    public boolean contains(String data) {
        //根节点是该节点 直接返回
        if (mNode.mData.equals(data)) {
            return true;
        } else {            //如果根节点不是,那么继续向下找!
            return mNode.constains(data);
        }
    }

    /**
     * 获取链表大小
     *
     * @return
     */
    public int getSize() {
        return mSize;
    }

    /**
     * 打印所有节点数据
     */
    public void printNode() {
        if (mNode != null) {
            mNode.printNode();
        }
    }

    /**
     * 链表节点类
     */
    public static class Node {
        //上一个节点引用
        private Node mPrevious;
        //当前节点数据
        private String mData;
        //下一个节点的引用
        private Node mNext;

        public Node(String data) {
            mData = data;
        }

        /**
         * 添加一个节点
         *
         * @param node
         */
        public void addNode(Node node) {
            //如果下个节点没有被填充,那么就填充到这里
            if (null == mNext) {
                mNext = node;
                mNext.mPrevious = this;
            } else {
                mNext.addNode(node);
                return;
            }
        }

        /**
         * 是否包含指定节点
         *
         * @param data
         * @return
         */
        public boolean constains(String data) {
            if (null == mNext) {
                return false;
            } else {
                if (data.equals(mNext.mData)) {
                    return true;
                } else {
                    return mNext.constains(data);
                }
            }
        }

        /**
         * 删除一个节点
         *
         * @param data
         */
        public void deleteNode(String data) {
            if (data.equals(mNext.mData)) {
                mNext = mNext.mNext;
            } else {
                mNext.deleteNode(data);
            }
            System.gc();
        }

        /**
         * 打印链表所有节点数据
         */
        public void printNode() {
            System.out.print("data:  " + mData + "   ");
            if (null != mNext) {
                mNext.printNode();
            }
        }

    }
}
Java中,我们可以自定义双向链表来实现一些特定的需求。双向链表是一种数据结构,它的每个节点都包含一个指向前一个节点和后一个节点的指针。这使得在双向链表中进行插入和删除操作非常高效,因为只需要修改前后节点的指针就可以完成。另外,访问节点可能会比较慢,因为需要从第一个节点开始遍历链表。但是,Java的LinkedList类提供了丰富的方法,可以模拟链式队列、链式堆栈等数据结构,为用户提供了极大的方便。 下面是一个简单的自定义双向链表Java代码示例: ```java // 定义节点类 class HeroNode2 { public int no; public String name; public String nickname; public HeroNode2 next; // 指向下一个节点,默认为null public HeroNode2 pre; // 指向上一个节点,默认为null // 构造器 public HeroNode2(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; } @Override public String toString() { return "HeroNode2{" + "no=" + no + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + '}'; } } // 自定义双向链表类 class DoubleLinkedList { private HeroNode2 head; // 头节点 // 构造器 public DoubleLinkedList() { head = new HeroNode2(0, "", ""); } // 在链表尾部添加节点 public void add(HeroNode2 node) { HeroNode2 temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node; node.pre = temp; } // 遍历链表 public void display() { HeroNode2 temp = head.next; while (temp != null) { System.out.println(temp); temp = temp.next; } } // 在某个节点后面插入新节点 public void insertAfter(HeroNode2 newNode, HeroNode2 afterNode) { HeroNode2 temp = head; while (temp != null) { if (temp == afterNode) { newNode.next = temp.next; if (temp.next != null) { temp.next.pre = newNode; } newNode.pre = temp; temp.next = newNode; break; } temp = temp.next; } } // 删除某个节点 public void delete(HeroNode2 node) { HeroNode2 temp = head; while (temp != null) { if (temp == node) { temp.pre.next = temp.next; if (temp.next != null) { temp.next.pre = temp.pre; } break; } temp = temp.next; } } } ``` 以上是一个简单的自定义双向链表的实现。你可以根据需要添加其他方法或功能来满足具体需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java双向链表的实现](https://download.csdn.net/download/weixin_38669628/11056304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Java 自定义双向链表](https://blog.csdn.net/ana35287/article/details/102111857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java实现双向链表](https://blog.csdn.net/m0_63732435/article/details/127195219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值