Community Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

Example Code Submission: Priority Queue

Summary:

This example illustrates how to use LVOOP to create a priority queue.  A priority queue is a queue that automatically sorts the elements according to their assigned priority.  If a high priority element is on the queue, it will be dequeued before a lower priority element, regardless of which element was actually placed on the queue first.  PriorityQueues are a generally useful component that can be applicable to any game or test application.

Note:  This simplified example is intended to show the core functionality of the PriorityQueue class.  As such, only four methods are given (Create, Enqueue, Dequeue, and Destroy) and options such as queue size and named queues are not implemented.  The Dequeue method has a disabled case showing how to implement code that allows the user to input a timeout value.  Methods to implement the other queue primitives along with all their available options are easily created.  They are omitted here for clarity.

Function:

When fully implemented this class duplicates the behavior of the queue primitives with the added feature of having the ability to set priority levels to each queue element.  The implementation uses an array of queue refnums--one for each priority level.  Encapsulating the array in a class allows the user to treat the PriorityQueue just like a normal queue.

There are two usage examples included.  Example 1 demonstrates the priority queue's basic capability of sorting queue elements.  Example 2 illustrates how a priority queue could be used in an application.

Steps to execute code (Example 1):

  1. Modify the priority levels for the elements to be place on the queue.  (Default values are configured to output the letters in alphabetical order.)
  2. Run the vi.
  3. Observe the elements are dequeued in priority order.

Screenshots (Example 1):

PriorityQueue Usage Example1_FP.png

Steps to execute code (Example 2):

  1. Run the vi.
  2. Use the buttons to enqueue wait messages.  Wait 0.5 has the highest priority, Wait 1.5 has the next highest, and Wait 3 has the lowest priority.
  3. Observe that higher priority messages are always placed before lower priority messages.

Screenshots (Example 2):

PriorityQueue Usage Example2_FP.png

VI Snippet:

Not applicable.  Download the project and open it in Labview 2009 or later.

Zipped Project attached below

Contributors