/**
* 文件名: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. CC2530 外部中断 提醒

    #include "ioCC2530.h" #define uchar unsigned char #define led1    P1_0 #define led2    P1_ ...

  2. Makefile 管理工具 — Automake and Autoconf

    该project下载路径:http://files.cnblogs.com/iTsihang/hello-2.0.zip automake 參考资料:http://www.linuxforum.net ...

  3. 给节点设置tag【从零開始cocos3.0final 】

    在cocos中通过tag来管理节点是非经常常使用的:以下介绍一个关于在cocos中使用tag的实例: typedef enum{ tag1 }Tag; 这里能够使用枚举类型,来为多个节点设置tag: ...

  4. oracle 包,函数,过程,块的创建和执行及在java中执行(转)

    SQL> create or replace procedure sp_guocheng1 is--如果有这个名字就替换  2  begin--执行部分  3  insert into guoc ...

  5. 构建自己的Java并发模型框架

    Java的多线程特性为构建高性能的应用提供了极大的方便,可是也带来了不少的麻烦.线程间同步.数据一致性等烦琐的问题须要细心的考虑,一不小心就会出现一些微妙的,难以调试的错误. 另外.应用逻辑和线程逻辑 ...

  6. Win 10开门人类智慧的世界领先

    3月18日,从微软硬件project大会(WinHEC 2015)上传来好消息:今年夏天,Win 10将要正式公布.Win 10公布,有何新意? 微软新领导人纳德拉(Nadella)主张:运计算,大数 ...

  7. leetcode先刷_Remove Duplicates from Sorted List II

    删除重复节点列表中的.假设所有val如果仅仅是为了保持一个非常easy.应承担重复val节点被删除话.要保持pre节点.每当你想保存这pre问题节点,应该head节点可以被取出,好了,没问题边境控制. ...

  8. c#并行任务多种优化方案分享(异步委托)

    遇到一个多线程任务优化的问题,现在解决了,分享如下. 假设有四个任务: 任务1:登陆验证(CheckUser) 任务2:验证成功后从Web服务获取数据(GetDataFromWeb) 任务3:验证成功 ...

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

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

  10. 小米2S twrp 中文,支持双系统

    更新日志: 更新日志: 汉化了要使用的功能 修改语言选择方式,修改为下拉方式 TDB(TrueDualBoot) 功能完美实现 **adb**功能,完美实现,无需特别操作(比CWM强大) 修改双系统切 ...