使用数组的队列>>出栈后移动元素

发布于 2025-01-01 23:37:18 字数 1742 浏览 0 评论 0原文

我正在尝试使用数组来实现队列。这是我的代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2025-01-08 23:37:18

您永远不会重置,它们会永远增加。

overlflow 测试也是错误的:

if (rear == (SIZE-1)) {
        cout<<"\n Overflow!";

你应该测试的是你是否要覆盖 front.我认为只保留 front 和元素数量 N 会让您受益匪浅。那么上溢和下溢就变成了 N>SIZEN==0 。然后,只需在弹出和推送时增加和减少 N 即可。保持前面不变,但也保持对 SIZE 取模。

另外,正如评论中所写。无需移动数据。

You never reset front or rear, they keep increasing forever.

the overlflow test is also wrong:

if (rear == (SIZE-1)) {
        cout<<"\n Overflow!";

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 elements N instead. Then overflow and underflow becomes N>SIZE and N==0 instead. Then just increase and decrease N as you pop and push. Keep front as it is but also keep it modulo SIZE.

Also, as written in a comment. No need to move around the data.

岁月蹉跎了容颜 2025-01-08 23:37:18

实现循环缓冲区以避免移动整个数据。

Implement a Circular Buffer to avoid the need to shift the whole data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文