zoukankan      html  css  js  c++  java
  • 队列课下作业

    作业要求

    用链表实现队列需补全:

    dequeue()方法
    声明一个泛型,首先判断是否为空;当链表中只有一个元素时,将front的元素赋给result,使其为null;当不只一个时,将front引用指向下一个节点,并使总数count--

    public T dequeue() throws EmptyCollectionException {
            if (isEmpty()) throw new EmptyCollectionException("queue");
            T result = front.getElement();
            front = front.getNext();
            count--;
            if (isEmpty()) rear = null;
            return result;
    

    first()方法
    直接返回front.getElement()

    public T first() throws EmptyCollectionException {
            if (isEmpty()) throw new EmptyCollectionException("queue");
            return front.getElement();
        }
    

    isEmpty()

    返回count == 0 的布尔值
    public boolean isEmpty() {
            return (count == 0);
        }
    

    size()
    返回count的值

    public int size() {
            return count;
        }
    

    toString()
    将每个元素历遍,toString的结果相加

    public String toString() {
            String result = "";
            LinearNode<T> current = front;
            while (current != null) {
                result = result + (current.getElement()).toString() + "
    ";
                current = current.getNext();
            }
            return result;
        }
    

    Java实现

    public class TicketCounter {
        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;
            LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
            int[] cashierTime = new int[MAX_CASHIERS];
            int totalTime, averageTime, departs;      /** process the simulation for various number of cashiers */
            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) {
    
                    public void deposit() {
    
                    }
                });
                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 + "
    ");
            }
        }
    }
    
    

    单步跟踪

    测试结果

    代码链接

    队列变化图

    不太理解题目要求

  • 相关阅读:
    tomcat bug之部署应用的时候经常会发上startup failed due to previous errors
    maven编译项目理解
    MyReport报表引擎2.6.5.0新功能
    PHP入门-摘要表格处理问题
    EnumMap源代码阅读器
    MySQL几种方法的数据库备份
    工作日志2014-08-19
    Linux通过网卡驱动程序和版本号的信息
    JS于,子类调用父类的函数
    hdu 5007 水 弦
  • 原文地址:https://www.cnblogs.com/JXY6996/p/7674818.html
Copyright © 2011-2022 走看看