c – 获取简单队列的长度

我正在使用C数据结构类,我正在处理的问题是编写一个客户端函数来获取队列的长度而不使用函数原型更改队列:

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];
  }
}

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

最佳答案 如何覆盖Enqueue和Dequeue函数来递增和递减计数器…那么你就知道队列中有多少项.

点赞