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

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

關于Java隊列實現的介紹

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

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

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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 亚洲va国产va欧美va综合 | 精品欧美中国福利第一导航 | 在线观看中文字幕亚洲 | 91视频最新网址 | 最近中文2019视频在线 | 国产大片中文字幕 | 国产精品欧美日韩精品 | 又www又黄又爽啪啪网站 | 日韩成a人片在线观看日本 日韩操操操 | 狠狠色狠狠色综合日日五 | 一道本视频在线 | 国产精品亚洲一区二区三区久久 | 欧美很很干 | 亚洲第一中文字幕 | 成人区在线观看免费视频 | 成年女人毛片免费观看不卡 | 91在i线观 | 无夜精品久久久久久 | 天天操天天干天天做 | 麻豆小说| 国内精品久久久久久久久野战 | 国产成人精品怡红院 | 黄污视频 | 精品一区二区三区中文字幕 | 国产一区二区三区四卡 | 不卡视频免费在线观看 | 日韩人体在线 | 天天插伊人 | 国产一区二区免费播放 | 成年人免费毛片 | 免费看片网址 | 日韩手机看片 | 精品在线观看一区 | 动漫精品专区一区二区三区不卡 | 亚洲国产成人精品一区二区三区 | 午夜色视频在线观看 | 免费国产成人α片 | a一级毛片 | 最近中文字幕2019高清8? | 中文字幕一区二区区免 | 久久精品免观看国产成人 |