c++ - 循环队列中的 pop() 操作。我如何真正删除该项目?

标签 c++ queue

我正在尝试在 C++ 中使用数组实现一个简单的循环队列。下面是我的代码。

#include <iostream>

int  pop();
void push(int );
const int arrayLength = 8;
int inputArray[arrayLength] = {0};
int queueFront=0,queueBack=0;

void push(int theElement)
{
  //Check if the push causes queue to overflow
     if  (((queueBack + 1 ) % arrayLength) == queueFront)
 {
     std::cout<<"Queue is full."<<std::endl;
     return ;
 }
 inputArray[queueBack] = theElement;
     queueBack = (queueBack + 1) % arrayLength;
}


 int pop()
 {
   //Check if queue  is already empty

   if ( queueFront == queueBack )
   {
    std::cout<<"Queue is empty."<<std::endl;
   }

       std::cout<<inputArray[queueFront]<<" removed."<<std::endl;
   queueFront = (queueFront + 1 ) % arrayLength;



 }

int main()
{
  for ( int i =0; i < arrayLength; ++i)
  {
      std::cout<<inputArray[i]<<std::endl;
  }
  push(1);
  push(2);
  push(3);
  pop();
  push(5);

      //printing arrayelements
  for ( int i =0; i < arrayLength; ++i)
  {
    std::cout<<inputArray[i]<<std::endl;
  }
 }

运行时得到如下输出:

0 0 0 0 0 0 0 0 1 已删除。 1个 2个 3个 5个 0 0 0 0

问题一: 1. 我如何实际删除 pop() 操作中的项目? 2. 我的实现是否正确?

谢谢

最佳答案

鉴于 pop() 在确定队列为空后仍会更改队列,因此对 #2 的回答是“否”。

关于c++ - 循环队列中的 pop() 操作。我如何真正删除该项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881930/

相关文章:

c++ - OpenCV findContours 堆栈溢出

c++ - 使用 stringstream 将字符串转换为数字

multithreading - 如何将 perl 子例程排队到线程队列而不是数据?

c++ - 为什么我使用队列和 fstream 的简单 C++ 程序会出错,变成无效指针?

c++ - 为什么 `std::array::at()` 没有实现为模板函数?

c++ - Qt + iperf3 = lconv 未声明

c++ - 从文件加载 Bullet Physics 网格

C++队列存储多种类型对象

c++ - 如何将整个 vector 复制到队列中?

Java Maze Solver - 我从来没有这么卡过