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 自定义双向链表
最新推荐文章于 2024-03-12 04:22:01 发布