Home > Net >  School is making us find the size of a queue without using .size() in our function
School is making us find the size of a queue without using .size() in our function

Time:02-21

I'm not sure how to find the size of a queue without using size() but our school is making us write a program and we are not allowed to use size().

CodePudding user response:

Copy to another queue and back again and count along the way.

int count = 0;
while (q.empty() == false) {
   q2.push(q.front());
   q.pop();
   count  ;
}
while (q2.empty() == false) {
   q.push(q2.front());
   q2.pop();
}

CodePudding user response:

I suggest function my_length(input_queue) with a loop that pops from your input queue, pushing to a temporary queue, counting as it does so, and then pops from that temporary queue back to the input queue.

  •  Tags:  
  • c
  • Related