首页 > 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学习者快速成长!