JAVA该队列中的数组,圆阵队列,链队列
/**
* 文件名: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该队列中的数组,圆阵队列,链队列的更多相关文章
- java 在循环中删除数组元素
在写代码中经常会遇到需要在数组循环中删除数组元素的情况,但删除会导致数组长度变化. package com.fortunedr.thirdReport; import java.util.ArrayL ...
- Java基础学习总结(67)——Java接口API中使用数组的缺陷
如果你发现在一个接口使用有如下定义方法: public String[] getParameters(); 那么你应该认真反思.数组不仅仅老式,而且我们有合理的理由避免暴露它们.在这篇文章中,我将试图 ...
- Qt事件机制(是动作发生后,一种通知对象的消息,是被动与主动的总和。先处理自己队列中的消息,然后再处理系统消息队列中的消息)
Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...
- 教你如何使用Java手写一个基于数组实现的队列
一.概述 队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表.在具体应用中通常用链表或者数组来实现.队列只允许在后端(称为rear)进行插入操作,在 ...
- Java中的四种引用和引用队列
目录 强引用 软引用 弱引用 幻象引用 Reachability Fence 参考 强引用 正常的引用,生命周期最长,例如 Object obj = new Object(); 当JVM内存不足时,宁 ...
- JAVA Concurrent包 中的并发集合类
我们平时写程序需要经常用到集合类,比如ArrayList.HashMap等,但是这些集合不能够实现并发运行机制,这样在服务器上运行时就会非常的消耗资源和浪费时间,并且对这些集合进行迭代的过程中不能进行 ...
- C# 编程中的堆栈(Stack)和队列(Queue)
一.什么是堆?(Heap) 堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收. 是程序运行期 ...
- javascript中稀疏数组和密集数组
密集数组 数组是一片连续的存储空间,有着固定的长度.加入数组其实位置是address,长度为n,那么占用的存储空间是address[0],address[1],address[2].......add ...
- java并发编程工具类JUC第一篇:BlockingQueue阻塞队列
Java BlockingQueue接口java.util.concurrent.BlockingQueue表示一个可以存取元素,并且线程安全的队列.换句话说,当多线程同时从 JavaBlocking ...
随机推荐
- codeforece Round#311 BCDE
B题 给我们n,m , m表示茶壶的容量 接下来2*n个数字,表示茶杯的容量,将这些茶杯分给n个男孩和n个女孩 可以倒x毫升的茶水给每个女孩,那么就要倒2x毫升的茶水给男孩,当然了,茶杯要装的下,且 ...
- Xamarin 安装步骤
原文:Xamarin 安装步骤 1.下载并解压吾乐吧软件站提供的“Mono for Android 离线包” 安装:jdk-6u45-windows-i586.exe (就算你是64位系统,也要安装i ...
- Intent有可能的使用(两)
Intent作为联系各Activity之间的纽带,其作用并不只只限于简单的数据传递. 通过其自带的属性.事实上能够方便的完毕非常多较为复杂的操作. 比如直接调用拨号功能.直接自己主动调用合适的程序打开 ...
- Red Gate系列之五 .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程
原文:Red Gate系列之五 .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程 Red Gate ...
- 删CentOS / RHEL库和配置文件(Repositories and configuraiton files)
1 删除库简介 随着root权限执行以下的命令: # cd /etc/yum.repos.d/ 列出全部库(repo) #ls CentOS-Base.repo epel.repo mirrors-r ...
- ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds Memory Limit: 65536 ...
- Appium0.18.x迁移到Appium1.x须知事项(灰常实用,解答了本人几个疑问)
英文原版:https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/migrating-to-1-0.md Migr ...
- node.js基础:模块的创建和引入
模块可能是一个文件,也可能是包含一个或多个文件的目录.如果模块是个目录,node.js通常会在这个目录下找一个叫index.js的文件作为模块的入口. 典型的模块是一个包含exports对象属性定义的 ...
- RH033读书笔记(15)-Lab 16 The Linux Filesystem
Lab 16 The Linux Filesystem Goal: Develop a better understanding of Linux filesystem essentials incl ...
- UVa10000_Longest Paths(最短路SPFA)
解题报告 求最长路. 用SPFA求最长路,初始化图为零,dis数组也为零 #include <iostream> #include <cstdio> #include < ...