I take the C ++ data structures class, and the problem I'm working on is writing a client function that gets the length of the queue without changing the queue using the function prototype:
int GetLength(QueType queue);
In my opinion, it is very simple, because when you pass the queue object to a function, it works with the copy, so if I repeat and delete until it becomes empty, I know how many elements are in the queue.
QueType is a simple queue type provided by text, ItemType is defined as
typedef char ItemType;
My simple driver code is below:
#include "QueType.h"
using namespace std;
int GetLength(QueType queue);
int main()
{
ItemType item;
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;
}
My expected result:
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 . . .
But I get the following:
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: class 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()
{
maxQue = 501;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
QueType::~QueType()
{
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];
}
}
, , . , , , . , . !