实现循环数组队列

重生

我要实现循环数组队列,但是我遇到了逻辑错误,但没有得到正确的结果。我需要在ArrayQueueP4.h中实现bool dequeue()的帮助。我怀疑这是否正确。

我已经尝试了不同的解决方案,并通过堆栈溢出和在线搜索了先前的问题,但是对于我所寻找的内容却没有任何想法

#ifndef ARRAY_QUEUE_P4_
#define ARRAY_QUEUE_P4_
#include "QueueInterface.h"
#include "PrecondViolatedExcept.h"

template<class ItemType>
class ArrayQueueP4 : public QueueInterface<ItemType>
{
private:
    static const int DEFAULT_CAPACITY = 50;
    ItemType items[DEFAULT_CAPACITY + 1]; // Array of queue items
    int front;                   // Index to front of queue
    int back;                    // Index to back of queue

public:
    ArrayQueueP4() : front(DEFAULT_CAPACITY),
        back(DEFAULT_CAPACITY) {};
    // Copy constructor and destructor supplied by compiler
    bool isEmpty() const;
    bool enqueue(const ItemType& newEntry);
    bool dequeue();

    /** @throw  PrecondViolatedExcept if queue is empty. */
    ItemType peekFront() const;
};

ArrayQueueP4.h is the header file for ArrayQueueP4.cpp
#include "ArrayQueueP4.h";

#include "PrecondViolatedExcept.h";

using namespace std;

template <class ItemType>
bool ArrayQueueP4 <ItemType>::isEmpty() const {
    return (front == back);
}

template <class ItemType>
bool ArrayQueueP4 <ItemType>::enqueue(const ItemType& newEntry) {

    if (!isEmpty())

    back = (back + 1) % DEFAULT_CAPACITY;
    items[back] = newEntry;
    back++;
    return true;


}

template<class ItemType>
bool ArrayQueueP4 <ItemType> ::dequeue() {
    bool result = false;
    if (!isEmpty()) {
        front = (front + 1) % DEFAULT_CAPACITY;
        front--;
        result = true;
    }
    return result;
}

template<class ItemType>
ItemType ArrayQueueP4<ItemType>::peekFront() const {

    if (isEmpty())
        throw PrecondViolatedExcept("peekFront() called with an empty queue.");
    else
        return items[front];

}

HERE is my main file main.cpp to test my code


#include <iostream>

#include "ArrayQueueP4.cpp";

using namespace std;

int main() {
    ArrayQueueP4<int> AP;
    AP.enqueue(1);
    AP.enqueue(2);
    AP.enqueue(3);

    /*LinkedQueueP1<int> LP;
    LP.enqueue(1);
    LP.enqueue(2);*/

    cout << "PEEK FRONT: " << AP.peekFront();
    //cout << "PEEK FRONT: " << LP.peekFront();

    system("pause");
    return 0;
}

根据我的主程序文件,当我调用enqueue函数时,输出现在应该为1。但是当我使用dequeue()删除第一项时,得到的结果不是-2,而是得到-858993460,这不是我的答案。我不知道这是否是队列的行为,但是删除第一个数字时,第二个数字是否不应该成为行中的下一个第一项?

Ka·肯本

根据您的描述,您frontback定义了一个范围,例如,它front是队列中可用的第一个元素,并且back是“ pass-the-end”索引。然后根据这些定义,代码应如下所示:

template <class ItemType>
bool ArrayQueueP4 <ItemType>::enqueue(const ItemType& newEntry) {
    // Check if queue is full
    if ((back + 1) % (DEFAULT_CAPACITY + 1) == front) return false;
    // Append element at one-pass-end position
    items[back] = newEntry;
    // Update the one-pass-end index (back)
    // All your modulo operation should be dealing with DEFAULT_CAPACITY + 1
    // because that is the size of your array
    back = (back + 1) % (DEFAULT_CAPACITY + 1);
    return true;
}

template<class ItemType>
bool ArrayQueueP4 <ItemType> ::dequeue() {
    // Dequeue fail if the queue is empty
    if (isEmpty()) return false;
    front = (front + 1) % (DEFAULT_CAPACITY + 1);
    return true;
}

另外,提醒一下,您的代码没有考虑资源管理(尽管它适用于大多数类型,并且似乎没有犯任何错误)。当一个项目出队时,它的相应资源应该被释放。作为练习,想想那里的情形ItemTypestd::unique_ptr(或std::shared_ptr)。这可能不是您的老师想要的,但这是一个好习惯。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Trove库队列实现

来自分类Dev

使用双端队列在C ++中实现循环缓冲区

来自分类Dev

数组队列Java

来自分类Dev

我可以使用“ XEP-0142:工作组队列”之类的“延迟” XEP吗?

来自分类Dev

实现循环队列的细微错误

来自分类Dev

我应该在哪里释放组队列?

来自分类Dev

在数组队列上使用包含时,控制台将返回False。为什么?

来自分类Dev

在LinkedList中实现队列

来自分类Dev

循环实现接口的项目数组

来自分类Dev

阻塞队列的实现

来自分类Dev

堆栈和队列:使用数组更容易实现?

来自分类Dev

如何在Spring Boot中实现循环队列使用者

来自分类Dev

一种实现循环优先级队列的有效方法?

来自分类Dev

双端队列数组和列表实现之间的区别

来自分类Dev

队列的数组实现:奇怪的输出

来自分类Dev

使队列数据结构作为数组实现的问题

来自分类Dev

如何实现队列,

来自分类Dev

队列实现为数组

来自分类Dev

C ++队列实现错误

来自分类Dev

是否可以在C ++中创建对象数组队列?

来自分类Dev

Java:不可转换类型错误(使用数组队列)

来自分类Dev

如何在Spring Boot中实现循环队列使用者

来自分类Dev

为什么要使用链接列表而不是数组或向量实现来实现堆栈或队列?

来自分类Dev

C++ 循环数组队列 setCapacity

来自分类Dev

通过数组实现的线性队列

来自分类Dev

Java - isFull 方法循环数组队列

来自分类Dev

在 C++ 中创建一个数组队列

来自分类Dev

php中的两个数组队列算法

来自分类Dev

使用 C++ 实现队列和调整数组大小