顺序队列(数组实现)
2015-10-05 19:10
239 查看
下面利用数组实现的循环队列,并考虑了上溢出和下溢出。
public class Queue { private int[] queue; private static final int defaultSize = 100; private int size; private int tail, head; public Queue() { setUp(defaultSize); } public Queue(int sz) { setUp(sz); } public void setUp(int sz) { size = sz; tail = 0; head = 0; queue = new int[size]; } public void enqueue(int x) throws Exception { if(isFull()) throw new Exception("overflow"); else { queue[tail%size] = x; tail++; } } public int dequeue() throws Exception { if(isEmpty()) throw new Exception("underflow"); else { int val = queue[head%size]; head++; return val; } } public int peek() throws Exception { if(isEmpty()) throw new Exception("underflow"); else return queue[head%size]; } public boolean isEmpty() { return tail == head; } public boolean isFull() { return (tail + 1)%size == head%size; //设置浪费一个存储空间单元,即head所指的位置仍空闲。 } public void clear() { tail = head = 0; } }
相关文章推荐
- hdu 1240 Asteroids! (bfs)
- java入门:JAVA和.net不同之处
- 快速幂的基本概念
- LaTeX中表格单元格中内容自动换行 (Wrap text)问题
- Andorid Async-HttpClient阅览
- mfc自定义工具栏如何锁定
- 44. Wildcard Matching (String; Recursion)
- iOS编程-------UINavigationController / 界面间通信(传值)
- HDU 4261 Estimation(set预处理中位数+dp+缺页中断t个爽)
- 10.1训练赛--2014上海区域赛--- World Cup**
- 开发人员常用的10个JavaScript库
- 随笔---2015/10/05
- 小宇宙,爆发吧
- 北京Uber优步司机奖励政策(10月5日~10月11日)
- Javascript的异常处理介绍
- Java虚拟机内存模型
- GO 和 KEGG 注释之前,为什么要先进行序列比对(BLAST)?
- 【数据结构】线性结构:存储&运算&时间复杂度
- 把笔记本电脑的无线网络通过网线共享给台式机
- 病毒侵袭---hdu2896(AC自动机)