I need to widdle down a queue of people until one person remains in the queue, that person is the chosen person for a task. The mothod I am writing take the queue of people and a number, which represents a number drawn from a hat. Then the queue is traversed and the people "count off" if they do not have the hat number they go back in the queue if they do they are removed from the queue. As they are removed from the queue, they are output to the screen. Currently, I am outputting the last person in the queue twice. How do I leave one person in the queue?

/** 
 * Displays a list of officers removed from the queue and also displays 
 * the name of the officer going for help. Retrieves officers from the 
 * officers queue, one at a time, counting as each is retrieved. If the 
 * count is not equal to hatNumber, the officer is put back into the
 * queue. When the count reaches hatNumber, that officer is removed from 
 * the queue permanently (i.e., not put back into the queue), and the 
 * officer's name is output to the screen. The last officer remaining in 
 * the queue is the one shown as going for help. 
 * @param officers - dynamic queue of Confederate officers in the Civil War 
 * @param hatNumber - the number drawn from the hat, used to count off 
*/
void countOff (LinkedQueue<string> officers, int count)
{
 //Write method here
 int hatNumber = count;
 int num = 0;
 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 = hatNumber - num;       
    }
 }//end while
cout << "The officer going for help is: " << lastMan << endl;
}//end countOff()

Recommended Answers

All 3 Replies

Could you change your while condition so that you don't stop when the queue is empty, but instead stop when queue.size() is one?

I don't have the queue.size() method, but yes essentially that is what I need to do. All I have for the queue is: enqueue(), dequeue, isEmpty(), and peekFront().

What happens if you call peekfront() on an empty queue?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.