数据结构双链表类
public class DoublyList < T > {
public DoubleNode < T > head;
public DoubleNode < T > rear;
public int size;
public DoublyList ( ) {
this . head= new DoubleNode < > ( ) ;
this . rear= this . head;
this . size= 0 ;
}
public DoublyList ( T [ ] values) {
this ( ) ;
for ( int i= 0 ; i< values. length; i++ ) {
if ( values[ i] != null ) {
rear. next= new DoubleNode < T > ( values[ i] , rear, rear. next) ;
rear= rear. next;
size++ ;
}
}
}
public boolean isEmpty ( ) {
return this . head. next== null ;
}
public T get ( int i) {
DoubleNode < T > p= this . head. next;
for ( int j= 0 ; p!= null && j< i; j++ ) {
p= p. next;
}
return ( i>= 0 && p!= null ) ? p. data : null ;
}
public int size ( ) {
return size;
}
public void set ( int i, T x) {
DoubleNode < T > p= this . head. next;
if ( x== null )
throw new NullPointerException ( "x==null" ) ;
if ( i>= 0 && i<= this . size) {
for ( int j= 0 ; p!= null && j< i; j++ ) {
p= p. next;
}
p. data= x;
}
else
throw new IndexOutOfBoundsException ( ) ;
}
public String toString ( ) {
String str= this . getClass ( ) . getName ( ) + "(" ;
for ( DoubleNode < T > p= this . head. next ; p!= null ; p= p. next) {
str += p. data. toString ( ) + ( p. next!= null ? "," : "" ) ;
}
return str+ ")" ;
}
public DoubleNode < T > insertFront ( int i, T x) {
DoubleNode < T > front= this . head;
if ( x== null )
return null ;
if ( i< 0 )
i= 0 ;
if ( i> this . size)
i= size;
for ( int j= 0 ; front. next!= null && j< i; j++ )
front= front. next;
front. next= new DoubleNode < > ( x, front, front. next) ;
this . size++ ;
return front. next;
}
public DoubleNode < T > insertBehind ( int i, T x) {
DoubleNode < T > front= this . head;
if ( x== null )
return null ;
if ( i< 0 )
i= 0 ;
if ( i> this . size)
i= size;
for ( int j= 0 ; j<= i; j++ )
front= front. next;
front. next= new DoubleNode < > ( x, front, front. next) ;
this . size++ ;
if ( i== size) {
rear= rear. next;
}
return front. next;
}
public DoubleNode < T > insert ( T x) {
return this . insertFront ( Integer . MAX_VALUE, x) ;
}
public T remove ( int i) {
DoubleNode < T > front = this . head;
for ( int j = 0 ; j <= i; j++ ) {
front = front. next;
}
if ( i >= 0 ) {
T x = front. data;
front. prev. next = front. next;
if ( front != null ) {
front. next. prev = front. prev;
return x;
}
this . size-- ;
}
return null ;
}
public static void main ( String [ ] args) {
DoublyList < String > list= new DoublyList < > ( ) ;
for ( int i= 4 ; i>= 0 ; i-- ) {
list. insertFront ( 0 , ( char ) ( 'A' + i) + "" ) ;
}
System . out. println ( list. toString ( ) ) ;
list. remove ( 0 ) ;
System . out. println ( list. toString ( ) ) ;
list. insertFront ( 0 , "A" ) ;
System . out. println ( list. toString ( ) ) ;
list. insertFront ( 0 , "A" ) ;
System . out. println ( list. toString ( ) ) ;
list. insertBehind ( 0 , "X" ) ;
System . out. println ( list. toString ( ) ) ;
list. insertBehind ( 6 , "F" ) ;
System . out. println ( list. toString ( ) ) ;
list. insertFront ( 6 , "F" ) ;
System . out. println ( list. toString ( ) ) ;
}
}