Question: Define a function removeAll that removes all the first occurrence of an item in a circular queue without changing the order of the other elements

Define a function removeAll that removes all the first occurrence of an item in a circular queue without changing the order of the other elements in the queue. Use the following header:

template

void queueType::removeAll (queueType& Q, const Type& x)

Answer:

template

void queueType::removeAll(queueType& Q, const Type& x)

{

assert(!Q.isEmptyQueue());

int m = Q.queueFront;

bool found = false;

while (m<=Q.queueFront+Q.count-1 && !found)

{

if (Q.list[m%Q.maxQueueSize] == x)

{

found = true;

if (m%Q.maxQueueSize != Q.queueRear)

{ // you do not want to move items if you are at the end

for (int n=m%Q.maxQueueSize; n

{

Q.list[n%Q.maxQueueSize] = Q.list[(n+1)%Q.maxQueueSize];

}

}

Q.count--; // you have deleted an item

Q.queueRear--; // so update count and queueRear

if (Q.queueRear<0)

{

Q.queueRear = Q.maxQueueSize-1;

}

}

m++; // move to next item in the queue

}

}

Question: Would this template function remove all the occurences of the item?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!