Verse Library verse

10 Complete Code

Defines a complete queue class with methods for enqueue, dequeue, size checks, and element access.

extracted-snippets/documentation/en-us/uefn/stacks-and-queues-in-verse/10-complete-code.verse

# Source URL: https://dev.epicgames.com/documentation/en-us/uefn/stacks-and-queues-in-verse
# Local doc:  epic-docs/documentation/en-us/uefn/stacks-and-queues-in-verse.md
# Section:    Complete Code
queue<public>(t:type) := class:
    Elements<internal>:[]t = array{}

    # Adds a new element to the back of the queue by returning a
    # new queue with NewElement at the back.
    Enqueue<public>(NewElement:t):queue(t)=
        queue(t){Elements := Elements + array{NewElement}}

    # Removes the element at the front of the queue and returns a tuple of
    # both a new queue with the element removed and the removed element.
    Dequeue<public>()<decides><transacts>:tuple(queue(t),t)=
        FirstElement := Front[](queue(t){Elements := Elements.RemoveElement[0]}, FirstElement)

    # Returns the size of the queue
    Size<public>()<transacts>:int=
        Elements.Length

    # Succeeds if the queue is empty
    IsEmpty<public>()<decides><transacts>:void=
        Size() = 0

    # Returns the element at the front of the queue.
    Front<public>()<decides><transacts>:t=
        Elements[0]

    # Returns the element at the back of the queue.
    Rear<public>()<decides><transacts>:t=

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in