Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2005

Tip: Looking for answers? Try searching our database.

AP exam priority queue

Thread view: 
Carol - 30 Jan 2005 23:34 GMT
This relates to the implementation of the priority queue for the AP
computer science exam in Java.  How do I implement a priority queue
using linked lists.  When an object is added using the enqueue method,
is it then inserted into the linked list in ascending/descending order?
Do you have a sample of code I can look at?

Thanks in advance.
Casey Hawthorne - 31 Jan 2005 00:33 GMT
Check "heaps"!

--
Regards,
Casey
Chris Smith - 31 Jan 2005 16:48 GMT
> This relates to the implementation of the priority queue for the AP
> computer science exam in Java.  How do I implement a priority queue
> using linked lists.  When an object is added using the enqueue method,
> is it then inserted into the linked list in ascending/descending order?
> Do you have a sample of code I can look at?

It's fairly simple to implement a priority queue using a
java.util.LinkedList.  Simply use a ListIterator to walk through the
list, and call listIterator.add(newObject) when you find the proper
place for the new item.

The data structure is perhaps less than ideal, though; insertion is O(n)
and removal is O(1).  Using a heap (a tree with special features),
insertion and removal can both be made O(log n).  Faster insertion and
slower removal might seem like a wash, but insertions and removals will
always be equally frequent in the long term.  To add AND remove y items
from the queue with about n items in the queue at any given time, the
linked list will take O(y * n), whereas the heap implementation will
take O(y * log n).

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.