OJプログラムブロックJava

2066 ワード

1、単一チェーンテーブルのノード:
public static class ListNode {
    int value;
    ListNode next;
}

2、スタック
Stack<> stack = new Stack<>();
stack.push();
stack.pop();

3、二叉木の接点
public static class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
}

4、自発的に異常を投げ出す
throw new RuntimeException("Xxx");

5、シングルチェーンテーブルの印刷
public static void printList(ListNode head) {
    while(head != null) {
        System.out.print(head.value + "->");
        head = head.next;
    }
    System.out.println("null");
}

6、配列要素の印刷
public static void printArray(int[] arr) {
    if(arr == null || arr.length <= 0) {
        return;
    }
    for (int i : arr) {
        System.out.print(i + " ");
    } 
    System.out.println();
}

7、先に二叉木を遍歴する
public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        System.out.print(node.value + " ");
        printTree(node.left);
        printTree(node.right);
    }
}

ちゅうかんじゅんかんツリー
public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        printTree(node.left);
        System.out.print(node.value + " ");
        printTree(node.right);
    }
}

後順に二叉木を巡る
public static void printTree(BinaryTreeNode node) {
    if(node != null) {
        printTree(node.left);
        printTree(node.right);
        System.out.print(node.value + " ");
    }
}

8、2つのチェーンテーブルが同じチェーンテーブルかどうかを判断する
public static boolean isSameList(ComplexListNode head1, ComplexListNode head2) {
    while (head1 != null && head2 != null) {
        if (head1 == head2) {
            head1 = head1.next;
            head2 = head2.next;
        } else {
            return false;
        }
    }
    return head1 == null && head2 == null;
}