How to resolve the algorithm Queue/Definition step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Queue/Definition step by step in the Icon and Unicon 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 Icon and Unicon programming language
Source code in the icon programming language
# Use a record to hold a Queue, using a list as the concrete implementation
record Queue(items)
procedure make_queue ()
return Queue ([])
end
procedure queue_push (queue, item)
put (queue.items, item)
end
# if the queue is empty, this will 'fail' and return nothing
procedure queue_pop (queue)
return pop (queue.items)
end
procedure queue_empty (queue)
return *queue.items = 0
end
# procedure to test class
procedure main ()
queue := make_queue()
# add the numbers 1 to 5
every (item := 1 to 5) do
queue_push (queue, item)
# pop them in the added order, and show a message when queue is empty
every (1 to 6) do {
write ("Popped value: " || queue_pop (queue))
if (queue_empty (queue)) then write ("empty queue")
}
end
# Use a class to hold a Queue, with a list as the concrete implementation
class Queue (items)
method push (item)
put (items, item)
end
# if the queue is empty, this will 'fail' and return nothing
method take ()
return pop (items)
end
method is_empty ()
return *items = 0
end
initially () # initialises the field on creating an instance
items := []
end
procedure main ()
queue := Queue ()
every (item := 1 to 5) do
queue.push (item)
every (1 to 6) do {
write ("Popped value: " || queue.take ())
if queue.is_empty () then write ("empty queue")
}
end
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Haskell programming language
You may also check:How to resolve the algorithm Ordered words step by step in the R programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the POV-Ray programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the Clojure programming language
You may also check:How to resolve the algorithm Program name step by step in the Nemerle programming language