I am taking a C++ data structures class and the problem I am working on is to write a client function that gets the length of the queue without changing the queue using the function prototype:
int GetLength(QueType queue); In my mind this is very simple because when you pass the queue object to a function it is working with a copy so if I iterate and dequeue until it's empty I know how many items are in the queue.
The QueType is a simple queue type provided by the 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; //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; } My expected output would be:
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 am getting this:
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() // 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]; } } The code gets the length but clearly the queue has been modified. If the object was passed by reference the queue would be empty but it's not, it just has garbage in each queue position. There is some concept that I am not understanding. Any help would be greatly appreciated!
QueTypeto get a definitive answer.isEmptyis defined as:return rear == tail;. If there's only 1 element in the queue, therear == tail, but the queue is not empty?