Java 队列
来源:互联网 发布:linux 进程调度命令 编辑:程序博客网 时间:2024/05/21 07:11
循环队列:
package com.darren.test.datastructure;/** * 此队列为循环队列。 不设标志位,少使用一个存储空间,约定队头指针在队尾指针的下一位时作为满状态。 * 此时如果队头指针和队尾指针相等,作为空状态。 * * @author Darren.Zhang */public class Queue { private int maxSize; private int[] data; private int front; private int end; public Queue(int size) { this.maxSize = size; data = new int[size]; } /** * 添加数据,并移动指针 * * @param value */ public void addValue(int value) { if ((end + 1) % maxSize == front) { System.out.println("队列已满!"); } else { data[end] = value; // 指针后移一位 end = (end + 1) % maxSize; } } /** * 取出数据,并移动指针 * * @return */ public Integer getValue() { Integer value = null; if (front == end) { System.out.println("队列已空!"); } else { value = data[front]; // 指针后移一位 front = (front + 1) % maxSize; } return value; } /** * 查看数据,不移动指针 * * @return */ public Integer peek() { Integer value = null; if (front == end) { System.out.println("队列已空!"); } else { value = data[front]; } return value; } /** * 计算队列大小 * * @return */ public int size() { int size = (end - front + maxSize) % maxSize; return size; }}循环队列测试:
package com.darren.test.datastructure;public class QueueTest { public static void main(String[] args) { Queue queue = new Queue(5); queue.addValue(5); queue.addValue(4); queue.addValue(3); queue.addValue(2); queue.addValue(1); // test1(queue); // test2(queue); test3(queue); } /** * 测试方式一 * * @param queue */ private static void test1(Queue queue) { System.out.println("测试方式一"); while (true) { Integer value = queue.getValue(); if (value != null) { System.out.println(value); } else { break; } } } /** * 测试方式二 * * @param queue */ private static void test2(Queue queue) { System.out.println("测试方式二"); int size = queue.size(); for (int i = 0; i < size; i++) { System.out.println(queue.getValue()); } } /** * 测试方式三 * * @param queue */ private static void test3(Queue queue) { System.out.println("测试方式三"); while (queue.peek() != null) { System.out.println(queue.getValue()); } }}
优先级队列:
package com.darren.test.datastructure;/** * 队头小队尾大 * * @author Darren.Zhang */public class PriorityQueue { private int maxSize; private int[] data; private int length; public PriorityQueue(int size) { this.maxSize = size; data = new int[size]; } public void addValue(int value) { if (length == maxSize) { System.out.println("优先队列已满!"); } else if (length == 0) { data[length++] = value; } else { int i = length - 1; for (; i >= 0; i--) { if (value > data[i]) { data[i + 1] = data[i]; } else { break; } } data[i + 1] = value; length++; } } public Integer getValue() { Integer value = null; if (length == 0) { System.out.println("优先队列已空!"); } else { value = data[--length]; } return value; } public Integer peek() { Integer value = null; if (length == 0) { System.out.println("优先队列已空!"); } else { value = data[length - 1]; } return value; } public int size() { return length; }}
优先级队列测试:
package com.darren.test.datastructure;public class PriorityQueueTest { public static void main(String[] args) { PriorityQueue priorityQueue = new PriorityQueue(5); priorityQueue.addValue(4); priorityQueue.addValue(5); priorityQueue.addValue(3); priorityQueue.addValue(1); priorityQueue.addValue(2); priorityQueue.addValue(0); // test1(priorityQueue); // test2(priorityQueue); test3(priorityQueue); } /** * 测试方式一 * * @param priorityQueue */ private static void test1(PriorityQueue priorityQueue) { System.out.println("测试方式一"); while (true) { Integer value = priorityQueue.getValue(); if (value != null) { System.out.println(value); } else { break; } } } /** * 测试方式二 * * @param priorityQueue */ private static void test2(PriorityQueue priorityQueue) { System.out.println("测试方式二"); int size = priorityQueue.size(); for (int i = 0; i < size; i++) { System.out.println(priorityQueue.getValue()); } } /** * 测试方式三 * * @param priorityQueue */ private static void test3(PriorityQueue priorityQueue) { System.out.println("测试方式三"); while (priorityQueue.peek() != null) { System.out.println(priorityQueue.getValue()); } }}
0 0
- java队列
- java 队列
- java队列
- java 队列
- java队列
- java 队列
- java--队列
- Java 队列
- java队列
- java 队列
- java队列
- Java - 队列
- java队列
- java 队列
- java 队列
- Java队列
- 队列(JAVA)
- Java 队列
- 题目1034:寻找大富翁
- 【Spark亚太研究院系列丛书】Spark实战高手之路-第一章 构建Spark集群(第五步)(8)
- eclipse启动tomcat, http://localhost:8080无法访问
- 漫谈程序员系列:谁是为加班而生的
- iphone6屏幕适配
- Java 队列
- 设计模式-单件模式
- ANT命令总结
- 数据结构——单链表的基本操作
- android开发工具
- iOS 高级开发技巧 1.熟悉Objective-C (四) 多用类型常量 少用#define预处理命令
- javaweb-通过servlet获取请求参数
- ScrollView嵌套ListView会出现的ListView底部会自动出现在屏幕上
- Google技术之 mod_Pagespeed 网页优化探索(1)