跟着我爹学Java 18-20天

第 18 天: 循环队列

18.1 整除的作用.
18.2 想像操场跑道里放一队人, 循环的感觉就出来了.
18.3 为了区分空队列与满队列, 需要留一个空间. 相当于不允许首尾相连. 还是画个图吧, 否则容易进坑.
18.4 用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统, 从这个代码可以感受下内存的管理. 

package linear_data_structure;

public class CircleIntQueue {
    public static final int TOTAL_SPACE = 10;
    int head;
    int tail;
    int[] data;

    public CircleIntQueue() {
        data = new int[TOTAL_SPACE];
        head = 0;
        tail = 0;
    }
    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;
    }
    public String toString(){
        String resultValue = "";
        if(head == tail){
            return "empty";
        }
        for (int i = head; i < tail; i++) {
            resultValue += data[i%TOTAL_SPACE] + ",";

        }
        return resultValue;
    }

    public static void main(String[] args) {
        CircleIntQueue testQueue = new CircleIntQueue();
        System.out.println("Initialized , the queue is :" + testQueue.toString());
        for (int i = 0; i < 5; i++) {
            testQueue.enQueue(i+1);
        }
        System.out.println("Enqueued , the queue is :" + testQueue.toString());
        int tempValue = testQueue.deQueue();
        System.out.println("Dequeued , the queue is :" + testQueue.toString());
        for (int i = 0; i < 10; i++) {
            testQueue.enQueue(i+10);
            System.out.println("Enqueued , the queue is :" + testQueue.toString());
        }
        for (int i = 0; i < 15; i++) {
            testQueue.deQueue();
            System.out.println("Dequeued , the queue is :" + testQueue.toString());
        }
    }
}

char类型

package linear_data_structure;

/**
 * Circle queue.
 *
 * @author Fan Min minfanphd@163.com.
 */
public class CircleCharQueue {

    /**
     * The total space. One space can never be used.
     */
    public static final int TOTAL_SPACE = 10;

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

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

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

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

    /**
     *********************
     * 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;
        } // Of if

        data[tail % TOTAL_SPACE] = paraValue;
        tail++;
    }// Of enqueue

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

        char resultValue = data[head % TOTAL_SPACE];

        head++;

        return resultValue;
    }// Of dequeue

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

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

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

        return resultString;
    }// Of toString

    /**
     *********************
     * The entrance of the program.
     *
     * @param args Not used now.
     *********************
     */
    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);
        } // Of for 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());
        } // Of for i

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

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

}// Of CircleCharQueue

第 19 天: 循环队列

package linear_data_structure;

public class MyString {
    public static final int MAX_LENGTH = 10;
    int length;
    char[] data;

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

    //construct using a system defined string.
    public MyString(String paraString) {
        data = new char[MAX_LENGTH];
        length = paraString.length();

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

    public String toString() {
        String resultString = "";
        for (int i = 0; i < length; i++) {
            resultString += data[i];
        }
        return resultString;
    }

    public int locate(MyString paraMyString) {
        boolean tempMatch = false;
        for (int i = 0; i < length - paraMyString.length + 1; i++) {
            tempMatch = true;
            for (int j = 0; j < paraMyString.length; j++) {
                if (data[i + j] != paraMyString.data[j]) {
                    tempMatch = false;
                    break;
                }

            }
            if (tempMatch) {
                return i;
            }
        }
        return -1;
    }

    public MyString substring(int paraStartPosition, int paralength) {
        if (paralength + paraStartPosition > length) {
            System.out.println("The bound is exceed.");
            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 + "\"");
    }// Of main
}

第 20 天: 总结

  1. 面向对象与面向过程相比, 有哪些优势? 注: 第 1 - 10 天的程序, 就是面向过程的.
  2. 比较顺序表和链表的异同.
     
  3. 分析顺序表和链表的优缺点.
     
  4. 分析链队列与循环队列的优缺点.
     
  5. 第 18 天建立的两个队列, 其区别仅在于基础数据不同, 一个是 int, 一个是 char. 按这种思路, 对于不同的基础数据类型, 都需要重写一个类, 这样合理吗? 你想怎么样?
     
  6. 分析调拭程序常见的问题及解决方案.
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值