2007-10-12
线性表之队列的实现
关键字: 队列/线性表java 代码
- /**
- * Queue.java
- * 线性表之队列
- * 队列有如下特点:
- * 先进先出
- * 即,从尾部添加(push)新数据
- * 从头部取出(pop)数据
- */
- /**
- * 队列(Queue)也是一种运算受限的线性表。
- * 它只允许在表的一端进行插入,而在另一端进行删除。
- * 允许删除的一端称为队头(front),允许插入的一端称为队尾(rear)。
- */
- package line;
- /**
- * @author sunxboy
- * 9:59:59 AM May 22, 2007
- */
- public class Queue {
- int data[];
- int maxSize;
- int size;
- int front; //允许删除的一端
- int rear; //允许插入的一端
- public Queue(int maxSize) {
- this.maxSize = maxSize;
- this.data = new int[maxSize];
- size= 0;
- rear = 0;
- front =0;
- }
- public boolean isEmpty() {
- return size==0;
- }
- public boolean isFull() {
- return size==maxSize;
- }
- /**
- * 循环队列
- * @param data
- * @return
- */
- public boolean push(int data)throws Exception {
- if(isFull())
- throw new Exception("队列已满!");
- size++;
- this.data[rear]= data;
- // if(rear+1==maxSize)
- // rear=0;
- // else
- // rear++;
- rear=(rear+1)%maxSize;
- return true;
- }
- public int pop() throws Exception{
- int temp;
- if(isEmpty())
- throw new Exception("队列是空的.");
- temp = this.data[this.front];
- this.size--;
- // if(front+1==maxSize)
- // front=0;
- // else
- // front++;
- front=(front+1)%maxSize;
- return temp;
- }
- public static void main(String[] args) throws Exception{
- Queue queue=new Queue(10);
- queue.push(1);
- queue.pop();
- // queue.pop();
- queue.push(2);
- queue.push(3);
- queue.push(4);
- queue.push(5);
- queue.push(6);
- queue.push(7);
- queue.push(8);
- queue.push(9);
- queue.push(10);
- // queue.push(11);
- while(!queue.isEmpty())
- {
- System.out.println(queue.pop());
- }
- }
- }
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 230279 次
- 性别:

- 来自: 深圳

- 详细资料
搜索本博客
我的相册
b48abcac33f225a880bb1b3a5950b3d5273e6852.jpg
共 10 张
共 10 张
最近加入圈子
最新评论
-
Chrome开发团队曝光 多人 ...
很好很强大
-- by jasin2008 -
用javascript与java进行RS ...
好强啊,谢谢了
-- by wv1124 -
分享下ubuntu 7.10的界面
把这些东西组装一下就于是有了我们的联想~
-- by citi.sh -
使用prototype.js选择选中 ...
用图片模拟实现超漂亮的选框checkbox效果 http://www.csspl ...
-- by goagrass -
名言系列(三)
如果想要获得成功,那么就需要对一个领域足够了解,热爱这个行业并保持热情.“如果想 ...
-- by sunxboy






评论排行榜