Hey everyone. I'm working on a project and need to use queues, specifically I need to check if a user input number matches a number already in a queue, and then remove that number, ie:
12 | 34 | 45 | 56 |
Input number ==> 34 //the user types 34
12 | x | 45 | 56 | //34 is removed, then I need to close the gap;
12 | 45 | 56 | x |
I don't know much about queues and was wondering if anyone had a link or an example to show how to close the gap and bring the numbers in. I know how to remove the number that is input, I just cant close the gap because I'm getting confused with front and back and how it relates to the whole structure. Thanks.
That's not a queue then. A queue is made specifically that you can only push into the back and get from the front. I'd just use a list or a vector if you want to do that kind of random access stuff.
Sorry, you're right. I'll elaborate. I will need to put the front numbers into a temporary queue, and then cycle all the numbers back into the original queue in the correct order.
You don't need to use a second queue then, check the top of the queue, if it's what you want, simply pop it, if it's not what you want, pop it, and push it onto the bottom, repeat for every element. Even after you find it, you still need to pop off the top and push it onto the bottom for every element you haven't examined, otherwise the order will be messed up.
That makes more sense, but if I had a queue of constant size (5 for example), that wouldn't work because if it was a full queue it would try to push front on the full queue.