Getting the length of a simple queue

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;  //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 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()          // 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];
  }
}

, , . , , , . , . !

+4
4

Enqueue Dequeue ... , .

+1

length size QueType, .

foreach. . GetLength , . , , .

+1

, , , , , , , , , tmp , .

-:

while(!Q.empty())
    tmp.enqueue(Q.dequeue())
    counter++

while(!tmp.empty())
    Q.enqueue(tmp.dequeue())

θ(2n*copy_cost), , , O(n), - , , .

0

, QueType , GetLength().

QueType::~QueType()         // Class destructor
{
  delete [] items;
}

(), .

You are correct in that since the queue argument of the GetLength () function is passed by value, you do not need to copy the values ​​of the queue, since the internal state will not be changed in the original variable.

You will find that if you comment out the deletion of [] in the destructor, the GetLength () function will work as expected.

0
source

All Articles