大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 關于Java隊列實現的介紹

關于Java隊列實現的介紹

更新時間:2022-12-28 13:04:23 來源:動力節點 瀏覽1330次

隊列在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隊列技術文檔,里面還有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产爽片在线观看 | 97视频在线观看播放 | 久久伊人精品综合观看99 | 四虎影视网址 | 狠狠操天天操视频 | 亚洲精品综合一二三区在线 | 国内精品久久久久影院日本 | 成人影院在线免费观看 | 国内精品区一区二区三 | 一级毛片免费不卡在线 | 青青青青爽视频在线播放 | 国产成人免费视频精品一区二区 | 成人美女黄网站色大色费 | 亚洲四虎永久在线播放 | 四虎国产在线 | 久久香蕉国产线看精品 | 免费爱爱片 | 婷婷激情在线 | 午夜性a一级毛片 | 久久久免费网站 | 免费激情网址 | 欧美精品久久一区二区三区 | 国产精品网页 | 亚洲精品一区国产二区 | 亚洲精品一区二区久久久久 | 99久久精品免费看国产交换 | 香蕉碰碰人人a久久动漫精品 | 久久大香伊蕉在人线国产昨爱 | 尹人香蕉网在线观看视频 | 久久国产免费福利资源网站 | 国产五月色婷婷六月丁香视频 | 天天色色色 | 老司机午夜免费视频 | 免费观看午夜在线欧差毛片 | 日本亚洲精品久久 | 中文字幕一区二区三区在线观看 | 韩日精品在线 | 伊人网综合 | 精品视频香蕉尹人在线 | 国产精品久久久久久久久久日本 | 国内在线播放 |