Queue/Definition: Difference between revisions
Content added Content deleted
Line 4,787: | Line 4,787: | ||
echo $foo->pop(); //Throws an exception |
echo $foo->pop(); //Throws an exception |
||
</lang> |
</lang> |
||
=={{header|Picat}}== |
|||
First variant. |
|||
<lang Picat>go => |
|||
println("Test 1"), |
|||
queue_test1, |
|||
nl. |
|||
empty(Q) => Q = []. |
|||
push(Queue, Value) = Q2 => |
|||
Q2 = [Value] ++ Queue. |
|||
pop(Q,_) = _, Q==[] ; var(Q) => |
|||
throw $error(empty_queue,pop,'Q'=Q). |
|||
pop(Queue,Q2) = Queue.last() => |
|||
Q2 = [Queue[I] : I in 1..Queue.len-1]. |
|||
queue_test1 => |
|||
% create an empty queue |
|||
println("Start test 2"), |
|||
empty(Q0), |
|||
printf("Create queue %w%n%n", Q0), |
|||
% add numbers 1 and 2 |
|||
println("Add numbers 1 and 2 : "), |
|||
Q1 = Q0.push(1), |
|||
Q2 = Q1.push(2), |
|||
% display queue |
|||
printf("Q2: %w\n\n", Q2), |
|||
% pop element |
|||
V = Q2.pop(Q3), |
|||
% display results |
|||
printf("Pop : Value: %w Queue: %w\n\n", V, Q3), |
|||
% test the queue |
|||
print("Test of the queue: "), |
|||
( Q3.empty() -> println("Queue empty"); println("Queue not empty") ), |
|||
nl, |
|||
% pop the elements |
|||
print("Pop the queue : "), |
|||
V1 = Q3.pop(Q4), |
|||
printf("Value %w Queue : %w%n%n", V1, Q4), |
|||
println("Pop empty queue:"), |
|||
catch(_V = Q4.pop(_Q5),Exception,println(Exception)), |
|||
nl, |
|||
println("\nEnd of tests.").</lang> |
|||
{{out}} |
|||
<pre>Test 1 |
|||
Create queue [] |
|||
Add numbers 1 and 2 : |
|||
Q2: [2,1] |
|||
Pop : Value: 1 Queue: [2] |
|||
Test of the queue: Queue not empty |
|||
Pop the queue : Value 2 Queue : [] |
|||
Pop empty queue: |
|||
error(empty_queue,pop,Q = []) |
|||
End of tests.</pre> |
|||
Another approach is to always returns the queue which makes command chaining possible, e,g, |
|||
<pre> |
|||
Q := Q.push2(1).push2(2), |
|||
Q := Q.pop2(V1).pop2(V2) |
|||
</pre> |
|||
<lang Picat>go2 => |
|||
println("Test 2"), |
|||
queue_test2, |
|||
nl. |
|||
empty2() = []. |
|||
push2(Queue, Value) = Q2 => |
|||
Q2 = [Value] ++ Queue. |
|||
pop2(Q,_) = _, Q==[] ; var(Q) => |
|||
throw $error(empty_queue,pop,'Q'=Q). |
|||
pop2(Queue,V) = [Queue[I] : I in 1..Queue.len-1] => |
|||
V = Queue.last(). |
|||
queue_test2 => |
|||
% create an empty queue |
|||
Q = empty2(), |
|||
printf("Create queue %w%n%n", Q), |
|||
% add numbers 1 and 2 |
|||
println("Add numbers 1 and 2 : "), |
|||
Q := Q.push2(1).push2(2), |
|||
% display queue |
|||
printf("Q: %w\n\n", Q), |
|||
% pop element |
|||
Q := Q.pop2(V), |
|||
% display results |
|||
printf("Pop : Value: %w Queue: %w\n\n", V, Q), |
|||
% test the queue |
|||
print("Test of the queue: "), |
|||
( Q.empty() -> println("Queue empty"); println("Queue not empty") ), |
|||
nl, |
|||
% pop the elements |
|||
print("Pop the queue : "), |
|||
Q := Q.pop2(V2), |
|||
printf("Value %w Queue : %w%n%n", V2, Q), |
|||
println("Pop empty queue:"), |
|||
catch(_ = Q.pop2(_V),Exception,println(Exception)), |
|||
% command chaining |
|||
println("\nCommand chaining: "), |
|||
Q := Q.push2(3).push2(4), |
|||
Q := Q.pop2(V3).pop2(V4), |
|||
printf("V3: %w V4: %w\n", V3, V4), |
|||
nl, |
|||
println(q=Q).</lang> |
|||
{{out}} |
|||
<pre>Test 2 |
|||
Create queue [] |
|||
Add numbers 1 and 2 : |
|||
Q: [2,1] |
|||
Pop : Value: 1 Queue: [2] |
|||
Test of the queue: Queue not empty |
|||
Pop the queue : Value 2 Queue : [] |
|||
Pop empty queue: |
|||
error(empty_queue,pop,Q = []) |
|||
Command chaining: |
|||
V3: 3 V4: 4 |
|||
q = []</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |