专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 关于Java队列实现的介绍

关于Java队列实现的介绍

更新时间:2022-12-28 13:04:23 来源:星辉 浏览742次

队列在Java中的实现

Queue 是一种线性数据结构,其中元素从称为Rear的一端插入,并从称为Front的另一端移除。

Rear 和 Front的值最初设置为-1,然后这些值随着元素的插入和删除而递增或递减。

队列的基本功能

enqueue:用于在队列尾部添加一个元素。

dequeue:它用于从队列的前面删除一个元素。

IsEmpty:用于检查队列是否为空。

IsFull:用于检查队列是否已满。

peek: 用于返回前面的值而不删除它。

使用数组的队列中的enqueue和操作的复杂度为。dequeueO(1)

尽管在 Java 中提供了各种抽象数据类型(如 Stack、Queue 和 LinkedList)的使用,但始终需要了解数据结构的基础知识并相应地实现它。

1. Java中使用数组实现队列

所以这里我们将在Java中使用数组来实现一个队列数据结构。

import java.util.*;
// define queue class
class Queue 
{
    int arr[], front, rear, cap, n1;  			
    // Queue constructor
	Queue(int n)
	{
		arr = new int[n];
		cap = n;
		front = 0;
		rear = -1;
		n = 0;
	}
	// dequeue function for removing the front element
	public void dequeue()
	{
		// check for queue underflow
		if (isEmpty())
		{
			System.out.println("No items in the queue,cannot delete");
			System.exit(1);
		}
		System.out.println("Deleting " + arr[front]);
		front = (front + 1) % cap;
		n1--;
	}
	// enqueue function for adding an item to the rear
    public void enqueue(int val)
	{
		// check for queue overflow
		if (isFull())
		{
			System.out.println("OverFlow!!Cannot add more values");
			System.exit(1);
		}
		System.out.println("Adding " + val);
		rear = (rear + 1) % cap;
		arr[rear] = val;
		n1++;
	}
	// peek function to return front element of the queue
	public int peek()
	{
		if (isEmpty()) 
		{
			System.out.println("Queue empty!!Cannot delete");
			System.exit(1);
		}
		return arr[front];
	}
	// returns the size of the queue
	public int size()
	{
		return n1;
	}
	// to check if the queue is empty or not
	public Boolean isEmpty()
	{
		return (size() == 0);
	}
	// to check if the queue is full or not
	public Boolean isFull()
	{
		return (size() == cap);
	}	
	// Queue implementation in java
	public static void main (String[] args)
	{
		// create a queue of capacity 5
		Queue q = new Queue(5);
		q.enqueue(10);
		q.enqueue(20);
		q.enqueue(30);		
		System.out.println("Front element is: " + q.peek());
		q.dequeue();
		System.out.println("Front element is: " + q.peek());
		System.out.println("Queue size is " + q.size());
		q.dequeue();
		q.dequeue();		
		if (q.isEmpty())
			System.out.println("Queue Is Empty");
		else
			System.out.println("Queue Is Not Empty");
	}
}

输出

添加 10
添加 20
添加 30
前端元素为:10
删除 10
前端元素为:20
队列大小为 2
删除 20
删除 30
队列为空

2. 使用队列接口在 Java 中实现队列

Queue 接口是Java Collections的一部分,由两个实现组成:

LinkedList并且PriorityQueue是实现Queue接口的两个类。

由于Queue 是一个接口,我们不能创建它的实例。因此我们创建了该类的实例LinkedList并将其PriorityQueue分配给队列接口。

Queue q1 = new LinkedList();
Queue q2 = new PriorityQueue();

Queue接口主要有五个操作。他们是:

boolean add(E e):此方法用于在队列末尾添加特定元素。由于它的返回类型是布尔值,如果元素添加成功则返回 true,否则返回 false。

E element():此方法返回队列的第一个元素。

E remove():此方法删除队列的第一个元素。

E poll(): 这个方法和a类似,remove()唯一的区别是队列为空时poll返回null。

E peek():该方法与 an 的方法类似,element()唯一的区别是如果队列为空,则 element 返回 null。

让我们通过示例学习这些操作:

1)使用LinkedList类

import java.util.*;
public class QueueExample1 
{	 
   public static void main(String[] args) 
   {
	  Queue<String> q = new LinkedList<String>();	    
      //Adding elements to the Queue
      q.add("Mohit");
      q.add("Priyanka"); 
      q.add("Prabhat");
      q.add("Pranjal");
      q.add("Anilanshu");       
      System.out.println("Elements in Queue:"+q);
      System.out.println("Removed element: "+q.remove());
      System.out.println("Head: "+q.element());
      System.out.println("poll(): "+q.poll());
      System.out.println("peek(): "+q.peek());
      System.out.println("Elements in Queue:"+q);
   }
}

输出

队列中的元素:[Mohit、Priyanka、Prabhat、Pranjal、Anilanshu]
删除的元素:Mohit 
Head:Priyanka 
poll():Priyanka 
peek():Prabhat
队列中的元素:[Prabhat、Pranjal、Anilanshu]

2)使用 PriorityQueue类

import java.util.*;
public class QueueExample2 
{ 
   public static void main(String[] args) 
   {
	  Queue<Integer> q2 = new PriorityQueue<Integer>();	    
      //Adding elements to the Queue
      q2.add(10);
      q2.add(20); 
      q2.add(30);
      q2.add(40);
      q2.add(50);  
      System.out.println("Elements in Queue:"+q2);
      System.out.println("Removed element: "+q2.remove());
      System.out.println("Head: "+q2.element());	    
      System.out.println("poll(): "+q2.poll());
      System.out.println("peek(): "+q2.peek());
      System.out.println("Elements in Queue:"+q2);
   }
}

输出

队列中的元素:[10,20,30,40,50]
删除的元素:10 
Head:20 
poll():20 
peek():30
队列中的元素:[30,40,50]

以上就是关于“关于Java队列实现的介绍”,大家如果对此比较感兴趣,想了解更多相关知识,不妨来关注一下本站的Java队列技术文档,里面还有更丰富的知识等着大家去学习,希望对大家能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>