11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

106 • Chapter 3: <strong>Programming</strong> with ModulesPriority QueuesA useful data structure that can be implemented in an object-orientedway with modules is the priority queue. A priority queue is a containerdata structure that admits the following operations:• Test for an empty priority queue• Insert a prioritized item into a priority queue• Return (non-destructively) the highest-priority item in the priorityqueue• Delete the highest priority item from a priority queueDesign An object representation of priority queues has the followingmethods.emptytopinsertdeletetest for an empty priority queuereturn the highest-priority iteminsert a prioritized itemremove (and return) the highest priority itemThis representation leads directly to the following Maple type, whichcan be used to identify priority queues.> ‘type/PriorityQueue‘ := ’‘module‘( empty, top, insert,> delete )’:Constructor Implementation Priority queues can be implemented asMaple objects by writing a constructor for the objects.> PriorityQueue := proc( priority::procedure )> description "priority queue constructor";> local largs, lnargs;>> lnargs := nargs;> if lnargs > 1 then> largs := [ args[ 2 .. -1 ] ]> else> largs := []> end if;>> module()> description "a priority queue";> export empty, top, insert,> size, delete, init;> local heap, nitems,> bubbleup, bubbledown;>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!