代码实现
package top. gldwolf. java. datastructure. linkedtable;
public class LinkedTable < T> { private Node head = new Node ( ) ; private Node last; private int length = 1 ; public LinkedTable ( ) { } public boolean add ( T data) { Node< T> node = new Node < > ( data) ; if ( head. next == null) { head. next = node; } else { last. next = node; } last = node; length++ ; return true ; } public T get ( int index) throws Exception { if ( index <= 0 || index >= length) { throw new Exception ( "下标越界" ) ; } else { Node cursor = head; for ( int i = 1 ; i <= index; i++ ) { cursor = cursor. next; } return ( T) cursor. getData ( ) ; } } public boolean insert ( int index, T data) throws Exception { if ( index < 1 || index > length) { throw new Exception ( "下标越界" ) ; } else if ( index == length) { Node node = new Node ( data) ; last. next = node; last = node; } else if ( index == 1 ) { Node node = new Node ( data) ; node. next = head. next; head. next = node; } else { Node node = new Node ( data) ; Node cursor = head; for ( int i = 1 ; i < index; i++ ) { cursor = cursor. next; } node. next = cursor. next; cursor. next = node; } length ++ ; return true ; } public boolean delete ( int index) throws Exception { if ( index < 1 || index > length) { throw new Exception ( "下标越界" ) ; } else if ( index == 1 ) { head. next = head. next. next; } else if ( index == length - 1 ) { Node cursor = head; for ( int i = 1 ; i < length; i++ ) { cursor = cursor. next; } cursor. next = null; } else { Node cursor = head; for ( int i = 1 ; i < index; i++ ) { cursor = cursor. next; } cursor. next = cursor. next. next; } length-- ; return true ; } @Override public String toString ( ) { StringBuffer res = new StringBuffer ( ) ; Node cursor = head; for ( int i = 1 ; i < length; i++ ) { cursor = cursor. next; res. append ( cursor. getData ( ) + ", " ) ; } return res. toString ( ) + " length: " + ( length - 1 ) ; }
} class Node < N> { protected Node next; private N data; protected Node ( ) { } public Node ( N data) { this . data = data; } public N getData ( ) { return data; }
}
测试类
package top. gldwolf. java. datastructure. linkedtable; public class LinkedTableDriver { public static void main ( String[ ] args) throws Exception { LinkedTable< String> table = new LinkedTable < > ( ) ; table. add ( "2" ) ; table. add ( "3" ) ; table. add ( "4" ) ; table. add ( "5" ) ; table. add ( "6" ) ; String s = table. get ( 5 ) ; System. out. println ( s) ; table. insert ( 1 , "1" ) ; System. out. println ( table. get ( 1 ) ) ; System. out. println ( table. get ( 5 ) ) ; table. insert ( 4 , "9" ) ; System. out. println ( table. get ( 4 ) ) ; System. out. println ( table. toString ( ) ) ; table. delete ( 4 ) ; System. out. println ( table) ; table. delete ( 1 ) ; System. out. println ( table) ; table. delete ( 5 ) ; System. out. println ( table) ; table. add ( "6" ) ; System. out. println ( table) ; System. out. println ( table. get ( 5 ) ) ; System. out. println ( table. get ( 3 ) ) ; table. delete ( 5 ) ; table. delete ( 3 ) ; System. out. println ( table) ; }
}