Queues and Deques

API

true or false The category of queues. true or false The category of deques. Add object to the back of deque. Add object to the front of deque. object Remove an element from the back of deque and return it. object Remove an element from the front of deque and return it. For queues, this is just an alias for PushBack Add object to queue. object Remove an object from the front of queue and return it. Allocated storage capacity of queue. Allocated storage capacity of deque. Number of elements in queue. Number of elements in deque.
Deques implemented using plain lists

datastructures implements deques using a circular buffer stored in a &GAP; a plain list, wrapped in a positional object ().

The five positions in such a deque Q have the following purpose

Q![1] - head, the index in Q![5] of the first element in the deque Q![2] - tail, the index in Q![5] of the last element in the deque Q![3] - capacity, the allocated capacity in the deque Q![4] - factor by which storage is increased if capacity is exceeded Q![5] - GAP plain list with storage for capacity many entries

Global constants QHEAD, QTAIL, QCAPACITY, QFACTOR, and QDATA are bound to reflect the above.

When a push fills the deque, its capacity is resized by a factor of QFACTOR using PlistDequeExpand. A new empty plist is allocated and all current entries of the deque are copied into the new plist with the head entry at index 1.

The deque is empty if and only if head = tail and the entry that head and tail point to in the storage list is unbound. a deque Constructor for plist based deques. The optional argument capacity must be a positive integer and is the capacity of the created deque, and the optional argument factor must be a rational number greater than one which is the factor by which the storage of the deque is increased if it runs out of capacity when an object is put on the queue. Push object to the front of deque. Push object to the back of deque. object or fail Pop object from the front of deque and return it. If deque is empty, returns fail. object or fail Pop object from the back of deque and return it. If deque is empty, returns fail. object or fail Returns the object at the front deque without removing it. If deque is empty, returns fail. object or fail Returns the object at the back deque without removing it. If deque is empty, returns fail. Helper function to expand the capacity of deque by the configured factor.

Queues are linear data structure that allow adding elements at the end of the queue, and removing elements from the front. A deque is a double-ended queue; a linear data structure that allows access to objects at both ends.

The API that objects that lie in and must implement the API set out below.

datastructures provides