• 2017-2018-1 20162307 队列加分项


    2017-2018-1 20162307 队列加分项

    要求

    1 用实现循环队列
    2 参考PPT用循环队列打印杨辉三角
    3 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息
    4 把代码推送到代码托管平台
    5 把完成过程写一篇博客:重点是单步跟踪过程和遇到的问题及解决过程
    6 提交博客链接

    解题步骤

    • 1.查看PPT

    1.想象队列为一个环形,让【0】在【N-1】之后
    2.分析杨辉三角N行元素与N-1之间关系

    • 2.参考15.6 CircularArrayQueue

        public class CircularArrayQueue<T> implements Queue<T>
        {
            private final int DEFAULT_CAPACITY = 100;
            private int front, rear, count;
            private T[] queue;
            private T element;
      
          public CircularArrayQueue()
            {
                front = rear = count = 0;
                queue = (T[]) (new Object[DEFAULT_CAPACITY]);
            }
      
      
      
            public CircularArrayQueue (int initialCapacity)
            {
                front = rear = count = 0;
                queue = ( (T[])(new Object[initialCapacity]) );
            }
      
            public void enqueue (T element)
            {
            if (size() == queue.length)
                    expandCapacity();
      
                queue[rear] = element;
                rear = (rear+1) % queue.length;
      
                count++;
            }
      
      
            public T dequeue() throws EmptyCollectionException
            {
                if (isEmpty())
                    throw new EmptyCollectionException ("queue");
      
                T result = queue[front];
                queue[front] = null;
                front = (front+1) % queue.length;
      
                count--;
      
                return result;
            }
      
      
      
            public T getElement()
            {
                return element;
            }
      
            public boolean isEmpty()
            {
                return (count == 0);
        }
      
      
            public int size()
            {
                return count;
            }
      
            public String toString()
            {
                String result = "";
                int scan = 0;
      
                while(scan < count)
                {
                    if(queue[scan]!=null)
                    {
                        result += queue[scan].toString()+"
      ";
                    }
                    scan++;
                }
      
                return result;
            }
      
            public void expandCapacity()
        {
                T[] larger = (T[])(new Object[queue.length *2]);
      
                for(int scan=0; scan < count; scan++)
                {
                    front=(front+1) % queue.length;
                }
      
                front = 0;
                rear = count;
                queue = larger;
            }
      
            public T[] first() {
                return queue.clone ();
            }
        }
      
      1. 杨辉三角(Pascal)

  • 相关阅读:
    java 语言里 遍历 collection 的方式
    struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
    mysql 查看表的类型
    memcached—向memcached中保存Java实体需注意的问题
    一个关于 UIPickerView 的 bug
    Wireshark数据抓包教程之安装Wireshark
    Mysql第四天 数据库设计
    产品经理怎样才干把一件事做出色
    Tokyo Tyrant(TTServer)系列(三)-Memcache协议
    Unity3D
  • 原文地址:https://www.cnblogs.com/Tiffany23/p/7707432.html
Copyright © 2020-2023  润新知