"Queue" (Data Structure)
"Queue" (Data Structure)
"Queue"
represents a queue of expressions.
Details
- A queue is a collection of elements that supports first-in, first-out insertion and removal:
-
CreateDataStructure["Queue"] create a new empty "Queue" CreateDataStructure["Queue",elems] create a new "Queue" containing elems Typed[x,"Queue"] give x the type "Queue" - For a data structure of type "Queue", the following operations can be used:
-
ds["Copy"] return a copy of ds time: O(n) ds["DropAll"] drop all the elements from ds time: O(n) ds["Elements"] return a list of the elements of ds time: O(n) ds["EmptyQ"] True, if ds is empty time: O(1) ds["Fold",fun] apply fun to the elements of ds, accumulating a result time: O(n) ds["Fold",fun,init] apply fun to the elements of ds, starting with init, accumulating a result time: O(n) ds["Length"] number of elements in ds time: O(1) ds["Peek"] first element in ds time: O(1) ds["Pop"] remove the first element of ds time: O(1) ds["Push",x] add x to the end of ds time: O(1) ds["PushList",elems] add elems to the end of ds time: O(nelems) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (2)
A new "Queue" can be created with CreateDataStructure:
ds = CreateDataStructure["Queue"]ds["EmptyQ"]Add an element to the end of ds:
ds["Push", f[1]]ds["EmptyQ"]ds["Length"]Add another element and peek. This shows the first element:
ds["Push", f[2]];
ds["Peek"]ds["Pop"]Return an expression version of ds:
Normal[ds]It is fast to put elements into a queue:
ds = CreateDataStructure["Queue"];
Do[ds["Push", i], {i, 1000}]A visualization of the data structure can be generated:
ds["Visualization"]ds["Fold", Plus, 0]Scope (1)
Information (1)
A new "Queue" can be created with CreateDataStructure:
ds = CreateDataStructure["Queue"]Information about the data structure ds:
Information[ds]See Also
CreateDataStructure DataStructure
Data Structures: Stack Deque RingBuffer LinkedList DynamicArray PriorityQueue
Related Guides
History
Introduced in 2020 (12.1)