更新時間:2022-12-28 13:04:23 來源:動力節點 瀏覽1330次
Queue 是一種線性數據結構,其中元素從稱為Rear的一端插入,并從稱為Front的另一端移除。
Rear 和 Front的值最初設置為-1,然后這些值隨著元素的插入和刪除而遞增或遞減。
enqueue:用于在隊列尾部添加一個元素。
dequeue:它用于從隊列的前面刪除一個元素。
IsEmpty:用于檢查隊列是否為空。
IsFull:用于檢查隊列是否已滿。
peek: 用于返回前面的值而不刪除它。
使用數組的隊列中的enqueue和操作的復雜度為。dequeueO(1)
盡管在 Java 中提供了各種抽象數據類型(如 Stack、Queue 和 LinkedList)的使用,但始終需要了解數據結構的基礎知識并相應地實現它。
所以這里我們將在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
隊列為空
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隊列技術文檔,里面還有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習