queue data structure
queue[new](obj1, ..., objn)
values to be inserted into the queue
A queue is a computer science data structure for storing objects that are to be processed in a "first come, first served" order. You place objects in a queue and, later, remove them for processing in the same order that they were placed in the queue. It is analogous to a theater line-up: the first person to arrive at the line is the first person to gain admission. For this reason, queues are often called FIFOs ("first in -- first out").
To place objects into a container, and then access them in the opposite order, use a stack.
Because you can only add items to the end of a queue, and remove them from the front, operations on queues are very efficient.
In Maple, queues are heterogeneous; that is, any valid Maple object may be placed in a queue, and a queue may contain objects of different types. (In the examples, below, a queue containing both strings and integers is used.)
In general, you should use only the operations described in this help page to manipulate queues. If you access directly the internal representation of queues, the results of subsequent queue operations are undefined.
Use type(obj, 'queue') to test whether a Maple object obj is of type queue.
A queue is created initially by using the procedure queue[new]. It returns a queue that is initialized and ready for use. Called with no arguments, new creates an empty queue. You can also create a queue initialized with a number of objects obj1, obj2, ..., objn by invoking new as new(obj1, obj2,..., objn). The queue that is returned will contain exactly the objects obj1, obj2, ..., objn, with obj1 at the front of the queue and objn at the back, and all of the objects in the specified order. The objects obj1, obj2, ..., objn may be Maple objects of any type, and need not all be of the same type.
To determine whether there are any objects left in a queue, awaiting processing, you can used the function empty. The call empty(Q) returns true if the queue Q contains no items, and false otherwise. The argument Q must be a queue, returned by a previous call to new.
Items may be placed on a queue by using the procedure enqueue. To insert the object obj at the end of a queue Q, you call the procedure as enqueue(Q, obj). The argument Q must be a queue, returned by a previous call to new.
To remove the object at the front of a queue Q, use dequeue(Q). If Q is nonempty, the object at the front of Q is returned. It is an error to attempt to remove an item from an empty queue. You should check that your queue is not empty using empty before calling this function. The argument Q must be a queue, returned by a previous call to new.
To examine the object at the front of a queue Q, without removing it, use front(Q). If the queue is nonempty, the object at the front of the queue is returned; otherwise, an error is signalled. You should check that your queue is not empty using empty before calling this function. The argument Q must be a queue, returned by a previous call to new.
The number of items in a queue Q may be queried using the function length. If Q is a queue, then length(Q) returns the number of items in Q. The argument Q must be a queue, returned by a previous call to new.
A queue Q may be cleared (that is, all objects removed from it) with clear(Q). This operation is done in linear time. The argument Q must be a queue, returned by a previous call to new.
The order in which items are stored in a queue Q may be reversed using reverse(Q).
An implementation of queues as objects is available using the SimpleQueue constructor.
To use a queue function, either define that specific function alone by the command with(queue,function), or define all queue functions by the command with(queue).
Q ≔ new⁡A,B,C:
Error, (in queue:-front) empty queue
MathFolks ≔ new⁡:
Download Help Document
What kind of issue would you like to report? (Optional)