How to resolve the algorithm Queue/Definition step by step in the REBOL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Queue/Definition step by step in the REBOL programming language
Table of Contents
Problem Statement
Implement a FIFO queue. Elements are added at one side and popped from the other in the order of insertion.
Operations:
Errors:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Queue/Definition step by step in the REBOL programming language
Source code in the rebol programming language
rebol [
Title: "FIFO"
URL: http://rosettacode.org/wiki/FIFO
]
; Define fifo class:
fifo: make object! [
queue: copy []
push: func [x][append queue x]
pop: func [/local x][ ; Make 'x' local so it won't pollute global namespace.
if empty [return none]
x: first queue remove queue x]
empty: does [empty? queue]
]
; Create and populate a FIFO:
q: make fifo []
q/push 'a
q/push 2
q/push USD$12.34 ; Did I mention that REBOL has 'money!' datatype?
q/push [Athos Porthos Aramis] ; List elements pushed on one by one.
q/push [[Huey Dewey Lewey]] ; This list is preserved as a list.
; Dump it out, with narrative:
print rejoin ["Queue is " either q/empty [""]["not "] "empty."]
while [not q/empty][print [" " q/pop]]
print rejoin ["Queue is " either q/empty [""]["not "] "empty."]
print ["Trying to pop an empty queue yields:" q/pop]
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the REXX programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Scala programming language
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Wren programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Go programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Action! programming language