dequeue all but one item from queue

How do I use a while loop to dequeue all but one item from a queue? I only have enqueue(), dequeue(), isEmpty() and peekFront() methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void countOff (LinkedQueue<string> officers, int count)
{
 //Write method here
 int hatNumber = count;
 int num = 1;
 string lastMan;
 string tempOfficer;

 while(!officers.isEmpty())
 {
    if(num != hatNumber)
    {
       tempOfficer = officers.peekFront();
       officers.dequeue();
       officers.enqueue(tempOfficer);
       num++;
    }
    else
    {
       lastMan = officers.peekFront();
       officers.dequeue();
       num = 1;
       cout << "The officers removed from the queue are: " << lastMan << endl;      
    }
 }//end while
cout << "The officer going for help is: " << lastMan << endl;
}//end countOff() 
Dequeue an item. If the queue is now empty, enqueue the item you just dequeued. If the queue is not empty, repeat.
Last edited on
Thank you. That part is now working as intended.
Topic archived. No new replies allowed.