• 队列课下作业


    一、本次作业要求

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

    二、完成作业过程

    1、补充教材上的代码

    • 教材中有给了一个TicketCounter类来实现排队和记录排队时间的功能,在这个类中,他调用了LinkedQueue以及Counter这两个类。其中的Counter类教材已经给出示例,而LinkedQueue中仅仅给出了enqueue方法,其他的dequeue、first、isEmpty、size、toString方法都没有给出定义,而且在代码中提出的LinearNode也没有代码实现,这些代码需要自己补全,下面是我对补全代码的说明。
    • LinearNode
      这段代码在学习第十四章的内容的时候就已经使用过。我们先定义一个泛型类,同时在这个类中定义getNext()、setNext、getElement()、setElement这些方法,便于后面代码的实现。
    public class LinearNode<T> {
        private LinearNode<T> next;
        private T element;
        //----------------------------------------------------------------
        //  Creates an empty node.
        // ----------------------------------------------------------------
        public LinearNode() {
            next = null;
            element = null;
        }
        //----------------------------------------------------------------
        //  Creates a node storing the specified element.
        // ----------------------------------------------------------------
        public LinearNode (T elem) {
            next = null;
            element = elem;
        }
        //----------------------------------------------------------------
        //  Returns the node that follows this one.
        // ----------------------------------------------------------------
        public LinearNode<T> getNext() {
            return next;
        }
        //----------------------------------------------------------------
        //  Sets the node that follows this one.
        // ----------------------------------------------------------------
        public void setNext (LinearNode<T> node) {
            next = node;
        }
        //----------------------------------------------------------------
        //  Returns the element stored in this node.
        // ----------------------------------------------------------------
        public T getElement() {
            return element;
        }
        //----------------------------------------------------------------
        //  Sets the element stored in this node.
        // ----------------------------------------------------------------
        public void setElement (T elem) {
            element = elem;
        }
    
    }
    
    
    • dequeue方法的实现
      这个方法是从队列中弹出一个元素,队列对元素的处理规则是“先进先出”,所以弹出的元素应该是位于队列中的第一个元素。先判断这个队列是否为空,如果队列为空,则弹出信息显示队列为空。如果不是为空的话,则获取第一个元素返回出来,再将front指针指向下一个元素,count规模减一。
            if (count == 0) {
                System.out.println("队列为空");
                return null;
            }
            else {
                T t = front.getElement();
                front = front.getNext();
                count--;
                return t;
            }
    
    
    • first方法的实现
      返回队列中第一个元素,比较容易实现,如队列为空的话就显示信息提示队列为空。
             if (count == 0) {
                System.out.println("队列为空");
                return null;
            }
            else {
                T t = front.getElement();
                return t;
            }
    
    
    • isEmpty方法的实现
      这个方法判定队列是否为空,如果count为0,则队列为空返回true,否则返回false。
    if (count == 0)
                return true;
            else
                return false;
    
    
    • toString方法的实现
      toString方法是将队列中的各个元素转换成String类型,然后便于打印出来。我参考教材中第十四章中ArrayStack方法中的toString方法,先定义一个String类型的空result,再利用for循环将队列中的元素遍历返回,赋给result,返回result。
     String result = "";
            LinearNode<T> current = front;
            for(int index=count-1; index>=0;index--){
                result = result + (current.getElement()).toString() + "
    ";
                current = current.getNext();
            }
            return result;
    
    

    2、单步跟踪

  • 相关阅读:
    L1-021 重要的话说三遍
    L1-020 帅到没朋友
    pytest--钩子
    pytest--allure
    pytest--常用插件
    pytest--高级用法
    pytest--配置文件
    pytest--控制运行
    pytest--fixture
    pytest--使用前提
  • 原文地址:https://www.cnblogs.com/lyxwatm/p/7674904.html
Copyright © 2020-2023  润新知