http://cur.lv/4x9e0 (Earn Bitcoins)
class CircularDoubleLinkedList{
public CircularDoubleLinkedList prev, next;
public int value;
public CircularDoubleLinkedList(int value){
this.value = value;
this.prev = this;
this.next = this;
//assert(this.checkStructure());
}
private CircularDoubleLinkedList(int value, CircularDoubleLinkedList prev, CircularDoubleLinkedList next){
this.value = value;
this.prev = prev;
this.next = next;
//assert(this.checkStructure());
}
public CircularDoubleLinkedList insert(int value){
CircularDoubleLinkedList res = new CircularDoubleLinkedList(value);
res.prev = this.prev;
res.next = this;
this.prev.next = res;
this.prev = res;
return res;
}
public int size(){
int res = 1;
CircularDoubleLinkedList tmp = this.next;
while(!(tmp == this)){
res++;
tmp = tmp.next;
}
return res;
}
public boolean checkStructure(){
CircularDoubleLinkedList tmpThisNext = this; //Laufvariable für die Vorgänger der Elemente
CircularDoubleLinkedList tmpThis = this; // für die Abbruchbedingung
if(tmpThis.next == null || tmpThis.prev == null){
return false;
}
CircularDoubleLinkedList tmpPrev = this.prev; // für die letzte Überprüfung
CircularDoubleLinkedList tmpNext = this.next; // Laufvariable für die Elemente
while(!(tmpNext == tmpThis)){
if(tmpNext.prev != tmpThisNext) return false;
if(tmpNext.next == null) return false;
if(tmpNext.next == this && tmpPrev != tmpNext) return false;
tmpThisNext = tmpThisNext.next;
tmpNext = tmpNext.next;
}
return true;
}
public static void main(String[] args){
CircularDoubleLinkedList c1 = new CircularDoubleLinkedList(1);
System.out.println(c1.size());
System.out.println(c1.checkStructure());
c1 = c1.insert(2);
System.out.println(c1.size());
System.out.println(c1.checkStructure());
c1 = c1.insert(3);
System.out.println(c1.size());
System.out.println(c1.checkStructure());
}
}