毎日 Java 300 行 day17-20

例証する

Min 氏の記事へのリンク: Three Hundred Lines of Java (Overview)_minfanphd のブログ - CSDN ブログ
メンテナンスのために、入力したコードを github に置きました: https://github.com/fulisha-ok/sampledata

day17 チェーン キュー

1.チェーンキュー機能

スタックと比較すると、キューは両端で操作できますが、キューの先頭はキューから出ることしかできず、キューの末尾はキューに入ることしかできません(人生で物を買うために列に並ぶのと同じです)キューを削除する場合、キューが空かどうかを判断する必要があります (次のコード: header.next == null)。head (header) と tail (tail) が同じノードを指している場合、キューは空なので、空のキューにもノードが必要です。

2. アイコン

  • キューを作成する
    ここに画像の説明を挿入
  • コードのエンキューとデキューを図と組み合わせて理解するのは簡単です。
    ここに画像の説明を挿入## コード
  • キューが空であると判断され
    、ヘッド ポインタとテール ポインタが同じヘッダーを指す == テール
package datastructure.queue;
public class LinkedQueue {
    class Node{
        /**
         * The data
         */
        int data;
        /**
         * The reference to the next node
         */
        Node next;

        /**
         * The constructor
         * @param paraValue The data
         */
        public Node(int paraValue){
            data = paraValue;
            next = null;
        }
    }

    /**
     * The header of the queue
     */
    Node header;

    /**
     * The tail of the queue
     */
    Node tail;

    /**
     * Construct an empty sequential list.
     */
    public LinkedQueue(){
        header = new Node(-1);

        tail = header;
    }

    /**
     * Enqueue
     * @param paraValue The value of the new node.
     */
    public void enqueue(int paraValue){
        Node tempNode = new Node(paraValue);
        tail.next = tempNode;
        tail = tempNode;
    }

    /**
     * Dequeue
     * @return The value at the header.
     */
    public int dequeue(){
        if (header == tail){
            System.out.println("No element in the queue");
            return -1;
        }
        int resultValue = header.next.data;
        header.next = header.next.next;

        // The queue becomes empty.
        if (header.next == null){
            tail = header;
        }
        return resultValue;
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    @Override
    public String toString(){
        String resultString = "";

        if (header.next == null){
            return "empty";
        }

        Node tempNode = header.next;
        while (tempNode != null){
            resultString += tempNode.data + ", ";
            tempNode = tempNode.next;
        }

        return resultString;
    }

    public static void main(String[] args) {
        LinkedQueue tempQueue = new LinkedQueue();
        System.out.println("Initialized, the list is: " + tempQueue.toString());

        for (int i = 0; i < 5; i++) {
            tempQueue.enqueue(i + 1);
        }
        System.out.println("Enqueue, the queue is: " + tempQueue.toString());

        tempQueue.dequeue();
        System.out.println("Dequeue, the queue is: " + tempQueue.toString());

        int tempValue;
        for (int i = 0; i < 5; i++) {
            tempValue = tempQueue.dequeue();
            System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
        }

        for (int i = 0; i < 3; i++) {
            tempQueue.enqueue(i + 10);
        }
        System.out.println("Enqueue, the queue is: " + tempQueue.toString());


    }
}

ここに画像の説明を挿入

day18 循環キュー

1.循環キュー機能

  • ダイアグラムを使ってコードを理解しますが、ダイアグラムと組み合わせると循環キューが理解しやすいと思います。チームに参加するときは、チームに入る前にチームが満員かどうかを判断します (順序: 最初に値を入力してからポインターを変更します); チームを離れるときは、チームを離れる前にチームが空かどうかを判断します (順序: 最初に値、次にポインターを変更します)
    ここに画像の説明を挿入

  • チーム空 vs フルの判定条件
    (1) チームフル: (後方 + 1) % MAX_SIZE = 前;
    (2) チーム空: 前 = 後方
    ここでのチームフル判定はスペースを犠牲にすることであり、他の判定方法もあるキューがいっぱいです。

2. 取模

チェーン キューから、キューのヘッド ポインターが 1 増加し、キューのテール ポインターが 1 増加していることがわかります。ヘッド/テール ポインターが何も処理を行わない場合、無限に増加し続けます。 、スペースの無駄につながります。上記の問題はモジュラスを実行することで回避できます. 循環キューの最大長が MAX_SIZE=10 のみであると仮定すると, モジュラスはヘッド/テール ポインタをループさせ, スペースの使用を最大化することができます.

3. コード

package datastructure.queue;
public class CircleIntQueue {
    /**
     * The total space. One space can never be used.
     */
    public static final int TOTAL_SPACE = 10;

    int[] data;

    /**
     * The index for calculating the head. The actual head is head % TOTAL_SPACE.
     */
    int head;

    /**
     * The index for calculating the tail.
     */
    int tail;

    public CircleIntQueue(){
        data = new int[TOTAL_SPACE];
        head = 0;
        tail = 0;
    }

    /**
     * enqueue
     * @param paraValue The value of the new node.
     */
    public void enqueue(int paraValue){
        if ((tail+1)%TOTAL_SPACE == head){
            System.out.println("Queue full.");
            return;
        }

        data[tail%TOTAL_SPACE] = paraValue;
        tail++;
    }

    public int dequeue(){
        if (head == tail){
            System.out.println("No element in the queue");
            return -1;
        }
        int resultValue = data[head%TOTAL_SPACE];
        head++;

        return resultValue;
    }

    @Override
    public String toString(){
        String resultString = "";

        if (head == tail){
            return "empty";
        }

        for (int i = head; i < tail; i++){
            resultString += data[i%TOTAL_SPACE] + ", ";
        }
        return resultString;
    }

    public static void main(String[] args) {
        CircleIntQueue tempQueue = new CircleIntQueue();
        System.out.println("Initialized, the list is: " + tempQueue.toString());

        for (int i = 0; i < 5; i++) {
            tempQueue.enqueue(i + 1);
        }
        System.out.println("Enqueue, the queue is: " + tempQueue.toString());

        int tempValue = tempQueue.dequeue();
        System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

        for (int i = 0; i < 6; i++) {
            tempQueue.enqueue(i + 10);
            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
        }

        for (int i = 0; i < 3; i++) {
            tempValue = tempQueue.dequeue();
            System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
        }

        for (int i = 0; i < 6; i++) {
            tempQueue.enqueue(i + 100);
            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
        }
    }
}

文字タイプ:

package datastructure.queue;
public class CircleCharQueue {
    public static final int TOTAL_SPACE = 10;

    char[] data;
    int head;
    int tail;

    /**
     * The constructor
     */
    public CircleCharQueue() {
        data = new char[TOTAL_SPACE];
        head = 0;
        tail = 0;
    }

    /**
     * Enqueue.
     * @param paraValue The value of the new node.
     */
    public void enqueue(char paraValue) {
        if ((tail + 1) % TOTAL_SPACE == head) {
            System.out.println("Queue full.");
            return;
        }

        data[tail % TOTAL_SPACE] = paraValue;
        tail++;
    }

    /**
     * Dequeue
     * @return The value at the head.
     */
    public char dequeue() {
        if (head == tail) {
            System.out.println("No element in the queue");
            return '\0';
        }

        char resultValue = data[head % TOTAL_SPACE];

        head++;

        return resultValue;
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    @Override
    public String toString() {
        String resultString = "";

        if (head == tail) {
            return "empty";
        }

        for (int i = head; i < tail; i++) {
            resultString += data[i % TOTAL_SPACE] + ", ";
        }

        return resultString;
    }

    public static void main(String args[]) {
        CircleCharQueue tempQueue = new CircleCharQueue();
        System.out.println("Initialized, the list is: " + tempQueue.toString());

        for (char i = '0'; i < '5'; i++) {
            tempQueue.enqueue(i);
        }
        System.out.println("Enqueue, the queue is: " + tempQueue.toString());

        char tempValue = tempQueue.dequeue();
        System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

        for (char i = 'a'; i < 'f'; i++) {
            tempQueue.enqueue(i);
            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
        }

        for (int i = 0; i < 3; i++) {
            tempValue = tempQueue.dequeue();
            System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
        }

        for (char i = 'A'; i < 'F'; i++) {
            tempQueue.enqueue(i);
            System.out.println("Enqueue, the queue is: " + tempQueue.toString());
        }
    }

}

ここに画像の説明を挿入

day19 ストリングマッチ

1.アイデア

文字列の一致、サブ文字列とメイン文字列の一致。部分文字列が主文字列と一致しない場合、主文字列は次の文字から部分文字列の一致を開始し、複数の一致の最後まで一致しない場合は、一致しない場合はアドレスを返します。このようにして、2 つの文字列の「ポインター」をさかのぼる必要があります。KMP を使用する場合は、部分文字列ポインターのみをトレースバックする必要があります。

2.コード

package main.java.datastructure;
import sun.applet.Main;
import java.time.Year;
public class MyString {
    /**
     *  The maximal length.
     */
    public static final int MAX_LENGTH = 10;

    /**
     * The actual length.
     */
    int length;

    /**
     * The data.
     */
    char[] data;

    /**
     * Construct an empty char array.
     */
    public MyString(){
        length = 0;
        data = new char[MAX_LENGTH];
    }

    /**
     *Construct using a system defined string.
     * @param paraString The given string. Its length should not exceed MAX_LENGTH - 1.
     */
    public MyString(String paraString){
        data = new char[MAX_LENGTH];
        length = paraString.length();

        for (int i = 0; i < length; i++){
            data[i] = paraString.charAt(i);
        }
    }

    public String toString(){
        String resultString = "";

        for (int i = 0; i < length; i++){
            resultString += data[i];
        }

        return resultString;
    }

    /**
     * Locate the position of a substring.
     * @param paraString The given substring.
     * @return The first position. -1 for no matching.
     */
    public int locate(MyString paraString){
        boolean tempMatch = false;

        for (int i = 0; i < length - paraString.length + 1; i++){
            tempMatch = true;

            for (int j = 0; j < paraString.length; j++){
                if (data[i+j] != paraString.data[j]){
                    tempMatch = false;
                    break;
                }
            }
            if (tempMatch){
                return i;
            }
        }

        return -1;
    }

    /**
     * Get a substring
     * @param paraStartPosition The start position in the original string.
     * @param paraLength  The length of the new string.
     * @return The first position. -1 for no matching.
     */
    public MyString substring(int paraStartPosition, int paraLength){
        if (paraStartPosition + paraLength > length){
            System.out.println("The bound is exceeded.");
            return null;
        }

        MyString resultMyString = new MyString();
        resultMyString.length = paraLength;
        for (int i = 0; i < paraLength; i++){
            resultMyString.data[i] = data[paraStartPosition + i];
        }

        return resultMyString;
    }

    public static void main(String args[]) {
        MyString tempFirstString = new MyString("I like ik.");
        MyString tempSecondString = new MyString("ik");
        int tempPosition = tempFirstString.locate(tempSecondString);
        System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString
                + "\" is: " + tempPosition);

        MyString tempThirdString = new MyString("ki");
        tempPosition = tempFirstString.locate(tempThirdString);
        System.out.println("The position of \"" + tempThirdString + "\" in \"" + tempFirstString
                + "\" is: " + tempPosition);

        tempThirdString = tempFirstString.substring(1, 2);
        System.out.println("The substring is: \"" + tempThirdString + "\"");

        tempThirdString = tempFirstString.substring(5, 5);
        System.out.println("The substring is: \"" + tempThirdString + "\"");

        tempThirdString = tempFirstString.substring(5, 6);
        System.out.println("The substring is: \"" + tempThirdString + "\"");
    }

}

ここに画像の説明を挿入

day20 まとめ

1. オブジェクト指向とプロセス指向の利点は何ですか?

プロセス指向で、以前に書いた「行列加算」などの操作手順に主に焦点を当てています。このクラスは主に機能を完成させ、行列加算を完了する方法、MatrixAddition です。

オブジェクト指向: 例えば以前に連結リストクラスを書いた.連結リストクラスをクラスオブジェクトに抽象化した.このオブジェクトは独自の変数とメソッドを持ち,検索,挿入,削除などの機能を実現する.これらのメソッドのいずれかを使用するには、オブジェクトを介して呼び出すことができます. 別のメソッドを使用したい場合は、何もないことがわかります. オブジェクトに追加して、将来そのメソッドを再利用できます.

したがって、オブジェクト指向は拡張と変更が容易で、モジュール化が容易で、コードの冗長性を減らします。

2. シーケンス テーブルとリンク リストの類似点と相違点を比較し、シーケンス テーブルとリンク リストの長所と短所を分析します。

(1) シーケンス テーブルは、定義時にスペースを事前に割り当てる必要があります。シーケンス テーブルを挿入および削除するときは、シーケンス テーブルをトラバースし、要素を移動する必要があります。連結リストは事前に割り当てる必要はありません。定義時にスペースを確保し、動的に割り当てることができる リンクリストは、挿入時に挿入するだけでよい そのポインタを (
2) データの検索に変更する シーケンステーブル (配列で識別される) の場合、検索位置が検索は便利ですが、追加と削除は非常に遅いです。リンクされたリストの場合、ヘッド ノードから検索する必要があり、検索は遅くなりますが、追加と削除は高速です。

3. 調整手順の一般的な問題と解決策を分析する

(1) NULL ポインタ例外 コードを記述する過程で、NULL と見なされる可能性があるものについては、NULL 判定保護コードのレイヤをコードに追加する必要があります。たとえば、配列がすでに null の場合でもデータをフェッチする必要があり、オブジェクトが null の場合でもオブジェクトの特定の値をフェッチする必要があります。
(2) 配列範囲外例外 配列を初期化するときは、長さの定義が適切でなければならず、配列の追加または削除には、配列の成都操作を判断する追加のレイヤーが必要です。

4. チェーン キューと循環キューの長所と短所を分析します。

チェーン キューと比較して、循環キューはスペース使用率が高く、削除された要素スペースを再利用できますが、動的に増加できるチェーン キューとは異なり、循環キューのサイズは事前に定義されています。

5.合理的ですか?

18日目に開設された2つのキューの違いは、基本データがint型で一方がchar型であるということで、この考え方によると、基本データ型が異なるため、クラスを書き換える必要があるということですが、これは妥当でしょうか?どう思いますか?
これは不合理であり、オブジェクト指向の考え方を満足させません。ジェネリックが使えます。ただし、Object クラスはすべてのクラスのスーパークラスであり、int 型と char 型を満足できるため、Object 型を使用して実装しますが、Object クラスは任意の型を受け取ることができるため、動作中にバグが発生する可能性があります。

  public static final int TOTAL_SPACE = 10;

    Object[] data;

    //The index for calculating the head
    int head;

    //The index for calculating the tail
    int tail;

    public CircleQueue () {
        data = new Object[TOTAL_SPACE];
        head = 0;
        tail = 0;
    }

おすすめ

転載: blog.csdn.net/fulishafulisha/article/details/130011304
おすすめ