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 ...
随机推荐
- 初涉sqlmap
1.基本注入(这个工具kali或者bt下面有集成的,这里附加一个window免py版,提取码:3ldv) sqlmap -u http://url/xx.php?id=1 判断注入 sqlmap - ...
- Spring4笔记5--基于注解的DI(依赖注入)
基于注解的DI(依赖注入): 对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例.只需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解. < ...
- WCF REST 工作总结
首先引用System.ServiceModel;System.ServiceModel;System.ServiceModel.Activation;命名空间 [ServiceContract] pu ...
- XSS注入常用语句
<script>alert('hello,gaga!');</script> //经典语句,哈哈! >"'><img src="javas ...
- Python缓存技术,装x新高度。
一段非常简单代码 普通调用方式 def console1(a, b): print("进入函数") return (a, b) print(console1(3, 'a')) pr ...
- linux查看内存、CPU占用资源最多的进程
[内存占用] #利用ps命令,默认使用ps参数会显示的结果 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 ...
- 虚拟机 ubuntu 16.04
下载地址:https://www.ubuntu.com/download/desktop 使用虚拟机直接安装
- P1183 多边形的面积
一道睡论数论题 其实是AC300祭才做的水题 题意: 很直白的的题意啊,就是求任意一个多边形的面积 所以我们来安利一下一个求多边形面积的数学通式: 给定多边形的顶点坐标(有序),让你来求这个多边形的面 ...
- How to omit h1 title heading in HTML export
How to omit h1 title heading in HTML export */--> Introduce how to omit h1 title in the exported ...
- Windows 10利用自带的 Hyper-v 安装Linux
Linux由于其众多独特的优势(可参见Linux系统的优势),而被很多人所喜爱.而要使用Linux那首先要做的工作就是安装Linux系统了.这里给出在 win10 下利用虚拟机 Hyper-v 安装 ...