Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to delete an element from the queue efficiently

 
Veerabhadraiah As
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

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..

thanks in advance
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic