/**
* 文件名:QueueText.java
* 时间:2014年10月22下午9:05:13
* 笔者:维亚康姆维修
*/
package chapter3;
/**
* 类名:ArrayQueue
* 说明:数组实现
*/
class ArrayQueue<AnyType>{
private static final int DEFAULT_CAPACITY = 10;
private int front;//队头
private int rear;//队尾
private int theSize;
private AnyType[] theItems;
public ArrayQueue(){
clear();
}
public void clear(){
front = 0;
rear = 0;
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public boolean isEmpty(){
return theSize == 0;//or front == rear;
}
public void enqueue(AnyType x){
theItems[rear++] = x;
theSize++;
}
public AnyType dequeue(){
if(theSize == 0)
return null;
theSize--;
return theItems[front++];
}
//返回队列前面的元素
public AnyType element(){
if(isEmpty())
return null;
return theItems[front];
}
/**
* 方法名:ensureCapacity
* 说明:确保容量
*/
public void ensureCapacity(int newCapacity){
if(newCapacity < theSize)
return;
AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapacity];
for(int i = 0;i < theSize;i++)
theItems[i] = old[i];
} }
/**
* 类名:CirArrQueue
* 说明:循环队列 (由于用数组实现的队列在删除的时候指针会后移 这样导致了前面的浪费,
* 循环数组能够解决问题,可是循环队列可能使得执行时间加倍)
*/
class CirArrQueue<AnyType>{
private static final int DEFAULT_CAPACITY = 3;
private int front;//队头
private int rear;//队尾
private int theSize;
private AnyType[] theItems;
public CirArrQueue(){
clear();
}
public void clear(){
theItems = (AnyType[]) new Object[DEFAULT_CAPACITY];
front = 0;
rear = 0;
theSize = 0;
}
public boolean isEmpty(){
return theSize == 0;//or front == rear;
}
public boolean enqueue(AnyType x){
if(theSize == DEFAULT_CAPACITY)
return false;
theItems[rear++] = x;
theSize++;
if(rear == DEFAULT_CAPACITY)//假设到了尾部则回到0
rear = 0;
return true;
}
public AnyType dequeue(){
if(theSize == 0)
return null;
theSize--;
AnyType temp = theItems[front];
if(++front == DEFAULT_CAPACITY)//假设加1超过了数组 则返回到0;
front = 0;
return temp;
}
//返回队列前面的元素
public AnyType element(){
if(isEmpty())
return null;
return theItems[front];
} }
/**
* 类名:LinkedQueue
* 说明:链表实现队列
*/
class LinkedQueue<AnyType>{
private static class Node<AnyType> {
Node(AnyType data, Node<AnyType> next) {
this.data = data;
this.next = next;
} private AnyType data;
private Node<AnyType> next;
} private Node<AnyType> front;
private Node<AnyType> rear;
public LinkedQueue(){
clear();
}
public void clear(){
front = null;
rear = null;
}
public boolean isEmpty(){
return front == null;
}
public void enqueue(AnyType x){
if(front == null&&rear == null){//最開始的时候
rear = new Node<AnyType>(x,null);
front = rear;
}else{
Node<AnyType> p = new Node<AnyType>(x,null);
rear.next = p;
rear = p;;
} }
public AnyType dequeue(){
if(!isEmpty()){
AnyType x = front.data;
front = front.next;
return x;
}
return null;
}
public AnyType element(){
if(!isEmpty())
return front.data;
return null;
} }
/**
* 类名:QueueText
* 说明:队列的数组和链表实现及循环队列的数组实现
*/
public class QueueText {
/**
* 方法名:main
* 说明:測试
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/****数组队列測试*****/
/*ArrayQueue<Integer> q = new ArrayQueue<Integer>();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
System.out.println(q.element());
while(!q.isEmpty())
System.out.println(q.dequeue());
/*********链表队列測试**********/
/*LinkedQueue<Integer> q2 = new LinkedQueue<Integer>();
q2.enqueue(1);
q2.enqueue(2);
q2.enqueue(3);
q2.enqueue(4);
System.out.println(q2.element());
while(!q2.isEmpty())
System.out.println(q2.dequeue());
/***********循环队列測试************/
CirArrQueue<Integer> q3 = new CirArrQueue<Integer>();
q3.enqueue(1);
q3.enqueue(2);
q3.enqueue(3);
q3.dequeue();
q3.enqueue(4);//设置默认容量为3。開始时3个,队列满了,后来删除第一个,队列数组里第一个空出来了,4进入了。可是还是满足先进先出的原则,加上 //size的控制,使其不会覆盖后面的
q3.enqueue(5);//没有进去
System.out.println(q3.element());
while(!q3.isEmpty())
System.out.println(q3.dequeue()); } }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

JAVA该队列中的数组,圆阵队列,链队列的更多相关文章

  1. java 在循环中删除数组元素

    在写代码中经常会遇到需要在数组循环中删除数组元素的情况,但删除会导致数组长度变化. package com.fortunedr.thirdReport; import java.util.ArrayL ...

  2. Java基础学习总结(67)——Java接口API中使用数组的缺陷

    如果你发现在一个接口使用有如下定义方法: public String[] getParameters(); 那么你应该认真反思.数组不仅仅老式,而且我们有合理的理由避免暴露它们.在这篇文章中,我将试图 ...

  3. Qt事件机制(是动作发生后,一种通知对象的消息,是被动与主动的总和。先处理自己队列中的消息,然后再处理系统消息队列中的消息)

    Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...

  4. 教你如何使用Java手写一个基于数组实现的队列

    一.概述 队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表.在具体应用中通常用链表或者数组来实现.队列只允许在后端(称为rear)进行插入操作,在 ...

  5. Java中的四种引用和引用队列

    目录 强引用 软引用 弱引用 幻象引用 Reachability Fence 参考 强引用 正常的引用,生命周期最长,例如 Object obj = new Object(); 当JVM内存不足时,宁 ...

  6. JAVA Concurrent包 中的并发集合类

    我们平时写程序需要经常用到集合类,比如ArrayList.HashMap等,但是这些集合不能够实现并发运行机制,这样在服务器上运行时就会非常的消耗资源和浪费时间,并且对这些集合进行迭代的过程中不能进行 ...

  7. C# 编程中的堆栈(Stack)和队列(Queue)

    一.什么是堆?(Heap)      堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收.      是程序运行期 ...

  8. javascript中稀疏数组和密集数组

    密集数组 数组是一片连续的存储空间,有着固定的长度.加入数组其实位置是address,长度为n,那么占用的存储空间是address[0],address[1],address[2].......add ...

  9. java并发编程工具类JUC第一篇:BlockingQueue阻塞队列

    Java BlockingQueue接口java.util.concurrent.BlockingQueue表示一个可以存取元素,并且线程安全的队列.换句话说,当多线程同时从 JavaBlocking ...

随机推荐

  1. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  2. 【UFLDL】多层神经网络

    请参见原始英文教程地址:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks 本文是在学习该教程时记得笔记,供參 ...

  3. 使用Ratpack和Spring Boot打造高性能的JVM微服务应用

    使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...

  4. Java 并发专题 : Semaphore 实现 互斥 与 连接池

    继续并发方面的知识.今天介绍Semaphore,同样在java.util.concurrent包下. 本来准备通过例子,从自己实现到最后使用并发工具实现,但是貌似效果并不是很好,有点太啰嗦的感觉,所有 ...

  5. s3c2440的A/D转换应用

    10 地点 CMOS ADC(模/数字转换器)是 8 通道模拟输入型设备回收.该模拟输入信号转换 10 位二进制数字编码,A/D变化.也被称为模数转换.该模拟信号被转换成美元 算机可以处理的数字信号. ...

  6. HTML5 Storage API

    原文:HTML5 Storage API Web Storage是HTML5引入的一个非常重要的功能,可以在客户端本地存储数据,类似HTML4的cookie,但可实现功能要比cookie强大的多. 在 ...

  7. Akka FSM 源代码分析

    Akka FSM 源代码分析 萧猛 <simonxiao@qq.com> 啰嗦几句 有限状态机本身不是啥新奇东西,在GoF的设计模式一书中就有状态模式, 也给出了实现的建议.各种语言对状态 ...

  8. char与unsigned char 差别

    char 与 unsigned char的本质差别 http://bbs.csdn.net/topics/270080484 同一个内存内容:10010000      你用char*   解释是-1 ...

  9. ZOJ 3734 LIKE vs CANDLE

    题目意思:(13年长沙站的一道水DP,本人也去了,当时太水笔) 说俩个人竞争选票,每个人可以随机选择支持谁.每个人带有权重不同. 现在已经结束了投票阶段,你一个骇客 支持LIKE  你写了一个软件可以 ...

  10. Eclipse——热键&amp;Help

    版权声明:本文博主原创文章.博客,未经同意不得转载.