20162311 队列课下作业

20162311 队列课下作业

作业目标

  1. 补充课上没有完成的作业
  2. 参考15.3节,用自己完成的队列(链队,循环数组队列)实现模拟票务柜台排队功能
  3. 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息
  4. 把代码推送到代码托管平台
  5. 把完成过程写一篇博客:重点是单步跟踪过程和遇到的问题及解决过程
  6. 提交博客链接

具体步骤

一、完成书上的队列(链队、循环数组队列)

dequeue()方法

无论是用链表还是循环数组,首先都要判断队列是否为空。链表中还要考虑只有一个元素的情况,此时要将rear引用置为null。正常情况的话,只需将front的元素赋给result,将front引用指向下一个节点,然后再返回result,同时count--;循环数组的话只需将数组的front位置的元素返回并赋值null,不过要注意front的赋值,应该为front = (front + 1) % queue.length,同时count--

first()方法

同样,两种方法都先判断队列是否为空。链表的直接返回front.getElement(),而循环数组返回queue[front]

isEmpty()和size()方法

这两个方法都一样,isEmpty()返回count == 0 的布尔值;size()返回count的值

toString()方法

用循环的方式将队列中的元素打印出来就行了

模拟票务柜台

书上已经写好了Customer类和TicketCounter类,我将TicketCounter类中的LinkedQueue换成了自己实现的CircularArrayQueue

package ch15.javafoundations;

import ch15.Customer;

/**
 * Created by Administrator on 2017/10/14.
 */
public class NewTicketCounter {
    final static int PROCESS = 120;
    final static int MAX_CASHIERS = 10;
    final static int NUM_CUSTOMERS = 100;

    public static void main(String[] args) {
        Customer customer;
        CircularArrayQueue<Customer> customerQueue = new CircularArrayQueue<>();
        int[] cashierTime = new int[MAX_CASHIERS];
        int totalTime, averageTime, departs;

        for (int cashiers = 0; cashiers < MAX_CASHIERS; cashiers++) {
            /** set each cashiers time to zero initially*/
            for (int count = 0; count < cashiers; count++)
                cashierTime[count] = 0;

            /** load customer queue */
            for (int count = 1; count <= NUM_CUSTOMERS; count++)
                customerQueue.enqueue(new Customer(count * 15));

            totalTime = 0;
            /** process all customers in the queue */
            while (!(customerQueue.isEmpty())) {
                for (int count = 0; count <= cashiers; count++) {
                    if (!(customerQueue.isEmpty())) {
                        customer = customerQueue.dequeue();
                        if (customer.getArrivalTime() > cashierTime[count])
                            departs = customer.getArrivalTime() + PROCESS;
                        else
                            departs = cashierTime[count] + PROCESS;
                        customer.setDepartureTime(departs);
                        cashierTime[count] = departs;
                        totalTime += customer.totalTime();
                    }
                }
            }
            /** output results for this simulation */
            averageTime = totalTime / NUM_CUSTOMERS;
            System.out.println("Number of cashiers: " + (cashiers + 1));
            System.out.println("Average time: " + averageTime + "\n");
        }
    }
}

接下来设置断点进行单步跟踪,我先在最开始设置了一个断点

然后一步步跟踪,出现了问题

到这里的时候还是没问题的,但点击下一步之后,就出现以下情况

然后不管我等多久都没动静,后来我试了一下,把断点设在while循环后面

这样跳过了循环,虽然第一次可以了,但继续进行后面的循环时,还是出现和上面一样的问题。我去网上查,也查不出什么结果,目前这个问题还没解决,但是代码运行的结果是没问题的。

posted @ 2017-10-15 21:51  20162311张之睿  阅读(198)  评论(0编辑  收藏  举报