Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实现循环数组队列_C++ - Fatal编程技术网

C++ 实现循环数组队列

C++ 实现循环数组队列,c++,C++,我要实现一个循环数组队列,但是我有一个逻辑错误,并且没有得到正确的结果。我需要在ArrayQueueP4.h中实现bool dequeue()的帮助。我怀疑它是否正确 我尝试了不同的解决方案,并搜索了以前关于堆栈溢出和联机的问题,但它并没有给我任何关于我在寻找什么的想法 #ifndef ARRAY_QUEUE_P4_ #define ARRAY_QUEUE_P4_ #include "QueueInterface.h" #include "PrecondViolatedExcept.h" t

我要实现一个循环数组队列,但是我有一个逻辑错误,并且没有得到正确的结果。我需要在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;
}
#ifndef数组_队列(P4)_
#定义数组\u队列\u P4_
#包括“QueueInterface.h”
#包括“预建议的概念h”
模板
类ArrayQueueP4:公共队列接口
{
私人:
静态常数int默认容量=50;
ItemType项[默认容量+1];//队列项数组
int front;//到队列前面的索引
int back;//到队列后面的索引
公众:
ArrayQueueP4():前端(默认容量),
后退(默认容量){};
//复制编译器提供的构造函数和析构函数
bool isEmpty()常量;
bool排队(const ItemType和newEntry);
bool-dequeue();
/**@throw predviolatedexcept如果队列为空*/
ItemType peekFront()常量;
};
ArrayQueueP4.h是ArrayQueueP4.cpp的头文件
#包括“ArrayQueueP4.h”;
#包括“预建议的概念h”;
使用名称空间std;
模板
布尔数组队列p4::isEmpty()常量{
返回(前=后);
}
模板
bool ArrayQueueP4::排队(const ItemType和newEntry){
如果(!isEmpty())
后退=(后退+1)%DEFAULT\u容量;
项目[返回]=新条目;
back++;
返回true;
}
模板
bool ArrayQueueP4::dequeue(){
布尔结果=假;
如果(!isEmpty()){
前端=(前端+1)%DEFAULT_容量;
前面--;
结果=真;
}
返回结果;
}
模板
ItemType ArrayQueueP4::peekFront()常量{
if(isEmpty())
抛出predviolatexcept(“使用空队列调用的peekFront()”);
其他的
归还物品[正面];
}
这是我的主文件main.cpp,用于测试我的代码
#包括
#包括“ArrayQueueP4.cpp”;
使用名称空间std;
int main(){
ArrayQueueP4-AP;
AP.排队(1);
AP.排队(2);
AP.排队(3);
/*LinkedQueueP1 LP;
LP.排队(1);
排队(2)*/

cout根据您的描述,您的
front
back
定义了一个范围,使得
front
是队列中可用的第一个元素,
back
是“pass the end”索引。根据这些定义,代码应该如下所示:

模板
bool ArrayQueueP4::排队(const ItemType和newEntry){
//检查队列是否已满
if((back+1)%(DEFAULT_CAPACITY+1)=front)返回false;
//在一个过程结束位置追加元素
项目[返回]=新条目;
//更新一次结束索引(返回)
//所有模运算都应处理默认的_容量+1
//因为这是数组的大小
后退=(后退+1)%(默认容量+1);
返回true;
}
模板
bool ArrayQueueP4::dequeue(){
//如果队列为空,则出列失败
if(isEmpty())返回false;
前端=(前端+1)%(默认容量+1);
返回true;
}


另外,作为提醒,您的代码没有考虑资源管理(尽管它适用于大多数类型,并且似乎没有任何错误)。当项目出列时,应该释放相应的资源。作为练习,请考虑以下场景:
ItemType
is
std::unique\u ptr
(或
std::shared_ptr
)。这可能不是您的老师想要的,但这是一个很好的实践。

根据您的描述,您的
front
back
定义了一个范围,使得
front
是队列中可用的第一个元素,
back
是“传递结束”索引。然后根据这些定义,代码应该如下所示:

模板
bool ArrayQueueP4::排队(const ItemType和newEntry){
//检查队列是否已满
if((back+1)%(DEFAULT_CAPACITY+1)=front)返回false;
//在一个过程结束位置追加元素
项目[返回]=新条目;
//更新一次结束索引(返回)
//所有模运算都应处理默认的_容量+1
//因为这是数组的大小
后退=(后退+1)%(默认容量+1);
返回true;
}
模板
bool ArrayQueueP4::dequeue(){
//如果队列为空,则出列失败
if(isEmpty())返回false;
前端=(前端+1)%(默认容量+1);
返回true;
}


另外,作为提醒,您的代码没有考虑资源管理(尽管它适用于大多数类型,并且似乎没有任何错误)。当项目出列时,应该释放相应的资源。作为练习,请考虑以下场景:
ItemType
is
std::unique\u ptr
(或
std::shared_ptr
)。这可能不是您的老师想要的,但这是一个很好的做法。

听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它偏离您期望的位置。如果您要进行任何编程,这是一个必不可少的工具。Further reading:这就是我所做的。我的问题来自于这些函数:bool isEmpty()const、bool enqueue(const ItemType&newEntry)和bool dequeue()。我还不知道如何解决它,但我注意到这就是我的问题的根源。不久前我编辑了我的帖子以澄清问题。
enqueue()
看起来可疑。唯一一次将写入索引(
向后移动
)是当队列不为空时。如果不执行
向后=(向后+1),读者如何知道您已将元素放入队列中%DEFAULT\u CAPACITY;
每当你把东西放进队列时?@Ted Lyngom谢谢你联系我。我已经修好了bool isEmp