队列课下作业

实验要求

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

实验过程

一、补充教材15.5代码。

  • 1、dequeue方法:
    按照队列中元素先进先出(FIFO)的方式,dequeue从队头删除元素,先查询队列是否为空,若为空,则直接打印;若不为空,则返回第一个元素,指针指向下一元素。

  • 代码如下:
    public T dequeue() throws Stack.EmptyCollectionException {
    if (count == 0) {
    System.out.println("The queue is empty");
    return null;
    }
    else {
    T result = front.getElement();
    front = front.getNext();
    count--;
    return result;
    }
    }

  • 2、first方法:
    检测队列中的第一个元素,先判断队列是否为空,若为空就提示队列为空;若不为空,则返回队列的第一个元素。

  • 代码如下:
    public T first() throws Stack.EmptyCollectionException {
    if (count == 0){
    throw new Stack.EmptyCollectionException("The queue is empty.");
    }
    return front.getElement();
    }

  • 3、isEmpty方法:
    判断队列是否为空,为空则返回true,不为空则返回false

  • 代码如下:
    public boolean isEmpty() {
    return count == 0;
    }

  • 4、toString方法:
    将队列中各元素转化为String类型

  • 代码如下:
    public String toString(){
    String result = "";
    LinearNode current = front;

          while (current != null)
          {
          result = result + (current.getElement()).toString() + "\n";
              current = current.getNext();
          }
          return result;
      }
    

    }

运行截图和单步跟踪如下


posted @ 2017-10-22 23:26  20162306陈是奇  阅读(113)  评论(0编辑  收藏  举报