This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Help with double ended queue

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm doing a project, and this involves a double ended queue where i must put some data into the tail, and it will get 'promoted' to the head after i call a time function. so its something like this:

head 1 2 3 4a tail (a is the data)
time()
head 1 2 3a 4 tail
time()
head 1 2a 3 4 tail
time()
head 1a 2 3 4 tail

but the problem is, since the code i have (and any other that i've found on the net) says it like this "head = tail = null" when i'm adding data for the 1st time, it makes my queue become:

head 1a tail

so now i have problems moving it to the head, 'cause i cant use code like queue.next or queue.previous since it kept giving me a null pointer exception. can anyone help? thanks
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to check out java.util.LinkedList.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The PriorityBuffer class from Jakarta Commons Collections may also be of interest. My thinking is that, rather than physically moving items towards the head of the queue each time you can simply increase their priority in the buffer (queue). The buffer allows you to remove the item with the highest priority; effectively the one at the head of the queue.

It may be useful or maybe not.

Jules
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tan kian, for this project, is it a requirement that you implement the data structure yourself?
[ September 11, 2004: Message edited by: Dirk Schreckmann ]
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope, its given to us on the specs that we use a DB ended queue.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic