Queue实现
1.Queue接口:
- public interface Queue<E> {
- int getSize();
- boolean isEmpty();
- void enqueue(E e);
- E dequeue();
- E getFront();
- }
2.使用Array数组实现ArrayQueue:
- public class ArrayQueue<E> implements Queue<E> {
- private Array<E> array;
- public ArrayQueue(int capacity){
- array = new Array<>(capacity);
- }
- public ArrayQueue(){
- array = new Array<>();
- }
- @Override
- public int getSize(){
- return array.getSize();
- }
- @Override
- public boolean isEmpty(){
- return array.isEmpty();
- }
- public int getCapacity(){
- return array.getCapacity();
- }
- @Override
- public void enqueue(E e){
- array.addLast(e);
- }
- @Override
- public E dequeue(){
- return array.removeFirst();
- }
- @Override
- public E getFront(){
- return array.getFirst();
- }
- @Override
- public String toString(){
- StringBuilder res = new StringBuilder();
- res.append("Queue: ");
- res.append("front [");
- for(int i = 0 ; i < array.getSize() ; i ++){
- res.append(array.get(i));
- if(i != array.getSize() - 1)
- res.append(", ");
- }
- res.append("] tail");
- return res.toString();
- }
- public static void main(String[] args){
- ArrayQueue<Integer> queue = new ArrayQueue<>();
- for(int i = 0 ; i < 10 ; i ++){
- queue.enqueue(i);
- System.out.println(queue);
- if(i % 3 == 2){
- queue.dequeue();
- System.out.println(queue);
- }
- }
- }
- }
3.使用LinkedList链表实现LinkedListQueue:
- public class LinkedListQueue<E> implements Queue<E> {
- private class Node{
- public E e;
- public Node next;
- public Node(E e, Node next){
- this.e = e;
- this.next = next;
- }
- public Node(E e){
- this(e, null);
- }
- public Node(){
- this(null, null);
- }
- @Override
- public String toString(){
- return e.toString();
- }
- }
- private Node head, tail;
- private int size;
- public LinkedListQueue(){
- head = null;
- tail = null;
- size = 0;
- }
- @Override
- public int getSize(){
- return size;
- }
- @Override
- public boolean isEmpty(){
- return size == 0;
- }
- @Override
- public void enqueue(E e){
- if(tail == null){
- tail = new Node(e);
- head = tail;
- }
- else{
- tail.next = new Node(e);
- tail = tail.next;
- }
- size ++;
- }
- @Override
- public E dequeue(){
- if(isEmpty())
- throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
- Node retNode = head;
- head = head.next;
- retNode.next = null;
- if(head == null)
- tail = null;
- size --;
- return retNode.e;
- }
- @Override
- public E getFront(){
- if(isEmpty())
- throw new IllegalArgumentException("Queue is empty.");
- return head.e;
- }
- @Override
- public String toString(){
- StringBuilder res = new StringBuilder();
- res.append("Queue: front ");
- Node cur = head;
- while(cur != null) {
- res.append(cur + "->");
- cur = cur.next;
- }
- res.append("NULL tail");
- return res.toString();
- }
- public static void main(String[] args){
- LinkedListQueue<Integer> queue = new LinkedListQueue<>();
- for(int i = 1 ; i < 11 ; i ++){
- queue.enqueue(i);
- System.out.println(queue);
- if(i % 3 == 0){
- queue.dequeue();
- System.out.println(queue);
- }
- }
- }
- }
4.实现循环队列LoopQueue:
- public class LoopQueue<E> implements Queue<E> {
- private E[] data;
- private int front, tail;
- private int size;
- public LoopQueue(int capacity){
- data = (E[])new Object[capacity + 1];
- front = 0;
- tail = 0;
- size = 0;
- }
- public LoopQueue(){
- this(10);
- }
- public int getCapacity(){
- return data.length - 1;
- }
- @Override
- public boolean isEmpty(){
- return front == tail;
- }
- @Override
- public int getSize(){
- return size;
- }
- @Override
- public void enqueue(E e){
- if((tail + 1) % data.length == front)
- resize(getCapacity() * 2);
- data[tail] = e;
- tail = (tail + 1) % data.length;
- size ++;
- }
- @Override
- public E dequeue(){
- if(isEmpty())
- throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
- E ret = data[front];
- data[front] = null;
- front = (front + 1) % data.length;
- size --;
- if(size == getCapacity() / 4 && getCapacity() / 2 != 0)
- resize(getCapacity() / 2);
- return ret;
- }
- @Override
- public E getFront(){
- if(isEmpty())
- throw new IllegalArgumentException("Queue is empty.");
- return data[front];
- }
- private void resize(int newCapacity){
- E[] newData = (E[])new Object[newCapacity + 1];
- for(int i = 0 ; i < size ; i ++)
- newData[i] = data[(i + front) % data.length];
- data = newData;
- front = 0;
- tail = size;
- }
- @Override
- public String toString(){
- StringBuilder res = new StringBuilder();
- res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
- res.append("front [");
- for(int i = front ; i != tail ; i = (i + 1) % data.length){
- res.append(data[i]);
- if((i + 1) % data.length != tail)
- res.append(", ");
- }
- res.append("] tail");
- return res.toString();
- }
- public static void main(String[] args){
- LoopQueue<Integer> queue = new LoopQueue<>(5);
- for(int i = 0 ; i < 10 ; i ++){
- queue.enqueue(i);
- System.out.println(queue);
- if(i % 3 == 2){
- queue.dequeue();
- System.out.println(queue);
- }
- }
- }
- }
Queue实现的更多相关文章
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
随机推荐
- Docker——概述
出现原因:开发接替运维的工作,将jar包连同(mysql,jdk)等环境上线 实现:java -> jar(环境) -> 打包项目带上环境(镜像) -> (Docker仓库:商店) ...
- Linux巡检检查项
不定时更新...... 1)服务器 1.1 SELINUX检查(sestatus) 1.2 资源限制检查(ulimit -a) 1.3 最近登录(last) 1.4 操作系统版本(cat /etc/r ...
- python写一个web目录扫描器
用到的模块urliib error #coding = utf-8 #web目录扫描器 by qianxiao996 #博客地址:https://blog.csdn.net/qq_36374896 i ...
- 变量 Java day 5
Java 第五天的学习 变量 变量注意事项 变量的底层 ASCII编码表 1.什么是变量? 概念:变量及代数. 在Java中,变量分为两种:基本类型的变量和引用类型的变量 1>基本类型的变量:必 ...
- Linux开发板(树莓派)和服务器进行双向通信(socket)
前言 物联网是目前嵌入开发必备的属性之一,我们常常需要把自己开发板和云端进行交互,本篇博文就记录一下. 使用Socket来实现Linux开发板和服务器进行双向通信,Python中是默认集成了s ...
- kafka 保证消息被消费和消息只消费一次
1. 保证消息被消费 即使消息发送到了消息队列,消息也不会万无一失,还是会面临丢失的风险. 我们以 Kafka 为例,消息在Kafka 中是存储在本地磁盘上的, 为了减少消息存储对磁盘的随机 I/O, ...
- (转)Linux的文件权限与目录配置
ref:https://www.cnblogs.com/ysocean/p/7712412.html#_label1_0 (转)Linux文件权限和目录配置 1.Linux命令的普遍语法格式 命令格式 ...
- SpringBoot与SpringCloud的关系与区别?
一.SpringBoot和SpringCloud简介 1.SpringBoot:是一个快速开发框架,通过用MAVEN依赖的继承方式,帮助我们快速整合第三方常用框架,完全采用注解化(使用注解方式启动Sp ...
- linux发布常用命令
一.linux发布常用命令 //启动Tomcat sh /opt/apache-tomcat-8.5.29/bin/startup.sh //停止tomcat sh /opt/apache-tomca ...
- Redis++:Redis做分布式锁真的靠谱吗
Redis做分布式锁真的靠谱吗 Redis的分布式锁可以通过Lua进行实现,通过setnx和expire命令连用的方式 || 也可以使用高版本的方法同时设置失效时间,但是假如在以下情况下,就会造成无锁 ...