c 2 java第1編汎用とダブルチェーンテーブル
前編はBigIntegerに膜拝しましたが、複雑だと思っていましたが、jdkバンドのソースコードを見ると、3000行javaファイルしかありませんでした.だから右は自分で底のものを書く必要がある.
一つは手を鍛えるため、二つはjavaの考え方を体得するため、三つはC++の後継者たちが何を放棄したかを比較するためだ.
くだらないことは言わないで、要点はすべて注釈の中に書きました:
一つは手を鍛えるため、二つはjavaの考え方を体得するため、三つはC++の後継者たちが何を放棄したかを比較するためだ.
くだらないことは言わないで、要点はすべて注釈の中に書きました:
/*LinkedListTest.java -- C
1. head->func(head) this head.func()。
2. C / --> ,
public <T extends Comparable<? super T>>
public <T extends Comparable<T>>
, 。
keywords:"Comparable generic type"
DNode<T> DNode<T extends Comparable<T>>
extends 。
* author ludi 2014.03*/
class DNode<T extends Comparable<T>> {
public T val;
public DNode<T> next, prev;
/* , */
public DNode(){
/* val , */
val = null;
next = prev = this;
}
public DNode(T item){
val = item;
next = prev = this;
/* null?
* , tail,
* head->prev = tail; tail.next = head.
* */
}
/* <T>, DNode<T> */
public String toString(){
String str = "[";
/* : , ,...*/
DNode<T> curr = this.next;
while(curr != this){
if(curr.val != null)str += curr.val.toString() + ", ";
curr = curr.next;
}
str += "]";
return str;
}
public DNode<T> indexOf( int idx){
DNode<T> curr = this.next;
for(; curr != this; curr = curr.next){
if(0 == idx)break;
--idx;
}
return (0 == idx) ? curr : null;
}
public void insertAfter(DNode<T> node, DNode<T> x){
x.next = node.next;
node.next.prev = x;
x.prev = node;
node.next = x;
}
public void insertTail( DNode<T> x){
insertAfter(this.prev, x);
}
public DNode<T> deleteAfter(DNode<T> node){
DNode<T> x = node.next;
x.next.prev = node;
node.next = x.next;
return x;
}
/* , , , 。
* */
public void reverseList(){
/* , 。( , ?)*/
DNode<T> first = this.next, last = this.prev;
while(first != last){
T val = first.val;
first.val = last.val;
last.val = val;
first = first.next;
last = last.prev;
}
}
public void deleteAll(){
/* */
DNode<T> x = null;
while(this.next != this){
x = deleteAfter(this);
x = null;
}
}
public DNode<T> insertionSort()
{/* */
DNode<T> list = new DNode<T>(), j = null, x = null;
DNode<T> head = this;
x = head.deleteAfter(head);
for(; x != head; x = head.deleteAfter(head)){
/*<= 。 */
j = list.prev;
while(j != list && x.val.compareTo(j.val) < 0){
j = j.prev;
/* , arr[j+1] = arr[j]。
* , sizeof(T) , */
}
System.out.println(list.toString() + " <-- " + x.val + " at " + j.val);
list.insertAfter(j, x);
}
return list;
}
public int search( T val){
/* -1*/
DNode<T> first = this.next;
int idx = 0;
for(; first != this; first = first.next){
if(0 == first.val.compareTo(val))return idx;
++idx;
}
System.out.printf("===xx
"); /* ?A: println(search(i));*/
return -1;
}
}
public class LinkedListTest{
public static void main(String[] arg){
/* , */
DNode<Integer> head = new DNode<Integer>(), node = null, curr = null;
int i;
for(i = 0; i < 5; ++i){
node = new DNode<Integer>(2*i);
head.insertTail(node);
}
System.out.println(head.toString() );
for(i = 0; i < 5; ++i){
node = new DNode<Integer>(2*i+1);
head.insertAfter(head, node);
}
System.out.println(head.toString() );
i = 5;
node = head.indexOf(i);
head.deleteAfter(node);
System.out.printf("delete afert %d-th %d:
", i, node.val);
System.out.println(head.toString() );
head.reverseList();
System.out.println("reversed:
" + head.toString());
node = head.insertionSort();
head = null; head = node; /* , ?*/
System.out.println("Sorted:
" + head.toString());
i = 6;
System.out.printf("Search %d at pos %d
", i, head.search( i));
head.deleteAll();
System.out.println("deleteAll:
" + head.toString());
head = null;
}
}
/*
ludi@ubun:~/java
$ javac -encoding UTF-8 LinkedListTest.java && java LinkedListTest
[0, 2, 4, 6, 8, ]
[9, 7, 5, 3, 1, 0, 2, 4, 6, 8, ]
delete afert 5-th 0:
[9, 7, 5, 3, 1, 0, 4, 6, 8, ]
reversed:
[8, 6, 4, 0, 1, 3, 5, 7, 9, ]
Sorted:
[0, 1, 3, 4, 5, 6, 7, 8, 9, ]
Search 6 at pos 5
deleteAll:
[]
ludi@ubun:~/java
$
*/