This module provides functionalities as one-way queue. The worst-case time complexity of enqueue and dequeue is O(1).
Represents queue with no element.
empty |> isEmpty --> True
empty |> head --> Nothing
empty |> rear --> NothingConverts a List to a Queue in a way that last-in element in the list becomes first-out element in the queue.
list : List String
list =
[]
|> (::) "m"
|> (::) "l"
|> (::) "e" -- Here is last-in element.
queue : Queue String
queue =
fromListLIFO list
head queue --> Just "e"
rear queue --> Just "m"Converts a List to a Queue in a way that first-in element in the list becomes first-out element in the queue.
list : List String
list =
[]
|> (::) "m" -- Here is first-in element.
|> (::) "l"
|> (::) "e"
queue : Queue String
queue =
fromListFIFO list
head queue --> Just "m"
rear queue --> Just "e"Returns True if and only if the queue has no element.
empty
|> isEmpty
-->
True
empty
|> enqueue "test"
|> isEmpty
-->
FalseCompare the contents and the order in two queues. Returns True if and only if all the elements contained and the orders are the same.
Note that comparison in == may return false even if the contents are the same, because there is some redunduncy in the inner structure of the queue. Takes O(n) time.
q1 : Queue String
q1 =
empty
|> enqueue "e"
|> enqueue "l"
|> enqueue "m"
|> dequeue
q2 : Queue String
q2 =
empty
|> enqueue "e"
|> dequeue
|> enqueue "l"
|> enqueue "m"
q3 : Queue String
q3 =
empty
|> enqueue "m"
|> enqueue "l"
|> enqueue "e"
|> dequeue
q1 |> isEqualTo q2 --> True
q1 |> isEqualTo q3 --> FalseReturns the oldest element in the queue wrapped in Maybe. Takes O(1) time.
queue : Queue String
queue =
empty
|> enqueue "e"
|> enqueue "l"
|> enqueue "m"
head queue --> Just "e"
head empty --> NothingReturns the newest element in the queue wrapped in Maybe. Takes _O(log_n)_ time at worst case.
queue : Queue String
queue =
empty
|> enqueue "e"
|> enqueue "l"
|> enqueue "m"
rear queue --> Just "m"
rear empty --> NothingReturns a queue with the given element attached on the rear side.
empty
|> enqueue "e"
-- first in
|> enqueue "l"
|> enqueue "m"
|> toListFIFO
-->
[ "e", "l", "m" ]Returns a queue with the oldest element removed.
intermediateQueue =
empty
|> enqueue "e" -- this will be removed
|> enqueue "l" -- first-in element in the final queue.
|> enqueue "m"
finalQueue =
dequeue intermediateQueue
finalQueue
|> toListFIFO -->
[ "l"
, "m"
]Updates all elements in the Queue with given function.
[1, 2, 3]
|> fromListLIFO
|> map (\x -> x * x)
|> map (String.fromInt)
|> toListLIFO --> ["9","4","1"]Converts a Queue to a List in a way that last-in element in the queue becomes first-out element in the list.
queue : Queue String
queue =
empty
|> enqueue "e"
|> enqueue "l"
|> enqueue "m" --Here is last-in element.
toListLIFO queue --> ["m","l","e"]Converts a Queue to a List in a way that first-in element in the queue becomes first-out element in the list.
queue : Queue String
queue =
empty
|> enqueue "e" --Here is last-in element.
|> enqueue "l"
|> enqueue "m"
toListFIFO queue --> ["e","l","m"]
Represents one-way queue for any contents.