使用数组的队列>>出栈后移动元素
我正在尝试使用数组来实现队列。这是我的代码:
#include <iostream.h>
#define SIZE 5
class queue
{
int *Queue, front, rear;
public:
queue() {
Queue = new int[SIZE];
front = rear = -1;
}
void push() {
if (rear == (SIZE-1)) {
cout<<"\n Overflow!";
} else {
rear++;
cout<<"\n Enter element: ";
cin>>Queue[rear];
}
}
void pop() {
if (front == rear) {
cout<<"\n Underflow!";
} else {
cout<<"\nElement popped: "<<Queue[++front];
}
}
void display() {
if (front == rear) {
cout<<"\n Queue Empty";
} else {
for(int i = (front+1); i<=rear; i++) {
cout<<Queue[i]<<" ";
}
}
}
};
int main()
{
int choice;
queue q;
while(choice != 4)
{
cout<<"\n\n Enter your choice :"
<<"\n 1. Push an element into Queue."
<<"\n 2. Pop an element from Queue."
<<"\n 3. Display the Queue."
<<"\n 4. Exit the program.\n\n";
cin>>choice;
switch (choice) {
case 1:
q.push();
break;
case 2:
q.pop();
break;
case 3:
q.display();
break;
case 4:
break;
}
}
return 0;
}
问题是,一旦遇到溢出,即使弹出一个元素后,后部仍然保持不变,并且当有一个可以移动的空闲空间时,不会添加另一个元素。
解决方案可能是将每个元素向前移动一位,以便最后有空位,但我在移动时遇到了麻烦。另外,如果我在达到溢出之前尝试在弹出 2-3 次后插入,那么即使队列中只有 3 个元素,它仍然会溢出。 我该如何解决这个问题?
I'm trying to implement a queue using an array. Here is my code:
#include <iostream.h>
#define SIZE 5
class queue
{
int *Queue, front, rear;
public:
queue() {
Queue = new int[SIZE];
front = rear = -1;
}
void push() {
if (rear == (SIZE-1)) {
cout<<"\n Overflow!";
} else {
rear++;
cout<<"\n Enter element: ";
cin>>Queue[rear];
}
}
void pop() {
if (front == rear) {
cout<<"\n Underflow!";
} else {
cout<<"\nElement popped: "<<Queue[++front];
}
}
void display() {
if (front == rear) {
cout<<"\n Queue Empty";
} else {
for(int i = (front+1); i<=rear; i++) {
cout<<Queue[i]<<" ";
}
}
}
};
int main()
{
int choice;
queue q;
while(choice != 4)
{
cout<<"\n\n Enter your choice :"
<<"\n 1. Push an element into Queue."
<<"\n 2. Pop an element from Queue."
<<"\n 3. Display the Queue."
<<"\n 4. Exit the program.\n\n";
cin>>choice;
switch (choice) {
case 1:
q.push();
break;
case 2:
q.pop();
break;
case 3:
q.display();
break;
case 4:
break;
}
}
return 0;
}
The thing is that once the overflow is met, even after popping an element the rear remains the same and another element is not added when there is a vacant space where it can go.
The solution for this could be to shift every element one place ahead so that there is empty spot at the end but I am having trouble with the shift. Also, if I try inserting after popping 2-3 times before reaching overflow then it still gives overflow even when there are only 3 elements in the queue.
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不会重置
前
或后
,它们会永远增加。overlflow 测试也是错误的:
你应该测试的是你是否要覆盖 front.我认为只保留
front
和元素数量N
会让您受益匪浅。那么上溢和下溢就变成了N>SIZE
和N==0
。然后,只需在弹出和推送时增加和减少N
即可。保持前面不变,但也保持对SIZE
取模。另外,正如评论中所写。无需移动数据。
You never reset
front
orrear
, they keep increasing forever.the overlflow test is also wrong:
what you should be testing is if your are about to overwrite front. I think you will benefit from keeping just
front
and the number of elementsN
instead. Then overflow and underflow becomesN>SIZE
andN==0
instead. Then just increase and decreaseN
as you pop and push. Keep front as it is but also keep it moduloSIZE
.Also, as written in a comment. No need to move around the data.
实现循环缓冲区以避免移动整个数据。
Implement a Circular Buffer to avoid the need to shift the whole data.