「java加队列」JAVA的队列

博主:adminadmin 2022-12-04 16:54:07 64

今天给各位分享java加队列的知识,其中也会对JAVA的队列进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 队列

//通过LinkedList实现队列

package 队列和堆栈;

import java.util.*;

public class LinkedListQueueTest {

//字段

private LinkedList list;

//无参数构造

public LinkedListQueueTest()

{

list=new LinkedList();

}

//队列元素的个数

public int size()

{

return list.size();

}

//进入队列

public void enqueue(Object obj)

{

list.addLast(obj);

}

//对头出来

public Object dequeue()

{

return list.removeFirst();

}

//浏览对头元素

public Object front()

{

//return list.getFirst();

return list.peekFirst();

}

//判断队列是否为空

public boolean isEmpty()

{

return list.isEmpty();

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

LinkedListQueueTest llq=new LinkedListQueueTest();

System.out.println(llq.isEmpty());

llq.enqueue("147");

llq.enqueue("258");

llq.enqueue("369");

System.out.println(llq.size());

System.out.println("移除队列头元素:"+llq.dequeue());

System.out.println(llq.size());

llq.enqueue("abc");

llq.enqueue("def");

System.out.println(llq.size());

System.out.println("查看队列的头元素:"+llq.front());

System.out.println(llq.size());

System.out.println(llq.isEmpty());

}

}

通过数组实现

package 队列和堆栈;

import java.util.NoSuchElementException;

//通过数组来实现队列

public class ArrayQueue {

//字段

public static Object[] data;

//队列的元素个数

protected int size ;

//队列头

protected int head;

//队列尾

public static int tail;

/**

*

*/

//无参数构造函数

public ArrayQueue() {

final int INITIAL_LENGTH=3;

data=new Object[INITIAL_LENGTH];

size=0;

head=0;

tail=-1;

}

//队列元素个数方法

public int size()

{

return size;

}

public boolean isEmpty()

{

return size==0;

}

//得到队列头元素

public Object front()

{

if(size==0)

throw new NoSuchElementException();

return data[head];

}

//进入队列enqueue()方法

public void enqueue(Object obj)

{

//此时队列已经满

if(size==data.length){

Object[] oldData=data;

data=new Object[data.length*2];

//if(head==0)

System.arraycopy(oldData, head, data, 0, oldData.length-head);

if(head0)

System.arraycopy(oldData, 0, data, head+1, tail+1);

head=0;

tail=oldData.length-1;

}

tail=(tail+1)%data.length;

size++;

data[tail]=obj;

}

//队列的元素出队

public Object dequeue()

{

if(size==0)

throw new NoSuchElementException();

Object ele=data[head];

//循环队列

head=(head+1)%data.length;

size--;

return ele;

}

@Override

public String toString() {

// TODO Auto-generated method stub

return super.toString();

}

}

通过向量实现:

//通过向量实现栈

package 队列和堆栈;

import java.util.*;

public class VectorStackTest {

//字段

Vector v;

//构造函数

public VectorStackTest()

{

v=new Vector();

}

//元素的个数

public int size()

{

return v.size();

}

//是否为空

public boolean isEmpty()

{

return size()==0;

}

//进栈

public Object Push(Object obj)

{

v.addElement(obj);

return obj;

}

//出栈方法

public Object Pop()

{

int len=size();

Object obj=Peek();

v.removeElementAt(len-1);

return obj;

}

//查看栈顶元素

public Object Peek()

{

int len = size();

if (len == 0)

throw new EmptyStackException();

return v.elementAt(len - 1);

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

VectorStackTest vst=new VectorStackTest();

System.out.println("大小:"+vst.size());

vst.Push("123");

vst.Push("456");

vst.Push("789");

vst.Push("abc");

System.out.println("大小:"+vst.size());

System.out.println("栈顶:"+vst.Peek());

System.out.println("出栈:"+vst.Pop());

vst.Push("def");

vst.Push("147");

System.out.println("大小:"+vst.size());

System.out.println("栈顶:"+vst.Peek());

System.out.println("出栈:"+vst.Pop());

System.out.println(vst.Peek());

vst.Push("def");

vst.Push("147");

System.out.println(vst.Pop());

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println(vst.Pop());

System.out.println(vst.Pop());

vst.Push("1aadf");

vst.Push("2dafad");

vst.Push("123789");

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println(vst.Pop());

System.out.println(vst.Peek());

System.out.println("------------------end------------");

VectorStackTest llst=new VectorStackTest();

llst.Push("123");

llst.Push("456");

System.out.println("栈顶:"+llst.Peek());

System.out.println("出栈:"+llst.Pop());

System.out.println(llst.Peek());

llst.Push("789");

llst.Push("abc");

System.out.println("栈顶:"+llst.Peek());

System.out.println("出栈:"+llst.Pop());

System.out.println(llst.size());

System.out.println("栈顶:"+llst.Peek());

}

}

推荐:都看API文档。有疑问可以问我,QQ:285479197

用java编一个队列

自己写了个简单的实现

class QueueE{

private Object[] integerQueue;//用来当队列

public int tail;//队尾

public int size;//队的长度,也可以设置一个默认值,溢出时从新申请

public Queue(int size){

integerQueue=new Object[size];

this.size=size;

tail=-1;

}

/**

* 将元素插入队列

* @return 如果该元素已添加到此队列,则返回 true;否则返回 false

*/

public boolean offer(E e){

if(tail size-1){

tail++;

this.integerQueue[tail]=e;

return true;

}else{

return false;

}

}

/**

* 获取并移除此队列的头,如果此队列为空,则返回 null。

*/

public E poll(){

Object tmp;

if(tail=0){

tmp=this.integerQueue[tail];

tail--;

return (E)tmp;

}else{

return null;

}

}

}

java中怎么实现队列

public class QueueE {

private Object[] data=null;

private int maxSize; //队列容量

private int front; //队列头,允许删除

private int rear; //队列尾,允许插入

//构造函数

public Queue(){

this(10);

}

public Queue(int initialSize){

if(initialSize =0){

this.maxSize = initialSize;

data = new Object[initialSize];

front = rear =0;

}else{

throw new RuntimeException("初始化大小不能小于0:" + initialSize);

}

}

//判空

public boolean empty(){

return rear==front?true:false;

}

//插入

public boolean add(E e){

if(rear== maxSize){

throw new RuntimeException("队列已满,无法插入新的元素!");

}else{

data[rear++]=e;

return true;

}

}

//返回队首元素,但不删除

public E peek(){

if(empty()){

throw new RuntimeException("空队列异常!");

}else{

return (E) data[front];

}

}

//出队

public E poll(){

if(empty()){

throw new RuntimeException("空队列异常!");

}else{

E value = (E) data[front]; //保留队列的front端的元素的值

data[front++] = null; //释放队列的front端的元素

return value;

}

}

//队列长度

public int length(){

return rear-front;

}

}

java中queue的使用方法?

java中的queue类是队列数据结构管理类。在它里边的元素可以按照添加它们的相同顺序被移除。

队列通常(但并非一定)以 FIFO(先进先出)的方式排序各个元素。不过优先级队列和 LIFO 队列(或堆栈)例外,前者根据提供的比较器或元素的自然顺序对元素进行排序,后者按 LIFO(后进先出)的方式对元素进行排序。无论使用哪种排序方式,队列的头都是调用remove()或poll()所移除的元素。在 FIFO 队列中,所有的新元素都插入队列的末尾。其他种类的队列可能使用不同的元素放置规则。每个Queue实现必须指定其顺序属性。

offer 添加一个元素并返回true 如果队列已满,则返回false

poll 移除并返问队列头部的元素 如果队列为空,则返回null

peek 返回队列头部的元素 如果队列为空,则返回null

put 添加一个元素 如果队列满,则阻塞

take 移除并返回队列头部的元素 如果队列为空,则阻塞

element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常

remove 移除并返回队列头部的元素 如果队列为空,则抛出一个

NoSuchElementException异常

注意:poll和peek方法出错进返回null。因此,向队列中插入null值是不合法的。

还有带超时的offer和poll方法重载,例如,下面的调用:

boolean success = q.offer(x,100,TimeUnit.MILLISECONDS);

尝试在100毫秒内向队列尾部插入一个元素。如果成功,立即返回true;否则,当到达超时进,返回false。同样地,调用:

Object head = q.poll(100, TimeUnit.MILLISECONDS);

如果在100毫秒内成功地移除了队列头元素,则立即返回头元素;否则在到达超时时,返回null。

阻塞操作有put和take。put方法在队列满时阻塞,take方法在队列空时阻塞。

Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Queue接 口。Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类型如果是Queue时,就完全只能访问Queue接口所定义的方法 了,而不能直接访问 LinkedList的非Queue的方法),以使得只有恰当的方法才可以使用。BlockingQueue 继承了Queue接口。

在java中,什么是队列?

java中没有队列这个东西吧...队列这个是出现在数据结构里面的吧..

然后 队列的结构是 先进先出..有队头对尾.数据从尾进...从头读出来

比如 1 2 3 4 5 这么一个队列....那么java读这个数据.是从1的队头开始读.....读到1 把1拿出来..然后队列剩下 2 3 4 5 ....如果要添加进去.那么就是 在对尾 就是5 的后面加一个 如3 ..那么就是 2 3 4 5 3 .这样的结构..

跟栈不同.栈是先进后出..这个你自己翻书吧..或者网上搜 数据结构 栈..就有了.

补充一下...java是有提供队列的类的.这个我就不说自己学会看api吧.

java用数组实现队列

1.1. 队列的数据结构

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

1.2. Java实现

QueueTest

package ch04;

public class QueueTest {

public static void main(String[] args) {

ArrayQueue queue = new ArrayQueue(10);

System.out.println(queue.isEmpty());

for (int i = 0; i 10; i++) {

queue.insert(i);

}

System.out.println(queue.isFull());

while (!queue.isEmpty()) {

System.out.println(queue.remove());

}

}

}

class ArrayQueue {

private int[] arrInt;// 内置数组

private int front;// 头指针

private int rear;// 尾指针

public ArrayQueue(int size) {

this.arrInt = new int[size];

front = 0;

rear = -1;

}

/**

* 判断队列是否为空

*

* @return

*/

public boolean isEmpty() {

return front == arrInt.length;

}

/**

* 判断队列是否已满

*

* @return

*/

public boolean isFull() {

return arrInt.length - 1 == rear;

}

/**

* 向队列的队尾插入一个元素

*/

public void insert(int item) {

if (isFull()) {

throw new RuntimeException("队列已满");

}

arrInt[++rear] = item;

}

/**

* 获得对头元素

*

* @return

*/

public int peekFront() {

return arrInt[front];

}

/**

* 获得队尾元素

*

* @return

*/

public int peekRear() {

return arrInt[rear];

}

/**

* 从队列的对头移除一个元素

*

* @return

*/

public int remove() {

if (isEmpty()) {

throw new RuntimeException("队列为空");

}

return arrInt[front++];

}

}

运行结果如下:

false

true

1

2

3

4

5

6

7

8

9

关于java加队列和JAVA的队列的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-04,除非注明,否则均为首码项目网原创文章,转载请注明出处。