gpt4 book ai didi

c++ - 获取简单队列的长度

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:36 24 4
gpt4 key购买 nike

我正在学习一个 C++ 数据结构类,我正在研究的问题是编写一个客户端函数来获取队列的长度,而无需使用函数原型(prototype)更改队列:

int GetLength(QueType queue);

在我看来,这非常简单,因为当您将队列对象传递给一个函数时,它正在使用一个拷贝,所以如果我迭代并出列直到它为空,我就知道队列中有多少项。

QueType是文中提供的简单队列类型,ItemType定义为

typedef char ItemType;

我的简单驱动程序代码如下:

#include "QueType.h"
using namespace std;

int GetLength(QueType queue);

int main()
{

ItemType item; //typedef char
//initialize and fill the queue
QueType que(5);
que.Enqueue('A');
que.Enqueue('B');
que.Enqueue('C');
que.Enqueue('D');
que.Enqueue('E');

cout << "The length of the queue is " << GetLength(que) << endl;

while (!que.IsEmpty())
{
que.Dequeue(item);
cout << "Dequeue item of queue: " << item << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}

int GetLength(QueType queue)
{
int cnt = 0;
ItemType item;
while (!queue.IsEmpty())
{
queue.Dequeue(item);
cout << "Dequeue item of local copy of queue: " << item << endl;
cnt++;
}
return cnt;
}

我的预期输出是:

Dequeue item of local copy of queue: A
Dequeue item of local copy of queue: B
Dequeue item of local copy of queue: C
Dequeue item of local copy of queue: D
Dequeue item of local copy of queue: E
The length of the queue is 5
Dequeue item of queue: A
Dequeue item of queue: B
Dequeue item of queue: C
Dequeue item of queue: D
Dequeue item of queue: E
Press any key to continue . . .

但是我得到了这个:

Dequeue item of local copy of queue: A
Dequeue item of local copy of queue: B
Dequeue item of local copy of queue: C
Dequeue item of local copy of queue: D
Dequeue item of local copy of queue: E
The length of the queue is 5
Dequeue item of queue: p
Dequeue item of queue: ↨
Dequeue item of queue: 7
Dequeue item of queue:
Dequeue item of queue: ─
Press any key to continue . . .

QueType.h: 类 FullQueue {};

class EmptyQueue
{};
typedef char ItemType;
class QueType
{
public:
QueType();
QueType(int max);
~QueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
private:
int front;
int rear;
ItemType* items;
int maxQue;
};

QueType.cpp:

#include "QueType.h"

QueType::QueType(int max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
QueType::QueType() // Default class constructor
{
maxQue = 501;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
QueType::~QueType() // Class destructor
{
delete [] items;
}

void QueType::MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}

bool QueType::IsEmpty() const
{
return (rear == front);
}

bool QueType::IsFull() const
{
return ((rear + 1) % maxQue == front);
}

void QueType::Enqueue(ItemType newItem)
{
if (IsFull())
throw FullQueue();
else
{
rear = (rear +1) % maxQue;
items[rear] = newItem;
}
}

void QueType::Dequeue(ItemType& item)
{
if (IsEmpty())
throw EmptyQueue();
else
{
front = (front + 1) % maxQue;
item = items[front];
}
}

代码获取长度但显然队列已被修改。如果该对象是通过引用传递的,则队列将为空,但事实并非如此,它只是在每个队列位置都有垃圾。有一些我不理解的概念。任何帮助将不胜感激!

最佳答案

重写入队和出队函数来递增和递减计数器怎么样...然后你就会知道队列中有多少项目。

关于c++ - 获取简单队列的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com