package myDataStructures;
import myDataStructures.MyArrayList;
import javax.swing.text.html.HTMLDocument;
import java.util.*;
public class MyLinkedList<T> {
private int theSize;
private int modCount; //统计对双向链表的修改的次数
private Node<T> theBeginNode;
private Node<T> theEndNode;
private Node<T> theMiddleNode;
private int indexOfMiddle;
public MyLinkedList(){
doClear();
}
public boolean clear(){
return doClear();
}
private boolean doClear(){
this.theSize=0;
this.theBeginNode=new Node<>(null,this.theEndNode,null);
this.theEndNode=new Node<>(null,null,this.theBeginNode);
this.theMiddleNode=this.theBeginNode;
this.indexOfMiddle=(int)theSize/2;
this.modCount=0;
return true;
}
public void add(T value){
add(theSize, value);
}
public void add(int index,T value){
if ((int)theSize/2 > indexOfMiddle){
theMiddleNode=returnThePredNode(0,theSize,(int)theSize/2);
indexOfMiddle=(int)theSize/2;
}
Node<T> predNodeOfIndexWantToInsert= returnThePredNode(0, theSize, index);
Node<T> indexData=new Node<>(value,predNodeOfIndexWantToInsert.next,predNodeOfIndexWantToInsert);
predNodeOfIndexWantToInsert.next=indexData;
}
public T get(){
return get(theSize-1);
}
public boolean isEmpty(){
return theSize==0;
}
public T get(int index){
Node<T> nodeGet= returnThePredNode(0,theSize-1,index);
return nodeGet.data;
}
public returnTwo<T,Boolean> remove(){
return remove(theSize-1);
}
public returnTwo<T,Boolean> remove(int index){
Node<T> predOfWantRemove= returnThePredNode(0,theSize-1,index);
T oldData=predOfWantRemove.next.data;
predOfWantRemove.next=predOfWantRemove.next.next;
predOfWantRemove.next.pred=predOfWantRemove; //删除了该节点的前后向next链
return new returnTwo<>(oldData,true);
}
public MyLinkedListIterator<T> iterator(){
return new MyLinkedListIterator<>(theBeginNode);
}
public Node<T> returnThePredNode(int lowBaseLine, int highBaseLine, int index){
Node<T> rePred;
if (index< lowBaseLine || index> highBaseLine){
throw new ArrayIndexOutOfBoundsException(index);
}
int flag=(int)theSize/2;
if (index == flag && indexOfMiddle == flag){
// ++modCount;
return theMiddleNode;
}
if (index< flag){
int newFlag=(int)(indexOfMiddle+theSize)/2;
if (index> newFlag){
rePred= theEndNode.pred;
for (int i=theSize-1; i>= index; i--){
rePred= rePred.pred;
}
// ++modCount;
return rePred;
}
else {
rePred= theMiddleNode.pred;
for (int i=indexOfMiddle-1; i< index-1; i++){
rePred=rePred.next;
}
return rePred;
}
}
else {
int newFlag=(int)(indexOfMiddle)/2;
if (index> newFlag){
rePred= theMiddleNode.pred;
for (int i=indexOfMiddle-1; i>= index; i--){
rePred= rePred.pred;
}
// ++modCount;
return rePred;
}
else {
rePred= theBeginNode.next;
for (int i=0; i< index-1; i++){
rePred=rePred.next;
}
return rePred;
}
}
}
public int size(){
return theSize;
}
// 实现节点 已经前向后向的next链
private static class Node<T>{
private Node<T> next;
private Node<T> pred;
private T data;
private Node(T d, Node<T> n, Node<T> p){
this.next=n;
this.pred=p;
this.data=d;
}
}
private class returnTwo<A,B>{
A a;
B b;
private returnTwo(A a,B b){
this.a=a;
this.b=b;
}
}
private class MyLinkedListIterator<T> implements Iterator<T>{ //实现了foreach
private Node<T> currentNode;
private int nowIndex;
private MyLinkedListIterator(Node<T> a){
currentNode= a;
nowIndex= -1;
}
@Override
public T next() {
if (! hasNext()){
throw new ArrayIndexOutOfBoundsException(nowIndex+1);
}
T returnData=currentNode.next.data;
currentNode=currentNode.next;
++nowIndex;
return returnData;
}
@Override
public boolean hasNext() {
return (currentNode.next != null);
}
@Override
public void remove(){
MyLinkedList.this.remove(nowIndex--);
}
}
}
class Test{
public static void main(String[] args){
MyLinkedList<Integer> a=new MyLinkedList<>();
System.out.println(a.toString());
MyLinkedList<Integer> b=a;
// System.out.println(a.theSize);
// b.theSize=10;
// System.out.println(a.theSize);
}
}
java实现Linkedlist
最新推荐文章于 2023-05-09 19:35:17 发布