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;
        }
    }
    
    

    单步跟踪:

    队列变化图:

  • 相关阅读:
    Java面向对象_常用类库api——二分查找算法
    Spyder中代码提示功能添加
    随机数种子random.seed()理解
    Windows10下Anaconda+Tensorflow+Keras环境配置
    面向对象分析与设计—OOD部分
    面向对象分析与设计—OOA部分
    面向对象分析与设计—基本概念部分
    基于聚类K-Means方法实现图像分割
    机器学习中强化学习与监督学习、无监督学习区别
    LeetCode501.二叉搜索树中的众数
  • 原文地址:https://www.cnblogs.com/1zhjch/p/7710415.html
Copyright © 2011-2022 走看看