20162327WJH使用队列:模拟票务站台代码分析
20162327WJH使用队列:模拟票务站台代码分析
用链队实现队列的情况
1、用链表实现队列的代码
- 关键方法代码及补全代(LinkedOueue类)
public void enqueue(T element) {
LinearNode<T> node = new LinearNode<T>(element);
if (count==0)
front = node;
else
rear.setNext(node);
rear = node;
count++;
}
public T dequeue() throws Exception {
if (count==0)
throw new Exception("队列是空的!");
T result = front.getElement();
front = front.getNext();
count--;
if (isEmpty())
rear = null;
return result;
}
public T first() throws Exception {
if (count==0)
throw new Exception("队列是空的!");
return front.getElement();
}
public boolean isEmpty() {
return (count == 0);
}
public int size() {
return count;
}
public String toString() {
String result = "<top of Queue>\n";
LinearNode current = front;
while (current != null)
{
result = result + (current.getElement()).toString() + "\n";
current = current.getNext();
}
return result + "<bottom of Queue>";
}
}
- 节点类
public class LinearNode<T> {
private LinearNode<T> next;
private T element;
public LinearNode() {
next = null;
element = null;
}
public LinearNode(T elem) {
next = null;
element = elem;
}
public LinearNode<T> getNext() {
return next;
}
public void setNext(LinearNode<T> node) {
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
}
2、用IDEA进行单步跟踪
3、遇到的问题及解决过程
二、用循环数组实现队列
1、用链表实现队列的代码
- 关键方法代码及补全代码(CircularArrayQueue类)
public void enqueue (T element){
if(count == queue.length){
expandCapacity();
queue[rear] = element;
rear = (rear+1) % queue.length;
count++;
}
}
public void expandCapacity()
{
T[]larger = (T[])(new Object[queue.length*2]);
for(int index = 0;index<count;index++){
larger[index] = queue[(front + index) % queue.length];
front = 0;
rear = count;
queue = larger;
}
}
public T dequeue() throws Exception {
if (count == 0)
throw new Exception("错误代码!");
T a = queue[front];
queue[front] = null;
front = (front + 1) % queue.length;
count--;
return a;
}
2、用IDEA进行单步跟踪
3、遇到的问题及解决过程
20162327WJH使用队列:模拟票务站台代码分析的更多相关文章
- Kafka Producer相关代码分析【转】
来源:https://www.zybuluo.com/jewes/note/63925 @jewes 2015-01-17 20:36 字数 1967 阅读 1093 Kafka Producer相关 ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- Device Tree(三):代码分析
一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请参考引入Device Tree的原因) 2.Device Tree的基础概 ...
- AngularJS PhoneCat代码分析
转载自:http://www.tuicool.com/articles/ym6Jfen AngularJS 官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些 ...
- 【转】Device Tree(三):代码分析
原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...
- Codeforces 704A Thor 队列模拟
题目大意:托尔有一部手机可执行三种操作 1.x APP产生一个新消息 2.读取x App已产生的所有消息 3.读取前t个产生的消息 问每次操作后未读取的消息的数量 题目思路: 队列模拟,坑点在于竟然卡 ...
- 手机自动化测试:Appium源码分析之跟踪代码分析五
手机自动化测试:Appium源码分析之跟踪代码分析五 手机自动化测试是未来很重要的测试技术,作为一名测试人员应该熟练掌握,POPTEST举行手机自动化测试的课程,希望可以训练出优秀的手机测试开发工 ...
- 虚拟机创建流程中neutron代码分析(三)
前言: 当neutron-server创建了port信息,将port信息写入数据库中.流程返回到nova服务端,接着nova创建的流程继续走.在计算节点中neutron-agent同样要完成很多的工作 ...
- (五) vivi代码分析
目录 vivi代码分析 初始化注册 使用open/read/ioctl 系统调用分析 ioctl流程一览 总结 title: vivi代码分析 date: 2019/4/23 19:30:00 toc ...
随机推荐
- aarch64_c2
collectd-5.7.2-5.fc26.aarch64.rpm 2017-06-18 21:17 634K fedora Mirroring Project collectd-amqp-5.7.2 ...
- Linux下简单粗暴使用rsync实现文件同步备份【转】
这篇来说说如何安全的备份,还有一点不同的是上一篇是备份服务器拉取数据,这里要讲的是主服务器如何推送数据实现备份. 一.备份服务器配置rsync文件 vim /etc/rsyncd.conf #工作中指 ...
- 10款常见MySQL高可用方案选型解读【转】
我们在考虑MySQL数据库的高可用架构时,主要考虑如下几方面: 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据库的故障而中断. 用作备份. ...
- TCP/IP详解(整理)
1.概述 路由器是在网络层进行联通,而网桥是在链路层联通不同的网络. IP层用ICMP来与其他主机或路由器交换错误报文和其他的重要信息.应用程序也可以访问ICMP,两个诊断工具:Ping和Tracer ...
- Jenkins关联GitHub进行构建
一.创建一个自由风格的项目 并在高级中勾选你构建完成后保存项目的路径 二.配置你存放代码的GitHub的地址并添加用户名密码 三.立即构建
- mysql -> 事务&事务锁_09
事务的特性 redo undo 锁的隔离级别
- mac 升级10.12 php debug 环境 跑不起的解决 解决方案
1: mac 升级后发现 php从原来的5.5 升级为 5.6 了... 所以以前 php.ini 里面的配置全部都没有了. mac 给我们做了备份2: 没办法只能升级php对应的插件到5. ...
- 浅谈js设计模式之策略模式
策略模式有着广泛的应用.本节我们就以年终奖的计算为例进行介绍. 很多公司的年终奖是根据员工的工资基数和年底绩效情况来发放的.例如,绩效为 S的人年终奖有 4倍工资,绩效为 A的人年终奖有 3倍工资,而 ...
- 下一代Android打包工具,100个渠道包只需要10秒钟 https://github.com/mcxiaoke
https://github.com/mcxiaoke/packer-ng-plugin https://github.com/Meituan-Dianping/walle https://githu ...
- oralce 笔记
查某一表的行数 select max(rownum) from tablename 插入数据之前判断是否重复 insert into tablename (coloum1,coloum2) selec ...