How to delete an element from the queue efficiently
posted 5 years ago
have a basic problem regarding queue. I am trying to implement Queue in Java using integer array. Normally what we when removing an element from the front of the queue, we will simply increment the front value by one so that the front pointer will point to next location.
But what my worry is, lets say i have int array of size 10. I will insert 10 elements to this queue, delete 8 elements from the queue. so my from will now be pointing at location 8 and rear at 9. so my queue can now hold only 2 elements???!!! Its crazy.
I even tried keeping the front pointer pointing always at the 0th location and shift all the elements to left after deleting the element at the front. This works but what about the efficiency when i have say 1 million elements?
Is there any better way to handle this case?
Hope you guys understood my problem, am waiting for your valuable solutions..