当前位置: 首页 > 编程笔记 >

循环队列-C ++中的插入和删除操作

赵明亮
2023-03-14
本文向大家介绍循环队列-C ++中的插入和删除操作,包括了循环队列-C ++中的插入和删除操作的使用技巧和注意事项,需要的朋友参考一下

队列是包含元素集合的抽象数据结构。队列执行FIFO机制,即,首先插入的元素也将首先删除。

队列拐杖是一种线性数据结构。但是如果我们使用数组实现队列,可能会产生一些问题。有时通过使用一些连续的插入和删除操作,前后位置将发生变化。在那一刻,队列看起来没有空间向其中插入元素。即使有一些可用空间,由于某些逻辑问题也不会使用。为了克服这个问题,我们将使用循环队列数据结构。

循环队列是一种队列,其中最后一个位置与第一个位置相连以形成一个圆圈。

算法

插入(队列,键)-

begin
   if front = 0 and rear = n – 1, or front = rear + 1, then queue is full, and return
   otherwise
   if front = -1, then front = 0 and rear = 0
   else
      if rear = n – 1, then, rear = 0, else rear := rear + 1
   queue[rear] = key
end

删除(队列) -

begin
   if front = -1 then queue is empty, and return
   otherwise
   item := queue[front]
   if front = rear, then front and rear will be -1
   else
      if front = n – 1, then front := 0 else front := front + 1
end

示例

#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;
void insertCQ(int val) {
   if ((front == 0 && rear == n-1) || (front == rear+1)) {
      cout<<"Queue Overflow \n";
      return;
   }
   if (front == -1) {
      front = 0;
      rear = 0;
   }
   else {
      if (rear == n - 1)
         rear = 0;
      else
         rear = rear + 1;
   }
   cqueue[rear] = val ;
}
void deleteCQ() {
   if (front == -1) {
      cout<<"Queue Underflow\n";
      return ;
   }
   cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
   if (front == rear) {
      front = -1;
      rear = -1;
   }
   else {
      if (front == n - 1)
         front = 0;
      else
         front = front + 1;
   }
}
void displayCQ() {
   int f = front, r = rear;
   if (front == -1) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are :\n";
   if (f <= r) {
      while (f <= r){
         cout<<cqueue[f]<<" ";
         f++;
      }
   }
   else {
      while (f <= n - 1) {
         cout<<cqueue[f]<<" ";
         f++;
      }
      f = 0;
      while (f <= r) {
         cout<<cqueue[f]<<" ";
         f++;
      }
   }
   cout<<endl;
}
int main() {
   int ch, val;
   cout<<"1)Insert\n";
   cout<<"2)Delete\n";
   cout<<"3)Display\n";
   cout<<"4)Exit\n";
   do {
      cout<<"Enter choice : "<<endl;
      cin>>ch;
      switch(ch) {
         case 1:
            cout<<"Input for insertion: "<<endl;
            cin>>val;
            insertCQ(val);
         break;
         case 2:
            deleteCQ();
         break;
         case 3:
            displayCQ();
         break;
         case 4:
            cout<<"Exit\n";
         break;
            default: cout<<"Incorrect!\n";
      }
   } while(ch != 4);
      return 0;
}

输出结果

1)Insert
2)Delete
3)Display
4)Exit
Enter choice :
1
Input for insertion:
10
Enter choice :
1
Input for insertion:
20
Enter choice :
1
Input for insertion:
30
Enter choice :
1
Input for insertion:
40
Enter choice :
1
Input for insertion:
50
Enter choice :
3
Queue elements are :
10 20 30 40 50
Enter choice :
2
Element deleted from queue is : 10
Enter choice :
2
Element deleted from queue is : 20
Enter choice :
3
Queue elements are :
30 40 50
Enter choice :
4
Exit
 类似资料:
  • 本文向大家介绍C++实现循环队列,包括了C++实现循环队列的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C++实现循环队列的具体代码,供大家参考,具体内容如下 circularQueue.h main.cpp 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 假设我有一个大小为[10]的数组,当该数组被填满时,我想实现一个FIFO结构,而不是它只是填满了,因此无法向数组中添加新的东西,并抛出旧的东西。 例如,如果我有一个包含汽车制造商的字符串数组,当我的数组中有10个制造商时,我希望删除最旧的条目,添加最新的条目,但要考虑kepping FIFO。我如何在这样的方法中实现它:

  • 本文向大家介绍C语言实现循环队列,包括了C语言实现循环队列的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C语言实现循环队列的具体代码,供大家参考,具体内容如下 注意事项: 1、循环队列,是队列的顺序表示和实现。因为是尾进头出,所以和顺序栈不同的是需要将顺序队列臆造成一个环状的空间,以便在尾部添加满之后从头部空位开始插入。 2、也可以使用数组队列,也就是不能动态增长的顺序队列,这样不

  • 问题内容: for (String fruit : list) { if(“banane”.equals(fruit)) list.remove(fruit); System.out.println(fruit); } 这是一个带有删除指令的循环。在执行时,我在控制台输出下得到一些ConcurrentModificationException: 问题:如何使用循环删除某些元素? 问题答案: 您需要

  • 本文向大家介绍C和C ++中的循环,包括了C和C ++中的循环的使用技巧和注意事项,需要的朋友参考一下 在本教程中,我们将讨论一个程序,以了解C和C ++中的循环。 当我们不得不一次又一次地执行给定的块代码时,使用编程中的循环。它采用了一次又一次编写同一代码行的方法,并促进了DRY代码实践。 示例 对于循环 输出结果 While循环 输出结果

  • 问题内容: 我正在使用RQ,我有一个队列包含数千个项目,而另一个队列我创建了一段时间用于测试,现在已经空了并且没有使用。我想知道如何从队列中删除所有作业,然后完全删除队列? 道歉的基本问题,但我无法在RQ文档中找到有关此信息,对于Redis和RQ来说,我都是新手…预先感谢! 问题答案: RQ提供了使任何队列为空的方法: 如果仍然存在,也可以对队列执行相同的操作。 清理使用 安装rq-dashboa