队列(顺序存储)代码
2016-04-29 08:57
155 查看
publicclassArrayQueue<T>{
privateT[]queue;
privatestaticintsize=20;//队列容量
privateintfront,rear;//队首、队尾下标
publicArrayQueue(){
queue=(T[])newObject[size];
front=0;
rear=0;
}
publicvoidadd(Tt)throwsException
{
if((rear+1)%size==front)
thrownewException("overflow!");
rear=(rear+1)%size;
queue[rear]=t;
}
publicTpoll()throwsException
{
if(front==rear)
thrownewException("underflow!");
front=(front+1)%size;
returnqueue[front];
}
publicbooleanisEmpty(){
returnfront==rear;
}
}
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
ArrayQueue<String>q=newArrayQueue<String>();
q.add("a");
q.add("b");
q.add("c");
q.add("d");
q.add("e");
q.add("f");
q.add("g");
q.add("h");
q.add("i");
q.add("j");
q.add("k");
q.add("l");
q.add("m");
while(!q.isEmpty()){
Stringtemp=q.poll();
System.out.print(temp);
}
}
}
输出
abcdefghijklm
相关文章推荐
- polygon-Graveyard(poj3154)
- LinkedList详解
- 简单学习Python time模块
- QT学习之 布局管理
- JVM内存的设置(解决eclipse下out of memory问题)
- JAVAEE项目结构以及并发随想
- java项目中经常碰到的内存溢出问题: java.lang.OutOfMemoryError: PermGen space, 堆内存和非堆内存,写的很好,理解很方便
- java中LinkedList的实现
- Java开发中的23种设计模式详解(转)
- ArrayList类详解
- java初学者:构造方法 技术文章
- JDBC(Java Data Base Connectivity)
- spring大文件分块上传原理解析
- 序列化与反序列化
- 在Java里处理文件的技巧
- java基础练习题
- NTP 服务进程修复了一大波安全漏洞,请尽快升级
- java perf
- 不知道是什么(ting)(shuo)效率很高的代码
- Asp.net 面向接口可扩展框架之类型转化基础服务