我正在尝试创建一个函数,该函数将从前面删除队列中的一个元素。我在Main函数中插入了一些值,知道我正在执行dequeue函数从顶部删除它,但它不能正常工作
#include<iostream>
using namespace std;
# define N 5
int queue[N];
int front = -1;
int rear = -1;
void dequeue();
int main()
{
}
void dequeue(){
if(front == -1 && rear == -1){
cout<<"Queue Underflow !!\n";
}
else if(rear == front){
front = rear = -1;
}
else{
front++;
}
}
发布于 2021-05-10 20:01:50
你需要检查
else if(rear == front)
在做完front++
之后。否则,您将处于队列为空的状态(前面的==后面),并且不会检测到它。下一次出队将不起作用。
https://stackoverflow.com/questions/67469833
复制相似问题