package com.zjp.structures; public class LoopDoubleLinkedList<T> { LDNode head;//不带数据的头结点 public LoopDoubleLinkedList() { this.head = new LDNode(); this.head.next = head; this.head.prev = head; } /** * 传入一个数组,转换成循环双链表 * * @param arr */ public LoopDoubleLinkedList(T[] arr) { this(); if (arr != null && arr.length > 0) { LDNode p = new LDNode(arr[0]); head.next = p; head.prev = p; p.prev = head; p.next = head; int i = 1; while (i < arr.length) { p.next = new LDNode(arr[i++], p, head); head.prev = p.next; p = p.next; } } } /** * 判断循环双链表是否为空 * * @return */ public boolean isEmpty() { return head.next == head;//循环双链表的后继节点指向自己说明是空链表 } /** * 循环双向链表的长度 * * @return */ public int length() { int length = 0; LDNode p = head.next; while (p != head) { length++; p = p.next; } return length; } /** * 根据下标获取元素值 * * @param index * @return */ public T get(int index) { if (index >= 0) { int j = 0; LDNode p = head.next; //寻找索引所在的节点 while (p != head && j < index) { j++; p = p.next; } if (p != head) { return p.data; } } return null; } /** * 根据index修改data * * @param index * @param data * @return */ public T set(int index, T data) { if (data == null) { return null; } if (index >= 0) { //寻找索引所在的节点 int j = 0; LDNode p = head.next; while (p != head && j < index) { j++; p = p.next; } if (p != head) { T old = p.data; p.data = data; return old; } } return null; } /** * 根据index添加 * * @param index * @param data * @return */ public boolean add(int index, T data) { int size = length(); if (data == null || index >= size) { return false; } //寻找插入节点的位置 int j = 0; LDNode p = head.next; while (j < index && p.next != head) { j++; p = p.next; } //插入节点,若果index==3,则插入位置是4 LDNode q = new LDNode(data, p, p.next); p.next.prev = q; p.next = q; return true; } /** * 尾部添加 * * @param data * @return */ public boolean add(T data) { //校验数据 if (data == null) { return false; } LDNode p = new LDNode(data, head.prev, head); head.prev.next = p; head.prev = p; return true; } /** * 根据索引删除节点 * * @param index * @return */ public T remove(int index) { T old = null; //校验数据 int size = length(); if (index < 0 || index >= size) { return old; } //寻找要删除的节点 int j = 0; LDNode p = head.next; while (j < index && p != head) { j++; p = p.next; } //删除节点 if (p != head) { old = p.data; p.prev.next = p.next; p.next.prev = p.prev; } return old; } /** * 删除指定data的所有节点 * * @param data * @return */ public boolean removeAll(T data) { boolean isRemove = false; //校验数据 if (data == null) { return isRemove; } //遍历删除 LDNode p = head.next; while (p != head) { if (p.data.equals(data)) { p.prev.next = p.next; p.next.prev = p.prev; isRemove = true; } p = p.next; } return isRemove; } public void clear() { head.next = head; head.prev = head; } /** * 判断循环双链表中是否含有data * @param data * @return */ public boolean contains(T data){ if (data == null) { return false; } //遍历查找data LDNode p = head.next; while (p != head) { if (p.data.equals(data)) { return true; } p = p.next; } return false; } @Override public String toString() { String s = "{"; LDNode p = head.next; while (p != head) { s += p.data; p = p.next; if (p != head) { s += ","; } } return s + "}"; } public static void main(String[] args) { String[] arr = {"A", "B", "C", "D", "E", "F"}; LoopDoubleLinkedList<String> loopDoubleLinkedList = new LoopDoubleLinkedList<String>(arr); System.out.println(loopDoubleLinkedList.length()); System.out.println(loopDoubleLinkedList.get(3)); System.out.println(loopDoubleLinkedList.contains("C")); System.out.println(loopDoubleLinkedList.contains("G")); System.out.println(loopDoubleLinkedList.isEmpty()); System.out.println(loopDoubleLinkedList.toString()); loopDoubleLinkedList.add(3, "H"); System.out.println(loopDoubleLinkedList.toString()); loopDoubleLinkedList.remove(4); System.out.println(loopDoubleLinkedList.toString()); loopDoubleLinkedList.add("H"); System.out.println(loopDoubleLinkedList.toString()); loopDoubleLinkedList.removeAll("E"); System.out.println(loopDoubleLinkedList.toString()); loopDoubleLinkedList.set(2, "XXOO"); System.out.println(loopDoubleLinkedList.toString()); } class LDNode { //节点值 T data; //前驱节点 LDNode prev; //后继节点 LDNode next; public LDNode(T data, LDNode prev, LDNode next) { this.data = data; this.prev = prev; this.next = next; } public LDNode() { this(null, null, null); } public LDNode(T data) { this(data, null, null); } @Override public String toString() { return data.toString(); } } }
java数据结构--循环双向链表的实现
最新推荐文章于 2021-02-16 19:10:05 发布