3、队列
来源:互联网 发布:2016年淘宝类目销售额 编辑:程序博客网 时间:2024/06/11 21:13
1、队列的基本实现
public class MyQueue01 { //数组 private int[] array; //数组的最大长度 private int maxSize; //队列的头 private int head; //队列的尾 private int tail; //有效数据的长度 private int elems; public MyQueue01(int size){ this.maxSize = size; array = new int[size]; head = 0; tail = -1; elems = 0; } //插入数据 public void insert(int value){ elems++; array[++tail] = value; } //删除数据 public int remove(){ elems--; return array[head++]; } //是否已满 public boolean isFull(){ return (elems == (maxSize-1)); } //是否为空 public boolean isEmpty(){ return elems==0; } //返回有效的长度 public int getSize(){ return elems; }}
测试:
public class TestMyQueue01 { public static void main(String[] args) { MyQueue01 myQueue01 = new MyQueue01(5); if(!myQueue01.isFull()){ myQueue01.insert(1); myQueue01.insert(2); myQueue01.insert(3); myQueue01.insert(4); myQueue01.insert(5); } while (!myQueue01.isEmpty()){ System.out.println(myQueue01.remove()); } // 不能再插入了,因为已经满了,头跟尾都已经到数组的最后了 // myQueue01.insert(6); }}
2、循环队列的实现
public class MyQueue02 { //数组 private int[] array; //数组的最大长度 private int maxSize; //队列的头 private int head; //队列的尾 private int tail; //有效数据的长度 private int elems; public MyQueue02(int size){ this.maxSize = size; array = new int[size]; head = 0; tail = -1; elems = 0; } //插入数据 public void insert(int value){ //判断尾指针是否已经到队列的数据尾部,如果到了,就回到-1的地方 if(tail == maxSize-1){ tail = -1; } elems++; array[++tail] = value; } //删除数据 public int remove(){ int tmp = array[head++]; if(head == maxSize){ head = 0; } elems--; return tmp; } //是否已满 public boolean isFull(){ System.out.println("此时的元素个数为:"+elems); System.out.println("tail="+tail); return (tail == (maxSize-1)); } //是否为空 public boolean isEmpty(){ return elems==0; } //返回有效的长度 public int getSize(){ return elems; }}
测试:
public class TestMyQueue02 { public static void main(String[] args) { MyQueue02 myQueue02 = new MyQueue02(5); myQueue02.insert(10); myQueue02.insert(20); myQueue02.insert(30); myQueue02.insert(40); myQueue02.insert(50); System.out.println(myQueue02.isFull()); System.out.println(myQueue02.isEmpty());// myQueue02.insert(100); while (!myQueue02.isEmpty()){ System.out.println(myQueue02.remove()); } }}
3、有序队列
public class MyQueue03 { //数组 private int[] array; //数组的最大长度 private int maxSize; //有效数据的长度 private int elems; public MyQueue03(int size){ this.maxSize = size; array = new int[size]; elems = 0; } //插入数据 public void insert(int value){ int i; for(i=0; i<elems; i++){ if(array[i] < value) break; } for(int j=elems; j>i; j--){ array[j] = array[j-1]; } array[i] = value; elems++; } //删除数据 public int remove(){ return array[--elems]; } //是否已满 public boolean isFull(){ return (elems == maxSize); } //是否为空 public boolean isEmpty(){ return elems==0; } //返回有效的长度 public int getSize(){ return elems; }}
测试:
public class TestMyQueue03 { public static void main(String[] args) { MyQueue03 myQueue03 = new MyQueue03(5); myQueue03.insert(10); myQueue03.insert(20); myQueue03.insert(30); myQueue03.insert(40); myQueue03.insert(50); while (!myQueue03.isEmpty()){ System.out.print(myQueue03.remove()+" "); } }}
阅读全文
0 0
- 3 队列
- 3、队列
- [caioj]单调队列3 单调队列
- [caioj]单调队列3 单调队列
- 数据结构基础(3)-->队列
- 3、SystemV 消息队列
- 队列练习 3
- 循环队列实验3
- 数据结构基础3:队列
- Codevs_P3187 队列练习3
- cheet 3 队列
- Cheet 3 队列
- 动画队列3
- 18 消息队列3
- 队列学习(3)
- 栈与队列(3)
- (3)队列
- 实验3 队列
- Android 退出程序的方式
- Linux文件系统的硬链接与软链接
- 薇辰语音变声助手 安卓变声软件破解版
- jquery事件
- 多年没玩汇编,最近在熟悉MK7A23 单片机,下面是我的学习方法
- 3、队列
- Java8实战教学视频
- python下的Memcache操作
- angularjs的自定义指令及其调用方式
- 《Flask Web开发》读书笔记(4)第4章Web表单(关键词:Web开发/Flask/表单/Web表单)
- G
- Mvp框架(完美实现P层剥离)+基类activity、fragment剥离 MVP demo
- 领域模型-实体对象和值对象
- 图片快速居中在div中