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

    要求:

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

    补充代码:

    public class CircularArrayQueue<T> implements Queue<T>
    {
        private final int DEFAULT_CAPACITY = 10000;
        private int front, rear, size;
        private T[] queue;
        //-----------------------------------------------------------------
    // Creates an empty queue using the default capacity.
    //-----------------------------------------------------------------
        public CircularArrayQueue()
        {
            front = rear = size = 0;
            queue = (T[]) (new Object[DEFAULT_CAPACITY]);
        }
        //-----------------------------------------------------------------
    // Adds the specified element to the rear of this queue, expanding
    // the capacity of the queue array if necessary.
    //-----------------------------------------------------------------
        public void enqueue (T element)
        {
            if (size == queue.length)
                expandCapacity();
            queue[rear] = element;
            rear = (rear+1) % queue.length;
            size++;
        }
        public void expandCapacity()
        {
            T[] larger = (T[])(new Object[queue.length*2]);
            for (int index = 0; index < size; index++)
                larger[index] = queue[(front+index) % queue.length];
            front = 0;
            rear = size;
            queue = larger;
        }
    
        @Override
        public T dequeue()
        {
            if (size == 0)
                throw new EmptyCollectionException("queue");
            T d = queue[front];
            queue[front] = null;
            front = (front+1)%queue.length;
            size--;
            return d;
        }
    
        @Override
        public T first() {
            return queue[front];
        }
    
        @Override
        public boolean isEmpty() {
            boolean course = false;
            if(size ==0){
                course = true;
            }
            return course;
        }
    
        @Override
        public int size() {
            return size;
        }
    
    
    
        @Override
        public String toString(){
            String result = "";
            int scan = 0;
    
            while(scan < size)
            {
                if(queue[scan]!=null)
                {
                    result += queue[scan].toString()+"
    ";
                }
                scan++;
            }
            return result;
        }
    }
    
    

    单步跟踪:

    队列变化图:

  • 相关阅读:
    游戏修改器编写原理
    欲练 CSS ,必先宫 IE
    HTML结构化:CSS布局入门指南
    用css来定义table的样式
    HTML表格无空隙嵌套方法
    CSS布局学习笔记
    绽放最后的一丝美丽
    这种感觉真爽
    诗人,是否还有生存的空间
    提升人气的秘密武器
  • 原文地址:https://www.cnblogs.com/1zhjch/p/7710415.html
Copyright © 2011-2022 走看看