/**
* 文件名: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. [IOS]本地化

    我们在IOS开发应用中,会碰到做好的一个应用,如何趋向国际化,也就是说支持多种语言?下面我就来简单演示一下,用一个Demo来实现中文和英文的实现. 实现步骤: 1.本地化项目中xib的view 1.在 ...

  2. http1.0 和 http1.1 区别

    http1.0 和 http1.1 主要区别 1.背景   KeepAlive是就是通常所称的长连接.KeepAlive带来的好处是可以减少tcp连接的开销,这对于短response body的请求效 ...

  3. uva 11427 - Expect the Expected(概率)

    题目链接:uva 11427 - Expect the Expected 题目大意:你每天晚上都会玩纸牌,每天固定最多玩n盘,每盘胜利的概率为p,你是一个固执的人,每天一定要保证胜局的比例大于p才会结 ...

  4. 一起学习android图片四舍五入图片集资源 (28)

    效果图: 參看下面代码: public class MainActivity extends Activity { private ImageView imageView1; private Imag ...

  5. Android DrawerLayout 抽屉

    Android DrawerLayout 抽屉 DrawerLayout 在supportV4 Lib在.类似的开源slidemenu如,DrawerLayout父类ViewGroup,自定义组件基本 ...

  6. cocos2d-x3.0rc 版 设置模拟器窗体大小

    由于刚接触这职业时间不是非常长.也是第一次写博客,假设有错误的地方还请大神们指出,开通这博客目的非常easy相互学习和讨论(更重要的是记录工作中学到的东西以方便以后自己查阅) 先后參与过两个项目.只是 ...

  7. 【从翻译mos文章】rac数据库,HC_&lt;SID&gt;.dat其他文件Oracle_Home用例下。

    rac数据库.HC_<SID>.dat其他文件Oracle_Home用例下. 参考原始: RAC database HC_<SID>.dat is used by instan ...

  8. 每天进步一点点——再次了解Linux进程ID

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/38968011 1. 概述 众所周知,进程(process)是一个可运行程序的实例,可是在Li ...

  9. 键盘控制div上下左右移动 (转)

    <html> <head> <title></title> <link rel="stylesheet" type=" ...

  10. hdu 4661 Message Passing(木DP&amp;组合数学)

    Message Passing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...