循环双链表Java
程序员文章站
2024-03-22 11:42:52
...
参考
算法导论
java数据结构与算法之双链表设计与实现
package com.example;
import com.sun.org.apache.bcel.internal.classfile.PMGClass;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* Created by 谢晗超 on 2018/6/21.
*/
public class LoopDoubleLinkedList<T> implements ILinkedList<T> {
DNode<T> head;
public LoopDoubleLinkedList() {
this.head = new DNode<>();
this.head.next = this.head;
this.head.prev = this.head;
}
public LoopDoubleLinkedList(T[] arrays) {
this();
if (arrays.length < 1 || arrays == null) {
return;
}
DNode<T> p = new DNode<>(arrays[0]);
this.head.next = p;
this.head.prev = p;
p.prev = this.head;
p.next = this.head;
int index = 1;
while (index < arrays.length) {
p.next = new DNode<>(arrays[index++], p, this.head);
this.head.prev = p.next;
p = p.next;
}
}
public void print() {
DNode<T> cur = this.head.next;
//因为是循环链表不用考虑null的情况
while (cur != this.head) {
System.out.println(cur.data);
cur = cur.next;
}
}
@Override
public T set(int index, T data) {
if (index < 0 || data == null || isEmpty()) {
return null;
}
DNode<T> cur = this.head.next;
int curIndex = 0;
while (cur != this.head && curIndex < index) {
cur = cur.next;
curIndex++;
}
if (cur != head) {
T oldData = cur.data;
cur.data = data;
return oldData;
}
return null;
}
public static void main(String[] args) {
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Integer[] arrays = new Integer[]{0, 1, 2, 3};
LoopDoubleLinkedList<Integer> loopDoubleLinkedList = new LoopDoubleLinkedList(arrays);
ILinkedList removeList = new LoopDoubleLinkedList(new Integer[]{0, 2});
loopDoubleLinkedList.removeAll(removeList);
loopDoubleLinkedList.print();
}
@Override
public boolean isEmpty() {
if (this.head == this.head.next) {
return true;
}
return false;
}
@Override
public int length() {
int count = 0;
if (isEmpty()) {
return 0;
}
DNode<T> cur = this.head.next;
while (cur != this.head) {
count++;
cur = cur.next;
}
return count;
}
@Override
public T get(int index) {
DNode<T> cur = null;
if (index < 0 || isEmpty()) {
return null;
}
cur = this.head.next;
int curIndex = 0;
while (cur != this.head) {
if (curIndex == index) {
break;
} else {
cur = cur.next;
}
curIndex++;
}
return cur.data;
}
@Override
public boolean add(int index, T data) {
if (index < 0 || isEmpty()){
return false;
}
DNode<T> cur = this.head.next;
int curIndex = 0;
while (cur != this.head && curIndex < index){
curIndex++;
cur = cur.next;
}
DNode<T> newData = new DNode<>(data, cur, cur.next);
cur.next.prev = newData;
cur.next = newData;
return true;
}
@Override
public boolean add(T data) {
if (data == null){
return false;
}
DNode<T> newDNode = null;
newDNode = new DNode<>(data, this.head.prev, this.head);
this.head.prev.next = newDNode;
this.head.prev = newDNode;
return true;
}
@Override
public T remove(int index) {
if (index < 0 || isEmpty()){
return null;
}
int curIndex = 0;
DNode<T> cur = this.head.next;
while (cur != this.head && curIndex < index){
curIndex++;
cur = cur.next;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
return cur.data;
}
public void remove(){
}
@Override
public boolean removeAll(ILinkedList data) {
boolean isRemoveAll = false;
DNode<T> cur = this.head.next;
while (cur != this.head){
if (data.contains(cur.data)){
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
isRemoveAll = true;
}
cur = cur.next;
}
return isRemoveAll;
}
@Override
public void clear() {
this.head.prev = this.head;
this.head.next = this.head;
}
@Override
public boolean contains(T data) {
if (data == null || isEmpty()){
return false;
}
DNode<T> cur = this.head.next;
while (cur != this.head){
if (data.equals(cur.data)){
return true;
}
cur = cur.next;
}
return false;
}
}