黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

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

關(guān)于Java隊列實現(xiàn)的介紹

更新時間:2022-12-28 13:04:23 來源:動力節(jié)點 瀏覽1341次

隊列在Java中的實現(xiàn)

Queue 是一種線性數(shù)據(jù)結(jié)構(gòu),其中元素從稱為Rear的一端插入,并從稱為Front的另一端移除。

Rear 和 Front的值最初設(shè)置為-1,然后這些值隨著元素的插入和刪除而遞增或遞減。

隊列的基本功能

enqueue:用于在隊列尾部添加一個元素。

dequeue:它用于從隊列的前面刪除一個元素。

IsEmpty:用于檢查隊列是否為空。

IsFull:用于檢查隊列是否已滿。

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

使用數(shù)組的隊列中的enqueue和操作的復(fù)雜度為。dequeueO(1)

盡管在 Java 中提供了各種抽象數(shù)據(jù)類型(如 Stack、Queue 和 LinkedList)的使用,但始終需要了解數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ)知識并相應(yīng)地實現(xiàn)它。

1. Java中使用數(shù)組實現(xiàn)隊列

所以這里我們將在Java中使用數(shù)組來實現(xiàn)一個隊列數(shù)據(jù)結(jié)構(gòu)。

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 中實現(xiàn)隊列

Queue 接口是Java Collections的一部分,由兩個實現(xiàn)組成:

LinkedList并且PriorityQueue是實現(xiàn)Queue接口的兩個類。

由于Queue 是一個接口,我們不能創(chuàng)建它的實例。因此我們創(chuàng)建了該類的實例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()唯一的區(qū)別是隊列為空時poll返回null。

E peek():該方法與 an 的方法類似,element()唯一的區(qū)別是如果隊列為空,則 element 返回 null。

讓我們通過示例學(xué)習(xí)這些操作:

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]

以上就是關(guān)于“關(guān)于Java隊列實現(xiàn)的介紹”,大家如果對此比較感興趣,想了解更多相關(guān)知識,不妨來關(guān)注一下本站的Java隊列技術(shù)文檔,里面還有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产欧美日韩视频在线观看 | 精品国产一区二区三区成人 | 日韩免费网站 | a级高清观看视频在线看 | 久久久久久国产精品视频 | 日韩资源网 | 在线免费看片网站 | 免费看欧美一级特黄a大片 免费看欧美一级特黄α大片 | 免费日韩在线视频 | 91看片淫黄大片欧美看国产片 | 日批视频免费看 | 午夜寂寞影院在线观看 | 波多野结衣免费一区二区三区香蕉 | 一个人在线看的免费视频 | 日本欧美一区二区三区在线 | 日韩在线二区 | 性欧美video另类hd | 日日操夜夜操免费视频 | 97精品国产高清久久久久蜜芽 | 成人影院在线看 | 国产一级鲁丝片 | 欧美成人剧情中文字幕 | 成人网视频 | 免费观看一级欧美大 | 国产成人夜间影院在线观看 | 噜噜噜色 | 欧美国产日本高清不卡免费 | xxx.国产| 欧美日本中文 | 污黄网站 | 精品国产国语对白主播野战 | 亚洲女人国产香蕉久久精品 | 波多野结衣免费在线视频 | 国产二区三区毛片 | 视频黄色片 | 99re在线免费视频 | 免费看日b视频 | 亚洲精品视频在线 | 国产日韩欧美精品一区 | 99热99re8国产在线播放 | 国产亚洲高清在线精品99 |