首頁 > Java > java教程 > 主體

Java循環佇列

WBOY
發布: 2024-08-30 15:08:27
原創
473 人瀏覽過

以下文章提供了循環佇列 Java 的概述。循環佇列是一種線性資料結構,與簡單佇列一樣以 FIFO(先進先出)方式執行操作。在循環隊列中,最後一個位置與第一個位置相連,形成一個循環。它也稱為環形緩衝區。在簡單的佇列結構中,如果尾部到達佇列末尾,即佇列已滿,則有可能開頭元素的空間為空而無法利用。循環隊列解決了隊列的這種限制。在本主題中,我們將學習 Java 循環佇列。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

下面給出了在Java程式中使用循環隊列的基本語法:

如何在Java中建立循環佇列及其工作原理?

讓我們詳細了解循環隊列的創建和工作原理:

在循環佇列上執行的基本操作如下:

  1. 前面:用來指向隊列的第一個元素。
  2. 後部:用來指向隊列的最後一個元素。
  3. Enqueue(value): 這是一個用來將元素插入佇列的函數。 Queue 中的元素插入到後面的位置。
  4. Dequeue(): 此函數用於刪除佇列中的元素。作為隊列的基本性質,元素的刪除是從隊列的前面開始的。

下面給出了在循環佇列中建立、插入或刪除元素時遵循的步驟:

  • 最初,Front 和 Rear 的值設定為 -1。
  • 入隊(值)操作中:
  • 如果我們在佇列中插入第一個元素,則 Front 和 Rear 都設為 0。
  • 在佇列中插入新元素時,後部變為後部 + 1。
  • 新元素加入到後面的位置。
  • 後部以循環方式遞增,即如果到達末尾並且隊列已滿,則它指向隊列的開頭。
  • 出隊操作中:
  • 首先,檢查佇列是否為空。 front==rear==-1的平均值,表示隊列為空,無法刪除。
  • 如果隊列只有1個元素,即front == back,則刪除該元素並設定front == after == -1。
  • 如果佇列中有元素,則刪除front指向的值,並循環將front索引加1。

在循環佇列中工作時需要牢記以下場景:

  1. 如果front = 0且rear = size -1,則隊列已滿,這表示front指向第一個位置,rear指向最後一個位置。所以隊列已滿,無法插入。
  2. 如果front =arrear+1,則隊列已滿,也就是說,經過多次刪除後,如果front在隊列的位置3,而循環插入後rear在位置2,則鍊錶已滿,無法再進行插入。
  3. 如果front!= 0且rear = max-1,則隊列未滿,這意味著隊列開頭有空位置,因此可以進行插入。
  4. 如果後部! = size-1,則可以插入新值,因為可以使用 mod(size) 進一步增加後部。

範例

下面給出了循環佇列在Java程式中的實作範例:

  • 向隊列插入元素
  • 顯示空隊列的項目
  • 向隊列插入元素
  • 從佇列中刪除元素
  • 在滿隊列中插入元素
  • 顯示隊列中的元素以及前後位置。
public class CirQueue {
// Defining the size of Circular Queue
int SIZE = 5;
int front, rear;
int queue[] = new int[SIZE];
//creating the constructor of the above class
CirQueue() {
front = -1;
rear = -1;
}
// Implementing the 2 scenarios to check if the queue is full or not
boolean isFullQueue() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
if (front == rear + 1) {
return true;
}
return false;
}
// Check if the queue is empty or not
boolean isEmptyQueue() {
if (front == -1)
return true;
else
return false;
}
// Adding an element in the queue
void enQueue(int value) {
if (isFullQueue()) {
System.out.println("Sorry !! Queue is full.. No more elements could be inserted in it");
}
else {
// if there is no element in the queue
if (front == -1)
front = 0;
// incrementing the rear position in circular manner using modulo operator
rear = (rear + 1) % SIZE;
//placing the value at the rear position
queue[rear] = value;
System.out.println("Element " + value + " is inserted successfully");
}
}
// Deleting the element from the queue
void deQueue() {
int value;
// checking of the queue is empty or not
if (isEmptyQueue()) {
System.out.println("Sorry !!The Queue is empty.. ");
} else {
value = queue[front];
// if there is only one element in the queue
if (front == rear) {
front = -1;
rear = -1;
}
else {
// Incrementing the front in a circular manner
front = (front + 1) % SIZE;
}
}
}
// Displaying the elements of the Circular queue
void displayQueue() {
int i;
if (isEmptyQueue()) {
System.out.println("Sorry!! The Queue is Empty");
} else {
System.out.println("Position of Front:  " + front);
System.out.println("Below given are the elements of the Queue");
for (i = front; i != rear; i = (i + 1) % SIZE)
System.out.print(queue[i] + " ");
System.out.println(queue[i]);
System.out.println("Position of Rear: " + rear);
}
}
// Main function to drive the code
public static void main(String[] args) {
// creating the object of the class to call the methods
CirQueue que = new CirQueue();
// Queue is empty. No element is inserted as of now
que.deQueue();
que.enQueue(10);
que.enQueue(24);
que.enQueue(33);
que.enQueue(67);
que.enQueue(22);
que.displayQueue();
que.deQueue();
que.displayQueue();
que.enQueue(900);
que.displayQueue();
// Element cannot be inserted as the queue is full
que.enQueue(867);
que.deQueue();
que.displayQueue();
}
}
登入後複製

輸出:

Java循環佇列

下面給出的是循環佇列上各種插入和刪除後的輸出截圖:

結論

上面的描述清楚地解釋了循環隊列是什麼以及它在任何程式語言中如何運作。循環隊列是為了解決普通隊列的限製而引入的。在開始工作之前,程式設計師首先了解佇列及其在實際程式中的實作非常重要。

以上是Java循環佇列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!