Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Queue-Usage/00-META.yaml
Normal file
3
Task/Queue-Usage/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Queue/Usage
|
||||
note: Data Structures
|
||||
18
Task/Queue-Usage/00-TASK.txt
Normal file
18
Task/Queue-Usage/00-TASK.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{{Data structure}}
|
||||
[[File:Fifo.gif|frame|right|Illustration of FIFO behavior]]
|
||||
|
||||
;Task:
|
||||
Create a queue data structure and demonstrate its operations.
|
||||
|
||||
(For implementations of queues, see the [[FIFO]] task.)
|
||||
|
||||
|
||||
Operations:
|
||||
::* push (aka ''enqueue'') - add element
|
||||
::* pop (aka ''dequeue'') - pop first element
|
||||
::* empty - return truth value when empty
|
||||
|
||||
<br>
|
||||
{{Template:See also lists}}
|
||||
<br><br>
|
||||
|
||||
9
Task/Queue-Usage/11l/queue-usage.11l
Normal file
9
Task/Queue-Usage/11l/queue-usage.11l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Deque[String] my_queue
|
||||
|
||||
my_queue.append(‘foo’)
|
||||
my_queue.append(‘bar’)
|
||||
my_queue.append(‘baz’)
|
||||
|
||||
print(my_queue.pop_left())
|
||||
print(my_queue.pop_left())
|
||||
print(my_queue.pop_left())
|
||||
34
Task/Queue-Usage/6502-Assembly/queue-usage-1.6502
Normal file
34
Task/Queue-Usage/6502-Assembly/queue-usage-1.6502
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
queuePointerStart equ #$FD
|
||||
queuePointerMinus1 equ #$FC ;make this equal whatever "queuePointerStart" is, minus 1.
|
||||
pushQueue:
|
||||
STA 0,x
|
||||
DEX
|
||||
RTS
|
||||
|
||||
popQueue:
|
||||
STX temp
|
||||
LDX #queuePointerStart
|
||||
LDA 0,x ;get the item that's first in line
|
||||
PHA
|
||||
LDX #queuePointerMinus1
|
||||
loop_popQueue:
|
||||
LDA 0,X
|
||||
STA 1,X
|
||||
DEX
|
||||
CPX temp
|
||||
BNE loop_popQueue
|
||||
LDX temp
|
||||
INX
|
||||
PLA ;return the item that just left the queue
|
||||
RTS
|
||||
|
||||
isQueueEmpty:
|
||||
LDA #1
|
||||
CPX #queuePointerStart
|
||||
BEQ yes ;return 1
|
||||
|
||||
SEC
|
||||
SBC #1 ;return 0
|
||||
|
||||
yes:
|
||||
RTS
|
||||
16
Task/Queue-Usage/6502-Assembly/queue-usage-2.6502
Normal file
16
Task/Queue-Usage/6502-Assembly/queue-usage-2.6502
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
define temp $00
|
||||
define queueEmpty $FD
|
||||
define queueAlmostEmpty $FC
|
||||
|
||||
LDX #queueEmpty ;set up software queue
|
||||
|
||||
LDA #$40
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$80
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$C0
|
||||
jsr pushQueue
|
||||
|
||||
brk
|
||||
18
Task/Queue-Usage/6502-Assembly/queue-usage-3.6502
Normal file
18
Task/Queue-Usage/6502-Assembly/queue-usage-3.6502
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
define temp $00
|
||||
define queueEmpty $FD
|
||||
define queueAlmostEmpty $FC
|
||||
|
||||
LDX #queueEmpty ;set up software queue
|
||||
|
||||
LDA #$40
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$80
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$C0
|
||||
jsr pushQueue
|
||||
|
||||
jsr popQueue
|
||||
|
||||
brk
|
||||
22
Task/Queue-Usage/6502-Assembly/queue-usage-4.6502
Normal file
22
Task/Queue-Usage/6502-Assembly/queue-usage-4.6502
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
define temp $00
|
||||
define queueEmpty $FD
|
||||
define queueAlmostEmpty $FC
|
||||
|
||||
|
||||
LDX #queueEmpty ;set up software queue
|
||||
|
||||
LDA #$40
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$80
|
||||
jsr pushQueue
|
||||
|
||||
LDA #$C0
|
||||
jsr pushQueue
|
||||
|
||||
jsr popQueue
|
||||
|
||||
lda #$ff
|
||||
jsr pushQueue
|
||||
|
||||
brk
|
||||
7
Task/Queue-Usage/8th/queue-usage.8th
Normal file
7
Task/Queue-Usage/8th/queue-usage.8th
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
10 q:new \ create a new queue 10 deep
|
||||
123 q:push
|
||||
341 q:push \ push 123, 341 onto the queue
|
||||
q:pop . cr \ displays 123
|
||||
q:len . cr \ displays 1
|
||||
q:pop . cr \ displays 341
|
||||
q:len . cr \ displays 0
|
||||
15
Task/Queue-Usage/ALGOL-68/queue-usage-1.alg
Normal file
15
Task/Queue-Usage/ALGOL-68/queue-usage-1.alg
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# -*- coding: utf-8 -*- #
|
||||
MODE DIETITEM = STRUCT(
|
||||
STRING food, annual quantity, units, REAL cost
|
||||
);
|
||||
|
||||
# Stigler's 1939 Diet ... #
|
||||
FORMAT diet item fmt = $g": "g" "g" = $"zd.dd$;
|
||||
[]DIETITEM stigler diet = (
|
||||
("Cabbage", "111","lb.", 4.11),
|
||||
("Dried Navy Beans", "285","lb.", 16.80),
|
||||
("Evaporated Milk", "57","cans", 3.84),
|
||||
("Spinach", "23","lb.", 1.85),
|
||||
("Wheat Flour", "370","lb.", 13.33),
|
||||
("Total Annual Cost", "","", 39.93)
|
||||
)
|
||||
20
Task/Queue-Usage/ALGOL-68/queue-usage-2.alg
Normal file
20
Task/Queue-Usage/ALGOL-68/queue-usage-2.alg
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/a68g --script #
|
||||
# -*- coding: utf-8 -*- #
|
||||
|
||||
MODE OBJVALUE = DIETITEM;
|
||||
PR read "prelude/link.a68" PR;# c.f. [[rc:Queue/Definition]] #
|
||||
PR read "prelude/queue_base.a68" PR; # c.f. [[rc:Queue/Definition]] #
|
||||
|
||||
PR read "test/data_stigler_diet.a68" PR;
|
||||
OBJQUEUE example queue; obj queue init(example queue);
|
||||
|
||||
FOR i TO UPB stigler diet DO
|
||||
# obj queue put(example queue, stigler diet[i]) or ... #
|
||||
stigler diet[i] +=: example queue
|
||||
OD;
|
||||
|
||||
printf($"Get remaining values from queue:"l$);
|
||||
WHILE NOT obj queue is empty(example queue) DO
|
||||
# OR example queue ISNT obj queue empty #
|
||||
printf((diet item fmt, obj queue get(example queue), $l$))
|
||||
OD
|
||||
55
Task/Queue-Usage/AWK/queue-usage.awk
Normal file
55
Task/Queue-Usage/AWK/queue-usage.awk
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
function deque(arr) {
|
||||
arr["start"] = 0
|
||||
arr["end"] = 0
|
||||
}
|
||||
|
||||
function dequelen(arr) {
|
||||
return arr["end"] - arr["start"]
|
||||
}
|
||||
|
||||
function empty(arr) {
|
||||
return dequelen(arr) == 0
|
||||
}
|
||||
|
||||
function push(arr, elem) {
|
||||
arr[++arr["end"]] = elem
|
||||
}
|
||||
|
||||
function pop(arr) {
|
||||
if (empty(arr)) {
|
||||
return
|
||||
}
|
||||
return arr[arr["end"]--]
|
||||
}
|
||||
|
||||
function unshift(arr, elem) {
|
||||
arr[arr["start"]--] = elem
|
||||
}
|
||||
|
||||
function shift(arr) {
|
||||
if (empty(arr)) {
|
||||
return
|
||||
}
|
||||
return arr[++arr["start"]]
|
||||
}
|
||||
|
||||
function printdeque(arr, i, sep) {
|
||||
printf("[")
|
||||
for (i = arr["start"] + 1; i <= arr["end"]; i++) {
|
||||
printf("%s%s", sep, arr[i])
|
||||
sep = ", "
|
||||
}
|
||||
printf("]\n")
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
deque(q)
|
||||
for (i = 1; i <= 10; i++) {
|
||||
push(q, i)
|
||||
}
|
||||
printdeque(q)
|
||||
for (i = 1; i <= 10; i++) {
|
||||
print shift(q)
|
||||
}
|
||||
printdeque(q)
|
||||
}
|
||||
86
Task/Queue-Usage/Action-/queue-usage.action
Normal file
86
Task/Queue-Usage/Action-/queue-usage.action
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
CARD EndProg ;required for ALLOCATE.ACT
|
||||
|
||||
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
|
||||
|
||||
DEFINE PTR="CARD"
|
||||
DEFINE NODE_SIZE="4"
|
||||
TYPE QueueNode=[PTR data,nxt]
|
||||
|
||||
QueueNode POINTER queueFront,queueRear
|
||||
|
||||
BYTE FUNC IsEmpty()
|
||||
IF queueFront=0 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
PROC Push(CHAR ARRAY v)
|
||||
QueueNode POINTER node
|
||||
|
||||
node=Alloc(NODE_SIZE)
|
||||
node.data=v
|
||||
node.nxt=0
|
||||
IF IsEmpty() THEN
|
||||
queueFront=node
|
||||
ELSE
|
||||
queueRear.nxt=node
|
||||
FI
|
||||
queueRear=node
|
||||
RETURN
|
||||
|
||||
PTR FUNC Pop()
|
||||
QueueNode POINTER node
|
||||
CHAR ARRAY v
|
||||
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Error: queue is empty!")
|
||||
Break()
|
||||
FI
|
||||
|
||||
node=queueFront
|
||||
v=node.data
|
||||
queueFront=node.nxt
|
||||
Free(node,NODE_SIZE)
|
||||
RETURN (v)
|
||||
|
||||
PROC TestIsEmpty()
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Queue is empty")
|
||||
ELSE
|
||||
PrintE("Queue is not empty")
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC TestPush(CHAR ARRAY v)
|
||||
PrintF("Push: %S%E",v)
|
||||
Push(v)
|
||||
RETURN
|
||||
|
||||
PROC TestPop()
|
||||
CHAR ARRAY v
|
||||
|
||||
Print("Pop: ")
|
||||
v=Pop()
|
||||
PrintE(v)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
AllocInit(0)
|
||||
queueFront=0
|
||||
queueRear=0
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
TestIsEmpty()
|
||||
TestPush("foo")
|
||||
TestIsEmpty()
|
||||
TestPush("bar")
|
||||
TestPop()
|
||||
TestIsEmpty()
|
||||
TestPush("baz")
|
||||
TestPop()
|
||||
TestIsEmpty()
|
||||
TestPop()
|
||||
TestIsEmpty()
|
||||
TestPop()
|
||||
RETURN
|
||||
21
Task/Queue-Usage/Ada/queue-usage.ada
Normal file
21
Task/Queue-Usage/Ada/queue-usage.ada
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
with FIFO;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Queue_Test is
|
||||
package Int_FIFO is new FIFO (Integer);
|
||||
use Int_FIFO;
|
||||
Queue : FIFO_Type;
|
||||
Value : Integer;
|
||||
begin
|
||||
Push (Queue, 1);
|
||||
Push (Queue, 2);
|
||||
Push (Queue, 3);
|
||||
Pop (Queue, Value);
|
||||
Pop (Queue, Value);
|
||||
Push (Queue, 4);
|
||||
Pop (Queue, Value);
|
||||
Pop (Queue, Value);
|
||||
Push (Queue, 5);
|
||||
Pop (Queue, Value);
|
||||
Put_Line ("Is_Empty " & Boolean'Image (Is_Empty (Queue)));
|
||||
end Queue_Test;
|
||||
29
Task/Queue-Usage/AppleScript/queue-usage.applescript
Normal file
29
Task/Queue-Usage/AppleScript/queue-usage.applescript
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
on push(StackRef, value)
|
||||
set StackRef's contents to {value} & StackRef's contents
|
||||
return StackRef
|
||||
end push
|
||||
|
||||
on pop(StackRef)
|
||||
set R to missing value
|
||||
if StackRef's contents ≠ {} then
|
||||
set R to StackRef's contents's item 1
|
||||
set StackRef's contents to {} & rest of StackRef's contents
|
||||
end if
|
||||
return R
|
||||
end pop
|
||||
|
||||
on isStackEmpty(StackRef)
|
||||
if StackRef's contents = {} then return true
|
||||
return false
|
||||
end isStackEmpty
|
||||
|
||||
|
||||
set theStack to {}
|
||||
repeat with i from 1 to 5
|
||||
push(a reference to theStack, i)
|
||||
log result
|
||||
end repeat
|
||||
repeat until isStackEmpty(theStack) = true
|
||||
pop(a reference to theStack)
|
||||
log result
|
||||
end repeat
|
||||
35
Task/Queue-Usage/Arturo/queue-usage.arturo
Normal file
35
Task/Queue-Usage/Arturo/queue-usage.arturo
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
define :queue [][
|
||||
init: [
|
||||
this\items: []
|
||||
]
|
||||
]
|
||||
|
||||
empty?: function [this :queue][
|
||||
zero? this\items
|
||||
]
|
||||
|
||||
push: function [this :queue, item][
|
||||
this\items: this\items ++ item
|
||||
]
|
||||
|
||||
pop: function [this :queue][
|
||||
ensure -> not? empty? this
|
||||
|
||||
result: this\items\0
|
||||
this\items: remove.index this\items 0
|
||||
return result
|
||||
]
|
||||
|
||||
Q: to :queue []
|
||||
|
||||
push Q 1
|
||||
push Q 2
|
||||
push Q 3
|
||||
|
||||
print ["queue is empty?" empty? Q]
|
||||
|
||||
print ["popping:" pop Q]
|
||||
print ["popping:" pop Q]
|
||||
print ["popping:" pop Q]
|
||||
|
||||
print ["queue is empty?" empty? Q]
|
||||
9
Task/Queue-Usage/Astro/queue-usage.astro
Normal file
9
Task/Queue-Usage/Astro/queue-usage.astro
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let my_queue = Queue()
|
||||
|
||||
my_queue.push!('foo')
|
||||
my_queue.push!('bar')
|
||||
my_queue.push!('baz')
|
||||
|
||||
print my_queue.pop!() # 'foo'
|
||||
print my_queue.pop!() # 'bar'
|
||||
print my_queue.pop!() # 'baz'
|
||||
31
Task/Queue-Usage/AutoHotkey/queue-usage.ahk
Normal file
31
Task/Queue-Usage/AutoHotkey/queue-usage.ahk
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
push("qu", 2), push("qu", 44), push("qu", "xyz") ; TEST
|
||||
|
||||
MsgBox % "Len = " len("qu") ; Number of entries
|
||||
While !empty("qu") ; Repeat until queue is not empty
|
||||
MsgBox % pop("qu") ; Print popped values (2, 44, xyz)
|
||||
MsgBox Error = %ErrorLevel% ; ErrorLevel = 0: OK
|
||||
MsgBox % pop("qu") ; Empty
|
||||
MsgBox Error = %ErrorLevel% ; ErrorLevel = -1: popped too much
|
||||
MsgBox % "Len = " len("qu") ; Number of entries
|
||||
|
||||
push(queue,_) { ; push _ onto queue named "queue" (!=_), _ string not containing |
|
||||
Global
|
||||
%queue% .= %queue% = "" ? _ : "|" _
|
||||
}
|
||||
|
||||
pop(queue) { ; pop value from queue named "queue" (!=_,_1,_2)
|
||||
Global
|
||||
RegExMatch(%queue%, "([^\|]*)\|?(.*)", _)
|
||||
Return _1, ErrorLevel := -(%queue%=""), %queue% := _2
|
||||
}
|
||||
|
||||
empty(queue) { ; check if queue named "queue" is empty
|
||||
Global
|
||||
Return %queue% = ""
|
||||
}
|
||||
|
||||
len(queue) { ; number of entries in "queue"
|
||||
Global
|
||||
StringReplace %queue%, %queue%, |, |, UseErrorLevel
|
||||
Return %queue% = "" ? 0 : ErrorLevel+1
|
||||
}
|
||||
31
Task/Queue-Usage/BBC-BASIC/queue-usage.basic
Normal file
31
Task/Queue-Usage/BBC-BASIC/queue-usage.basic
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
FIFOSIZE = 1000
|
||||
|
||||
FOR n = 3 TO 5
|
||||
PRINT "Push ";n : PROCenqueue(n)
|
||||
NEXT
|
||||
PRINT "Pop " ; FNdequeue
|
||||
PRINT "Push 6" : PROCenqueue(6)
|
||||
REPEAT
|
||||
PRINT "Pop " ; FNdequeue
|
||||
UNTIL FNisempty
|
||||
PRINT "Pop " ; FNdequeue
|
||||
END
|
||||
|
||||
DEF PROCenqueue(n) : LOCAL f%
|
||||
DEF FNdequeue : LOCAL f% : f% = 1
|
||||
DEF FNisempty : LOCAL f% : f% = 2
|
||||
PRIVATE fifo(), rptr%, wptr%
|
||||
DIM fifo(FIFOSIZE-1)
|
||||
CASE f% OF
|
||||
WHEN 0:
|
||||
wptr% = (wptr% + 1) MOD FIFOSIZE
|
||||
IF rptr% = wptr% ERROR 100, "Error: queue overflowed"
|
||||
fifo(wptr%) = n
|
||||
WHEN 1:
|
||||
IF rptr% = wptr% ERROR 101, "Error: queue empty"
|
||||
rptr% = (rptr% + 1) MOD FIFOSIZE
|
||||
= fifo(rptr%)
|
||||
WHEN 2:
|
||||
= (rptr% = wptr%)
|
||||
ENDCASE
|
||||
ENDPROC
|
||||
38
Task/Queue-Usage/Bracmat/queue-usage.bracmat
Normal file
38
Task/Queue-Usage/Bracmat/queue-usage.bracmat
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
( queue
|
||||
= (list=)
|
||||
(enqueue=.(.!arg) !(its.list):?(its.list))
|
||||
( dequeue
|
||||
= x
|
||||
. !(its.list):?(its.list) (.?x)
|
||||
& !x
|
||||
)
|
||||
(empty=.!(its.list):)
|
||||
)
|
||||
& new$queue:?Q
|
||||
& ( (Q..enqueue)$1
|
||||
& (Q..enqueue)$2
|
||||
& (Q..enqueue)$3
|
||||
& out$((Q..dequeue)$)
|
||||
& (Q..enqueue)$4
|
||||
& out$((Q..dequeue)$)
|
||||
& out$((Q..dequeue)$)
|
||||
& out
|
||||
$ ( The
|
||||
queue
|
||||
is
|
||||
((Q..empty)$&|not)
|
||||
empty
|
||||
)
|
||||
& out$((Q..dequeue)$)
|
||||
& out
|
||||
$ ( The
|
||||
queue
|
||||
is
|
||||
((Q..empty)$&|not)
|
||||
empty
|
||||
)
|
||||
& out$((Q..dequeue)$)
|
||||
& out$Success!
|
||||
| out$"Attempt to dequeue failed"
|
||||
)
|
||||
;
|
||||
46
Task/Queue-Usage/C++/queue-usage-1.cpp
Normal file
46
Task/Queue-Usage/C++/queue-usage-1.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <queue>
|
||||
#include <cassert> // for run time assertions
|
||||
|
||||
int main()
|
||||
{
|
||||
std::queue<int> q;
|
||||
assert( q.empty() ); // initially the queue is empty
|
||||
|
||||
q.push(1); // add an element
|
||||
assert( !q.empty() ); // now the queue isn't empty any more
|
||||
assert( q.front() == 1 ); // the first element is, of course, 1
|
||||
|
||||
q.push(2); // add another element
|
||||
assert( !q.empty() ); // it's of course not empty again
|
||||
assert( q.front() == 1 ); // the first element didn't change
|
||||
|
||||
q.push(3); // add yet an other element
|
||||
assert( !q.empty() ); // the queue is still not empty
|
||||
assert( q.front() == 1 ); // and the first element is still 1
|
||||
|
||||
q.pop(); // remove the first element
|
||||
assert( !q.empty() ); // the queue is not yet empty
|
||||
assert( q.front() == 2); // the first element is now 2 (the 1 is gone)
|
||||
|
||||
q.pop();
|
||||
assert( !q.empty() );
|
||||
assert( q.front() == 3);
|
||||
|
||||
q.push(4);
|
||||
assert( !q.empty() );
|
||||
assert( q.front() == 3);
|
||||
|
||||
q.pop();
|
||||
assert( !q.empty() );
|
||||
assert( q.front() == 4);
|
||||
|
||||
q.pop();
|
||||
assert( q.empty() );
|
||||
|
||||
q.push(5);
|
||||
assert( !q.empty() );
|
||||
assert( q.front() == 5);
|
||||
|
||||
q.pop();
|
||||
assert( q.empty() );
|
||||
}
|
||||
1
Task/Queue-Usage/C++/queue-usage-2.cpp
Normal file
1
Task/Queue-Usage/C++/queue-usage-2.cpp
Normal file
|
|
@ -0,0 +1 @@
|
|||
std::queue<int, std::list<int> >
|
||||
37
Task/Queue-Usage/C-sharp/queue-usage.cs
Normal file
37
Task/Queue-Usage/C-sharp/queue-usage.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RosettaCode
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
// Create a queue and "push" items into it
|
||||
Queue<int> queue = new Queue<int>();
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(5);
|
||||
|
||||
// "Pop" items from the queue in FIFO order
|
||||
Console.WriteLine(queue.Dequeue()); // 1
|
||||
Console.WriteLine(queue.Dequeue()); // 3
|
||||
Console.WriteLine(queue.Dequeue()); // 5
|
||||
|
||||
// To tell if the queue is empty, we check the count
|
||||
bool empty = queue.Count == 0;
|
||||
Console.WriteLine(empty); // "True"
|
||||
|
||||
// If we try to pop from an empty queue, an exception
|
||||
// is thrown.
|
||||
try
|
||||
{
|
||||
queue.Dequeue();
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
{
|
||||
Console.WriteLine(exception.Message); // "Queue empty."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Task/Queue-Usage/C/queue-usage.c
Normal file
31
Task/Queue-Usage/C/queue-usage.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
/* #include "fifolist.h" */
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
FIFOList head;
|
||||
|
||||
TAILQ_INIT(&head);
|
||||
|
||||
/* insert 20 integer values */
|
||||
for(i=0; i < 20; i++) {
|
||||
m_enqueue(i, &head);
|
||||
}
|
||||
|
||||
/* dequeue and print */
|
||||
while( m_dequeue(&i, &head) )
|
||||
printf("%d\n", i);
|
||||
|
||||
fprintf(stderr, "FIFO list %s\n",
|
||||
( m_dequeue(&i, &head) ) ?
|
||||
"had still an element" :
|
||||
"is void!");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
11
Task/Queue-Usage/Clojure/queue-usage-1.clj
Normal file
11
Task/Queue-Usage/Clojure/queue-usage-1.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(def q (make-queue))
|
||||
|
||||
(enqueue q 1)
|
||||
(enqueue q 2)
|
||||
(enqueue q 3)
|
||||
|
||||
(dequeue q) ; 1
|
||||
(dequeue q) ; 2
|
||||
(dequeue q) ; 3
|
||||
|
||||
(queue-empty? q) ; true
|
||||
11
Task/Queue-Usage/Clojure/queue-usage-2.clj
Normal file
11
Task/Queue-Usage/Clojure/queue-usage-2.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(def q (java.util.LinkedList.))
|
||||
|
||||
(.add q 1)
|
||||
(.add q 2)
|
||||
(.add q 3)
|
||||
|
||||
(.remove q) ; 1
|
||||
(.remove q) ; 2
|
||||
(.remove q) ; 3
|
||||
|
||||
(.isEmpty q) ; true
|
||||
32
Task/Queue-Usage/CoffeeScript/queue-usage-1.coffee
Normal file
32
Task/Queue-Usage/CoffeeScript/queue-usage-1.coffee
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# We build a Queue on top of an ordinary JS array, which supports push
|
||||
# and shift. For simple queues, it might make sense to just use arrays
|
||||
# directly, but this code shows how to encapsulate the array behind a restricted
|
||||
# API. For very large queues, you might want a more specialized data
|
||||
# structure to implement the queue, in case arr.shift works in O(N) time, which
|
||||
# is common for array implementations. On my laptop I start noticing delay
|
||||
# after about 100,000 elements, using node.js.
|
||||
Queue = ->
|
||||
arr = []
|
||||
enqueue: (elem) ->
|
||||
arr.push elem
|
||||
dequeue: (elem) ->
|
||||
throw Error("queue is empty") if arr.length == 0
|
||||
arr.shift elem
|
||||
is_empty: (elem) ->
|
||||
arr.length == 0
|
||||
|
||||
# test
|
||||
do ->
|
||||
q = Queue()
|
||||
for i in [1..100000]
|
||||
q.enqueue i
|
||||
|
||||
console.log q.dequeue() # 1
|
||||
while !q.is_empty()
|
||||
v = q.dequeue()
|
||||
console.log v # 1000
|
||||
|
||||
try
|
||||
q.dequeue() # throws Error
|
||||
catch e
|
||||
console.log "#{e}"
|
||||
4
Task/Queue-Usage/CoffeeScript/queue-usage-2.coffee
Normal file
4
Task/Queue-Usage/CoffeeScript/queue-usage-2.coffee
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
> coffee queue.coffee
|
||||
1
|
||||
100000
|
||||
Error: queue is empty
|
||||
7
Task/Queue-Usage/Common-Lisp/queue-usage.lisp
Normal file
7
Task/Queue-Usage/Common-Lisp/queue-usage.lisp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(let ((queue (make-queue)))
|
||||
(enqueue 38 queue)
|
||||
(assert (not (queue-empty-p queue)))
|
||||
(enqueue 23 queue)
|
||||
(assert (eql 38 (dequeue queue)))
|
||||
(assert (eql 23 (dequeue queue)))
|
||||
(assert (queue-empty-p queue)))
|
||||
23
Task/Queue-Usage/Component-Pascal/queue-usage.pas
Normal file
23
Task/Queue-Usage/Component-Pascal/queue-usage.pas
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
MODULE UseQueue;
|
||||
IMPORT
|
||||
Queue,
|
||||
Boxes,
|
||||
StdLog;
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
q: Queue.Instance;
|
||||
b: Boxes.Box;
|
||||
BEGIN
|
||||
q := Queue.New(10);
|
||||
q.Push(Boxes.NewInteger(1));
|
||||
q.Push(Boxes.NewInteger(2));
|
||||
q.Push(Boxes.NewInteger(3));
|
||||
b := q.Pop();
|
||||
b := q.Pop();
|
||||
q.Push(Boxes.NewInteger(4));
|
||||
b := q.Pop();
|
||||
b := q.Pop();
|
||||
StdLog.String("Is empty:> ");StdLog.Bool(q.IsEmpty());StdLog.Ln
|
||||
END Do;
|
||||
END UseQueue.
|
||||
28
Task/Queue-Usage/Cowgol/queue-usage.cowgol
Normal file
28
Task/Queue-Usage/Cowgol/queue-usage.cowgol
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
typedef QueueData is uint8; # the queue will contain bytes
|
||||
include "queue.coh"; # from the Queue/Definition task
|
||||
|
||||
var queue := MakeQueue();
|
||||
|
||||
# enqueue bytes 0 to 20
|
||||
print("Enqueueing: ");
|
||||
var n: uint8 := 0;
|
||||
while n < 20 loop
|
||||
print_i8(n);
|
||||
print_char(' ');
|
||||
Enqueue(queue, n);
|
||||
n := n + 1;
|
||||
end loop;
|
||||
print_nl();
|
||||
|
||||
# dequeue and print everything in the queue
|
||||
print("Dequeueing: ");
|
||||
while QueueEmpty(queue) == 0 loop
|
||||
print_i8(Dequeue(queue));
|
||||
print_char(' ');
|
||||
end loop;
|
||||
print_nl();
|
||||
|
||||
# free the queue
|
||||
FreeQueue(queue);
|
||||
44
Task/Queue-Usage/D/queue-usage-1.d
Normal file
44
Task/Queue-Usage/D/queue-usage-1.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
class LinkedQueue(T) {
|
||||
private static struct Node {
|
||||
T data;
|
||||
Node* next;
|
||||
}
|
||||
|
||||
private Node* head, tail;
|
||||
|
||||
bool empty() { return head is null; }
|
||||
|
||||
void push(T item) {
|
||||
if (empty())
|
||||
head = tail = new Node(item);
|
||||
else {
|
||||
tail.next = new Node(item);
|
||||
tail = tail.next;
|
||||
}
|
||||
}
|
||||
|
||||
T pop() {
|
||||
if (empty())
|
||||
throw new Exception("Empty LinkedQueue.");
|
||||
auto item = head.data;
|
||||
head = head.next;
|
||||
if (head is tail) // Is last one?
|
||||
// Release tail reference so that GC can collect.
|
||||
tail = null;
|
||||
return item;
|
||||
}
|
||||
|
||||
alias push enqueue;
|
||||
alias pop dequeue;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto q = new LinkedQueue!int();
|
||||
q.push(10);
|
||||
q.push(20);
|
||||
q.push(30);
|
||||
assert(q.pop() == 10);
|
||||
assert(q.pop() == 20);
|
||||
assert(q.pop() == 30);
|
||||
assert(q.empty());
|
||||
}
|
||||
75
Task/Queue-Usage/D/queue-usage-2.d
Normal file
75
Task/Queue-Usage/D/queue-usage-2.d
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
module queue_usage2;
|
||||
|
||||
import std.traits: hasIndirections;
|
||||
|
||||
struct GrowableCircularQueue(T) {
|
||||
public size_t length;
|
||||
private size_t first, last;
|
||||
private T[] A = [T.init];
|
||||
|
||||
this(T[] items...) pure nothrow @safe {
|
||||
foreach (x; items)
|
||||
push(x);
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow @safe @nogc {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
@property T front() pure nothrow @safe @nogc {
|
||||
assert(length != 0);
|
||||
return A[first];
|
||||
}
|
||||
|
||||
T opIndex(in size_t i) pure nothrow @safe @nogc {
|
||||
assert(i < length);
|
||||
return A[(first + i) & (A.length - 1)];
|
||||
}
|
||||
|
||||
void push(T item) pure nothrow @safe {
|
||||
if (length >= A.length) { // Double the queue.
|
||||
immutable oldALen = A.length;
|
||||
A.length *= 2;
|
||||
if (last < first) {
|
||||
A[oldALen .. oldALen + last + 1] = A[0 .. last + 1];
|
||||
static if (hasIndirections!T)
|
||||
A[0 .. last + 1] = T.init; // Help for the GC.
|
||||
last += oldALen;
|
||||
}
|
||||
}
|
||||
last = (last + 1) & (A.length - 1);
|
||||
A[last] = item;
|
||||
length++;
|
||||
}
|
||||
|
||||
@property T pop() pure nothrow @safe @nogc {
|
||||
assert(length != 0);
|
||||
auto saved = A[first];
|
||||
static if (hasIndirections!T)
|
||||
A[first] = T.init; // Help for the GC.
|
||||
first = (first + 1) & (A.length - 1);
|
||||
length--;
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
|
||||
version (queue_usage2_main) {
|
||||
void main() {
|
||||
GrowableCircularQueue!int q;
|
||||
q.push(10);
|
||||
q.push(20);
|
||||
q.push(30);
|
||||
assert(q.pop == 10);
|
||||
assert(q.pop == 20);
|
||||
assert(q.pop == 30);
|
||||
assert(q.empty);
|
||||
|
||||
uint count = 0;
|
||||
foreach (immutable i; 1 .. 1_000) {
|
||||
foreach (immutable j; 0 .. i)
|
||||
q.push(count++);
|
||||
foreach (immutable j; 0 .. i)
|
||||
q.pop;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Task/Queue-Usage/Delphi/queue-usage.delphi
Normal file
25
Task/Queue-Usage/Delphi/queue-usage.delphi
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
program QueueUsage;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses Generics.Collections;
|
||||
|
||||
var
|
||||
lStringQueue: TQueue<string>;
|
||||
begin
|
||||
lStringQueue := TQueue<string>.Create;
|
||||
try
|
||||
lStringQueue.Enqueue('First');
|
||||
lStringQueue.Enqueue('Second');
|
||||
lStringQueue.Enqueue('Third');
|
||||
|
||||
Writeln(lStringQueue.Dequeue);
|
||||
Writeln(lStringQueue.Dequeue);
|
||||
Writeln(lStringQueue.Dequeue);
|
||||
|
||||
if lStringQueue.Count = 0 then
|
||||
Writeln('Queue is empty.');
|
||||
finally
|
||||
lStringQueue.Free;
|
||||
end
|
||||
end.
|
||||
31
Task/Queue-Usage/Diego/queue-usage.diego
Normal file
31
Task/Queue-Usage/Diego/queue-usage.diego
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
set_ns(rosettacode)_me();
|
||||
|
||||
add_queue({int},q)_values(1..4); // 1,2,3,4 (1 is first/bottom, 4 is last/top)
|
||||
with_queue(q)_pop(); // 2,3,4
|
||||
with_queue(q)_dequeue(); // 3,4
|
||||
with_queue(q)_enqueue(5); // 3,4,5
|
||||
|
||||
with_queue(q)_push()_v(6,7); // 3,4,5,6,7
|
||||
|
||||
add_var({int},b)_value(8);
|
||||
with_queue(q)_push[b]; // 3,4,5,6,7,8
|
||||
|
||||
with_queue(q)_pluck()_at(2); // callee will return `with_queue(q)_err(pluck invalid with queue);`
|
||||
|
||||
me_msg()_queue(q)_top(); // "8"
|
||||
me_msg()_queue(q)_last(); // "8"
|
||||
me_msg()_queue(q)_peek(); // "8"
|
||||
|
||||
me_msg()_queue(q)_bottom(); // "3"
|
||||
me_msg()_queue(q)_first(); // "3"
|
||||
me_msg()_queue(q)_peer(); // "3"
|
||||
|
||||
me_msg()_queue(q)_isempty(); // "false"
|
||||
with_queue(q)_empty();
|
||||
with_queue(q)_msg()_isempty()_me(); // "true" (alternative syntax)
|
||||
|
||||
with_queue()_pop(); // callee will return `with_queue(q)_err(pop invalid with empty queue);`
|
||||
|
||||
me_msg()_queue(q)_history()_all(); // returns the entire history of queue 'q' since its creation
|
||||
|
||||
reset_namespace[];
|
||||
9
Task/Queue-Usage/E/queue-usage.e
Normal file
9
Task/Queue-Usage/E/queue-usage.e
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def [reader, writer] := makeQueue()
|
||||
require(escape empty { reader.dequeue(empty); false } catch _ { true })
|
||||
writer.enqueue(1)
|
||||
writer.enqueue(2)
|
||||
require(reader.dequeue(throw) == 1)
|
||||
writer.enqueue(3)
|
||||
require(reader.dequeue(throw) == 2)
|
||||
require(reader.dequeue(throw) == 3)
|
||||
require(escape empty { reader.dequeue(empty); false } catch _ { true })
|
||||
24
Task/Queue-Usage/Elena/queue-usage.elena
Normal file
24
Task/Queue-Usage/Elena/queue-usage.elena
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import system'collections;
|
||||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
// Create a queue and "push" items into it
|
||||
var queue := new Queue();
|
||||
|
||||
queue.push:1;
|
||||
queue.push:3;
|
||||
queue.push:5;
|
||||
|
||||
// "Pop" items from the queue in FIFO order
|
||||
console.printLine(queue.pop()); // 1
|
||||
console.printLine(queue.pop()); // 3
|
||||
console.printLine(queue.pop()); // 5
|
||||
|
||||
// To tell if the queue is empty, we check the count
|
||||
console.printLine("queue is ",(queue.Length == 0).iif("empty","nonempty"));
|
||||
|
||||
// If we try to pop from an empty queue, an exception
|
||||
// is thrown.
|
||||
queue.pop() | on:(e){ console.writeLine:"Queue empty." }
|
||||
}
|
||||
10
Task/Queue-Usage/Elixir/queue-usage-1.elixir
Normal file
10
Task/Queue-Usage/Elixir/queue-usage-1.elixir
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
defmodule Queue do
|
||||
def empty?([]), do: true
|
||||
def empty?(_), do: false
|
||||
|
||||
def pop([h|t]), do: {h,t}
|
||||
|
||||
def push(q,t), do: q ++ [t]
|
||||
|
||||
def front([h|_]), do: h
|
||||
end
|
||||
14
Task/Queue-Usage/Elixir/queue-usage-2.elixir
Normal file
14
Task/Queue-Usage/Elixir/queue-usage-2.elixir
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
iex(2)> q = [1,2,3,4,5]
|
||||
[1, 2, 3, 4, 5]
|
||||
iex(3)> Queue.push(q,10)
|
||||
[1, 2, 3, 4, 5, 10]
|
||||
iex(4)> front=Queue.front(q)
|
||||
1
|
||||
iex(5)> Queue.empty?(q)
|
||||
false
|
||||
iex(6)> Queue.pop(q)
|
||||
{1, [2, 3, 4, 5]}
|
||||
iex(7)> l=[]
|
||||
[]
|
||||
iex(8)> Queue.empty?(l)
|
||||
true
|
||||
17
Task/Queue-Usage/Erlang/queue-usage.erl
Normal file
17
Task/Queue-Usage/Erlang/queue-usage.erl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
1> Q = fifo:new().
|
||||
{fifo,[],[]}
|
||||
2> fifo:empty(Q).
|
||||
true
|
||||
3> Q2 = fifo:push(Q,1).
|
||||
{fifo,[1],[]}
|
||||
4> Q3 = fifo:push(Q2,2).
|
||||
{fifo,[2,1],[]}
|
||||
5> fifo:empty(Q3).
|
||||
false
|
||||
6> fifo:pop(Q3).
|
||||
{1,{fifo,[],[2]}}
|
||||
7> {Popped, Q} = fifo:pop(Q2).
|
||||
{1,{fifo,[],[]}}
|
||||
8> fifo:pop(fifo:new()).
|
||||
** exception error: 'empty fifo'
|
||||
in function fifo:pop/1
|
||||
13
Task/Queue-Usage/Factor/queue-usage-1.factor
Normal file
13
Task/Queue-Usage/Factor/queue-usage-1.factor
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
USING: combinators deques dlists kernel prettyprint ;
|
||||
IN: rosetta-code.queue-usage
|
||||
|
||||
DL{ } clone { ! make new queue
|
||||
[ [ 1 ] dip push-front ] ! push 1
|
||||
[ [ 2 ] dip push-front ] ! push 2
|
||||
[ [ 3 ] dip push-front ] ! push 3
|
||||
[ . ] ! DL{ 3 2 1 }
|
||||
[ pop-back drop ] ! pop 1 (and discard)
|
||||
[ pop-back drop ] ! pop 2 (and discard)
|
||||
[ pop-back drop ] ! pop 3 (and discard)
|
||||
[ deque-empty? . ] ! t
|
||||
} cleave
|
||||
6
Task/Queue-Usage/Factor/queue-usage-2.factor
Normal file
6
Task/Queue-Usage/Factor/queue-usage-2.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
DL{ } clone {
|
||||
[ [ { 1 2 3 } ] dip push-all-front ] ! push all from sequence
|
||||
[ . ] ! DL{ 3 2 1 }
|
||||
[ [ drop ] slurp-deque ] ! pop and discard all
|
||||
[ deque-empty? . ] ! t
|
||||
} cleave
|
||||
14
Task/Queue-Usage/Fantom/queue-usage.fantom
Normal file
14
Task/Queue-Usage/Fantom/queue-usage.fantom
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
q := Queue()
|
||||
q.push (1)
|
||||
q.push ("a")
|
||||
echo ("Is empty? " + q.isEmpty)
|
||||
echo ("Element: " + q.pop)
|
||||
echo ("Element: " + q.pop)
|
||||
echo ("Is empty? " + q.isEmpty)
|
||||
try { q.pop } catch (Err e) { echo (e.msg) }
|
||||
}
|
||||
}
|
||||
45
Task/Queue-Usage/Forth/queue-usage-1.fth
Normal file
45
Task/Queue-Usage/Forth/queue-usage-1.fth
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
: cqueue: ( n -- <text>)
|
||||
create \ compile time: build the data structure in memory
|
||||
dup
|
||||
dup 1- and abort" queue size must be power of 2"
|
||||
0 , \ write pointer "HEAD"
|
||||
0 , \ read pointer "TAIL"
|
||||
0 , \ byte counter
|
||||
dup 1- , \ mask value used for wrap around
|
||||
allot ; \ run time: returns the address of this data structure
|
||||
|
||||
\ calculate offsets into the queue data structure
|
||||
: ->head ( q -- adr ) ; \ syntactic sugar
|
||||
: ->tail ( q -- adr ) cell+ ;
|
||||
: ->cnt ( q -- adr ) 2 cells + ;
|
||||
: ->msk ( q -- adr ) 3 cells + ;
|
||||
: ->data ( q -- adr ) 4 cells + ;
|
||||
|
||||
: head++ ( q -- ) \ circular increment head pointer of a queue
|
||||
dup >r ->head @ 1+ r@ ->msk @ and r> ->head ! ;
|
||||
|
||||
: tail++ ( q -- ) \ circular increment tail pointer of a queue
|
||||
dup >r ->tail @ 1+ r@ ->msk @ and r> ->tail ! ;
|
||||
|
||||
: qempty ( q -- flag)
|
||||
dup ->head off dup ->tail off dup ->cnt off \ reset all fields to "off" (zero)
|
||||
->cnt @ 0= ; \ per the spec qempty returns a flag
|
||||
|
||||
: cnt=msk? ( q -- flag) dup >r ->cnt @ r> ->msk @ = ;
|
||||
: ?empty ( q -- ) ->cnt @ 0= abort" queue is empty" ;
|
||||
: ?full ( q -- ) cnt=msk? abort" queue is full" ;
|
||||
: 1+! ( adr -- ) 1 swap +! ; \ increment contents of adr
|
||||
: 1-! ( adr -- ) -1 swap +! ; \ decrement contents of adr
|
||||
|
||||
: qc@ ( queue -- char ) \ fetch next char in queue
|
||||
dup >r ?empty \ abort if empty
|
||||
r@ ->cnt 1-! \ decr. the counter
|
||||
r@ tail++
|
||||
r@ ->data r> ->tail @ + c@ ; \ calc. address and fetch the byte
|
||||
|
||||
|
||||
: qc! ( char queue -- )
|
||||
dup >r ?full \ abort if q full
|
||||
r@ ->cnt 1+! \ incr. the counter
|
||||
r@ head++
|
||||
r@ ->data r> ->head @ + c! ; \ data+head = adr, and store the char
|
||||
16
Task/Queue-Usage/Forth/queue-usage-2.fth
Normal file
16
Task/Queue-Usage/Forth/queue-usage-2.fth
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
make-queue constant q1
|
||||
make-queue constant q2
|
||||
q1 empty? .
|
||||
5 q1 enqueue
|
||||
q1 empty? .
|
||||
7 q1 enqueue
|
||||
9 q1 enqueue
|
||||
q2 empty? .
|
||||
3 q2 enqueue
|
||||
q2 empty? .
|
||||
q1 dequeue .
|
||||
q1 dequeue .
|
||||
q1 dequeue .
|
||||
q1 empty? .
|
||||
q2 dequeue .
|
||||
q2 empty? .
|
||||
33
Task/Queue-Usage/Fortran/queue-usage.f
Normal file
33
Task/Queue-Usage/Fortran/queue-usage.f
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
module fifo_nodes
|
||||
type fifo_node
|
||||
integer :: datum
|
||||
! the next part is not variable and must be present
|
||||
type(fifo_node), pointer :: next
|
||||
logical :: valid
|
||||
end type fifo_node
|
||||
end module fifo_nodes
|
||||
|
||||
program FIFOTest
|
||||
use fifo
|
||||
implicit none
|
||||
|
||||
type(fifo_head) :: thehead
|
||||
type(fifo_node), dimension(5) :: ex, xe
|
||||
integer :: i
|
||||
|
||||
call new_fifo(thehead)
|
||||
|
||||
do i = 1, 5
|
||||
ex(i)%datum = i
|
||||
call fifo_enqueue(thehead, ex(i))
|
||||
end do
|
||||
|
||||
i = 1
|
||||
do
|
||||
call fifo_dequeue(thehead, xe(i))
|
||||
print *, xe(i)%datum
|
||||
i = i + 1
|
||||
if ( fifo_isempty(thehead) ) exit
|
||||
end do
|
||||
|
||||
end program FIFOTest
|
||||
28
Task/Queue-Usage/FreeBASIC/queue-usage.basic
Normal file
28
Task/Queue-Usage/FreeBASIC/queue-usage.basic
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Include "queue_rosetta.bi" '' include macro-based generic Queue type used in earlier task
|
||||
|
||||
Declare_Queue(String) '' expand Queue type for Strings
|
||||
|
||||
Dim stringQueue As Queue(String)
|
||||
With stringQueue '' push some strings into the Queue
|
||||
.push("first")
|
||||
.push("second")
|
||||
.push("third")
|
||||
.push("fourth")
|
||||
.push("fifth")
|
||||
End With
|
||||
Print "Number of Strings in the Queue :" ; stringQueue.count
|
||||
Print "Capacity of string Queue :" ; stringQueue.capacity
|
||||
Print
|
||||
' now pop them
|
||||
While Not stringQueue.empty
|
||||
Print stringQueue.pop(); " popped"
|
||||
Wend
|
||||
Print
|
||||
Print "Number of Strings in the Queue :" ; stringQueue.count
|
||||
Print "Capacity of string Queue :" ; stringQueue.capacity '' capacity should be unchanged
|
||||
Print "Is Queue empty now : "; stringQueue.empty
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
39
Task/Queue-Usage/Go/queue-usage-1.go
Normal file
39
Task/Queue-Usage/Go/queue-usage-1.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"queue"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := new(queue.Queue)
|
||||
fmt.Println("empty?", q.Empty())
|
||||
|
||||
x := "black"
|
||||
fmt.Println("push", x)
|
||||
q.Push(x)
|
||||
|
||||
fmt.Println("empty?", q.Empty())
|
||||
r, ok := q.Pop()
|
||||
if ok {
|
||||
fmt.Println(r, "popped")
|
||||
} else {
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
|
||||
var n int
|
||||
for _, x := range []string{"blue", "red", "green"} {
|
||||
fmt.Println("pushing", x)
|
||||
q.Push(x)
|
||||
n++
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
r, ok := q.Pop()
|
||||
if ok {
|
||||
fmt.Println(r, "popped")
|
||||
} else {
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Task/Queue-Usage/Go/queue-usage-2.go
Normal file
36
Task/Queue-Usage/Go/queue-usage-2.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
q := make(chan string, 3)
|
||||
fmt.Println("empty?", len(q) == 0)
|
||||
|
||||
x := "black"
|
||||
fmt.Println("push", x)
|
||||
q <- x
|
||||
|
||||
fmt.Println("empty?", len(q) == 0)
|
||||
select {
|
||||
case r := <-q:
|
||||
fmt.Println(r, "popped")
|
||||
default:
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
|
||||
var n int
|
||||
for _, x := range []string{"blue", "red", "green"} {
|
||||
fmt.Println("pushing", x)
|
||||
q <- x
|
||||
n++
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
select {
|
||||
case r := <-q:
|
||||
fmt.Println(r, "popped")
|
||||
default:
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
39
Task/Queue-Usage/Go/queue-usage-3.go
Normal file
39
Task/Queue-Usage/Go/queue-usage-3.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"container/list"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := list.New()
|
||||
fmt.Println("empty?", q.Len() == 0)
|
||||
|
||||
x := "black"
|
||||
fmt.Println("push", x)
|
||||
q.PushBack(x)
|
||||
|
||||
fmt.Println("empty?", q.Len() == 0)
|
||||
if e := q.Front(); e != nil {
|
||||
r := q.Remove(e)
|
||||
fmt.Println(r, "popped")
|
||||
} else {
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
|
||||
var n int
|
||||
for _, x := range []string{"blue", "red", "green"} {
|
||||
fmt.Println("pushing", x)
|
||||
q.PushBack(x)
|
||||
n++
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
if e := q.Front(); e != nil {
|
||||
r := q.Remove(e)
|
||||
fmt.Println(r, "popped")
|
||||
} else {
|
||||
fmt.Println("pop failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/Queue-Usage/Groovy/queue-usage-1.groovy
Normal file
1
Task/Queue-Usage/Groovy/queue-usage-1.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
def q = new LinkedList()
|
||||
46
Task/Queue-Usage/Groovy/queue-usage-2.groovy
Normal file
46
Task/Queue-Usage/Groovy/queue-usage-2.groovy
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
assert q.empty
|
||||
println q
|
||||
// "push" adds to end of "queue" list
|
||||
q.push('Stuart')
|
||||
println q
|
||||
assert !q.empty
|
||||
// "add" adds to end of "queue" list
|
||||
q.add('Pete')
|
||||
println q
|
||||
assert !q.empty
|
||||
// left shift operator ("<<") adds to end of "queue" list
|
||||
q << 'John'
|
||||
println q
|
||||
assert !q.empty
|
||||
// add assignment ("+=") adds the list elements
|
||||
// to the end of the "queue" list in list order
|
||||
q += ['Paul', 'George']
|
||||
println q
|
||||
assert !q.empty
|
||||
// "poll" removes and returns the first element in the
|
||||
// "queue" list ("pop" exists for Groovy lists, but it
|
||||
// removes and returns the LAST element for "Stack"
|
||||
// semantics). "poll" only exists in objects that
|
||||
// implement java.util.Queue, like java.util.LinkedList
|
||||
assert q.poll() == 'Stuart'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.poll() == 'Pete'
|
||||
println q
|
||||
assert !q.empty
|
||||
q << 'Ringo'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.poll() == 'John'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.poll() == 'Paul'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.poll() == 'George'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.poll() == 'Ringo'
|
||||
println q
|
||||
assert q.empty
|
||||
assert q.poll() == null
|
||||
25
Task/Queue-Usage/Haskell/queue-usage.hs
Normal file
25
Task/Queue-Usage/Haskell/queue-usage.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Prelude> :l fifo.hs
|
||||
[1 of 1] Compiling Main ( fifo.hs, interpreted )
|
||||
Ok, modules loaded: Main.
|
||||
*Main> let q = emptyFifo
|
||||
*Main> isEmpty q
|
||||
True
|
||||
*Main> let q' = push q 1
|
||||
*Main> isEmpty q'
|
||||
False
|
||||
*Main> let q'' = foldl push q' [2..4]
|
||||
*Main> let (v,q''') = pop q''
|
||||
*Main> v
|
||||
Just 1
|
||||
*Main> let (v',q'''') = pop q'''
|
||||
*Main> v'
|
||||
Just 2
|
||||
*Main> let (v'',q''''') = pop q''''
|
||||
*Main> v''
|
||||
Just 3
|
||||
*Main> let (v''',q'''''') = pop q'''''
|
||||
*Main> v'''
|
||||
Just 4
|
||||
*Main> let (v'''',q''''''') = pop q''''''
|
||||
*Main> v''''
|
||||
Nothing
|
||||
18
Task/Queue-Usage/Icon/queue-usage.icon
Normal file
18
Task/Queue-Usage/Icon/queue-usage.icon
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
procedure main(arglist)
|
||||
queue := []
|
||||
write("Usage:\nqueue x x x - x - - - - -\n\t- pops elements\n\teverything else pushes")
|
||||
write("Queue is:")
|
||||
every x := !arglist do {
|
||||
case x of {
|
||||
"-" : pop(queue) | write("pop(empty) failed.") # pop if the next arglist[i] is a -
|
||||
default : put(queue,x) # push arglist[i]
|
||||
}
|
||||
if empty(queue) then writes("empty")
|
||||
else every writes(!queue," ")
|
||||
write()
|
||||
}
|
||||
end
|
||||
|
||||
procedure empty(X) #: fail if X is not empty
|
||||
if *X = 0 then return
|
||||
end
|
||||
19
Task/Queue-Usage/J/queue-usage-1.j
Normal file
19
Task/Queue-Usage/J/queue-usage-1.j
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
queue=: conew 'fifo'
|
||||
isEmpty__queue ''
|
||||
1
|
||||
push__queue 9
|
||||
9
|
||||
push__queue 8
|
||||
8
|
||||
push__queue 7
|
||||
7
|
||||
isEmpty__queue ''
|
||||
0
|
||||
pop__queue ''
|
||||
9
|
||||
pop__queue ''
|
||||
8
|
||||
pop__queue ''
|
||||
7
|
||||
isEmpty__queue ''
|
||||
1
|
||||
17
Task/Queue-Usage/J/queue-usage-2.j
Normal file
17
Task/Queue-Usage/J/queue-usage-2.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
is_empty make_empty _
|
||||
1
|
||||
first_named_state =: push 9 onto make_empty _
|
||||
newer_state =: push 8 onto first_named_state
|
||||
this_state =: push 7 onto newer_state
|
||||
is_empty this_state
|
||||
0
|
||||
tell_queue this_state
|
||||
9 8 7
|
||||
tell_atom pop this_state
|
||||
9
|
||||
tell_atom pop pop this_state
|
||||
8
|
||||
tell_atom pop pop pop this_state
|
||||
7
|
||||
is_empty pop pop pop this_state
|
||||
1
|
||||
13
Task/Queue-Usage/Java/queue-usage-1.java
Normal file
13
Task/Queue-Usage/Java/queue-usage-1.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
...
|
||||
Queue<Integer> queue = new LinkedList<Integer>();
|
||||
System.out.println(queue.isEmpty()); // empty test - true
|
||||
// queue.remove(); // would throw NoSuchElementException
|
||||
queue.add(1);
|
||||
queue.add(2);
|
||||
queue.add(3);
|
||||
System.out.println(queue); // [1, 2, 3]
|
||||
System.out.println(queue.remove()); // 1
|
||||
System.out.println(queue); // [2, 3]
|
||||
System.out.println(queue.isEmpty()); // false
|
||||
11
Task/Queue-Usage/Java/queue-usage-2.java
Normal file
11
Task/Queue-Usage/Java/queue-usage-2.java
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import java.util.LinkedList;
|
||||
...
|
||||
LinkedList queue = new LinkedList();
|
||||
System.out.println(queue.isEmpty()); // empty test - true
|
||||
queue.add(new Integer(1));
|
||||
queue.add(new Integer(2));
|
||||
queue.add(new Integer(3));
|
||||
System.out.println(queue); // [1, 2, 3]
|
||||
System.out.println(queue.removeFirst()); // 1
|
||||
System.out.println(queue); // [2, 3]
|
||||
System.out.println(queue.isEmpty()); // false
|
||||
10
Task/Queue-Usage/JavaScript/queue-usage.js
Normal file
10
Task/Queue-Usage/JavaScript/queue-usage.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
var f = new Array();
|
||||
print(f.length);
|
||||
f.push(1,2); // can take multiple arguments
|
||||
f.push(3);
|
||||
f.shift();
|
||||
f.shift();
|
||||
print(f.length);
|
||||
print(f.shift())
|
||||
print(f.length == 0);
|
||||
print(f.shift());
|
||||
7
Task/Queue-Usage/Julia/queue-usage.julia
Normal file
7
Task/Queue-Usage/Julia/queue-usage.julia
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
using DataStructures
|
||||
|
||||
queue = Queue(String)
|
||||
@show enqueue!(queue, "foo")
|
||||
@show enqueue!(queue, "bar")
|
||||
@show dequeue!(queue) # -> foo
|
||||
@show dequeue!(queue) # -> bar
|
||||
22
Task/Queue-Usage/Kotlin/queue-usage.kotlin
Normal file
22
Task/Queue-Usage/Kotlin/queue-usage.kotlin
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val q: Queue<Int> = ArrayDeque<Int>()
|
||||
(1..5).forEach { q.add(it) }
|
||||
println(q)
|
||||
println("Size of queue = ${q.size}")
|
||||
print("Removing: ")
|
||||
(1..3).forEach { print("${q.remove()} ") }
|
||||
println("\nRemaining in queue: $q")
|
||||
println("Head element is now ${q.element()}")
|
||||
q.clear()
|
||||
println("After clearing, queue is ${if(q.isEmpty()) "empty" else "not empty"}")
|
||||
try {
|
||||
q.remove()
|
||||
}
|
||||
catch (e: NoSuchElementException) {
|
||||
println("Can't remove elements from an empty queue")
|
||||
}
|
||||
}
|
||||
30
Task/Queue-Usage/Lambdatalk/queue-usage.lambdatalk
Normal file
30
Task/Queue-Usage/Lambdatalk/queue-usage.lambdatalk
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{def queue.add
|
||||
{lambda {:v :q}
|
||||
{let { {_ {A.addlast! :v :q}}}
|
||||
} ok}}
|
||||
-> queue.add
|
||||
|
||||
{def queue.get
|
||||
{lambda {:q}
|
||||
{let { {:v {A.first :q}}
|
||||
{_ {A.subfirst! :q}}
|
||||
} :v}}}
|
||||
-> queue.get
|
||||
|
||||
{def queue.empty?
|
||||
{lambda {:q}
|
||||
{A.empty? :q}}}
|
||||
-> queue.empty?
|
||||
|
||||
{def Q {A.new}} -> Q []
|
||||
{queue.add 1 {Q}} -> ok [1]
|
||||
{queue.add 2 {Q}} -> ok [1,2]
|
||||
{queue.add 3 {Q}} -> ok [1,2,3]
|
||||
{queue.get {Q}} -> 1 [2,3]
|
||||
{queue.add 4 {Q}} -> ok [2,3,4]
|
||||
{queue.empty? {Q}} -> false
|
||||
{queue.get {Q}} -> 2 [3,4]
|
||||
{queue.get {Q}} -> 3 [4]
|
||||
{queue.get {Q}} -> 4 []
|
||||
{queue.get {Q}} -> undefined
|
||||
{queue.empty? {Q}} -> true
|
||||
20
Task/Queue-Usage/Lasso/queue-usage.lasso
Normal file
20
Task/Queue-Usage/Lasso/queue-usage.lasso
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
local(queue) = queue
|
||||
#queue->size
|
||||
// => 0
|
||||
|
||||
#queue->insert('a')
|
||||
#queue->insert('b')
|
||||
#queue->insert('c')
|
||||
#queue->size
|
||||
// => 3
|
||||
|
||||
loop(#queue->size) => {
|
||||
stdoutnl(#queue->get)
|
||||
}
|
||||
// =>
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
|
||||
#queue->size == 0
|
||||
// => true
|
||||
9
Task/Queue-Usage/Logo/queue-usage.logo
Normal file
9
Task/Queue-Usage/Logo/queue-usage.logo
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
make "fifo []
|
||||
print empty? :fifo ; true
|
||||
queue "fifo 1
|
||||
queue "fifo 2
|
||||
queue "fifo 3
|
||||
show :fifo ; [1 2 3]
|
||||
print dequeue "fifo ; 1
|
||||
show :fifo ; [2 3]
|
||||
print empty? :fifo ; false
|
||||
7
Task/Queue-Usage/Lua/queue-usage-1.lua
Normal file
7
Task/Queue-Usage/Lua/queue-usage-1.lua
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
q = Queue.new()
|
||||
Queue.push( q, 5 )
|
||||
Queue.push( q, "abc" )
|
||||
|
||||
while not Queue.empty( q ) do
|
||||
print( Queue.pop( q ) )
|
||||
end
|
||||
16
Task/Queue-Usage/Lua/queue-usage-2.lua
Normal file
16
Task/Queue-Usage/Lua/queue-usage-2.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
> -- create queue:
|
||||
> q = {}
|
||||
> -- push:
|
||||
> q[#q+1] = "first"
|
||||
> q[#q+1] = "second"
|
||||
> q[#q+1] = "third"
|
||||
> -- pop:
|
||||
> =table.remove(q, 1)
|
||||
first
|
||||
> =table.remove(q, 1)
|
||||
second
|
||||
> =table.remove(q, 1)
|
||||
third
|
||||
> -- empty?
|
||||
> =#q == 0
|
||||
true
|
||||
38
Task/Queue-Usage/M2000-Interpreter/queue-usage.m2000
Normal file
38
Task/Queue-Usage/M2000-Interpreter/queue-usage.m2000
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
Module CheckStackAsLIFO {
|
||||
a=stack
|
||||
Stack a {
|
||||
Push 1, 2, 3
|
||||
Print number=3
|
||||
Print number=2
|
||||
Print number=1
|
||||
Print Empty=True
|
||||
Push "A", "B", "C"
|
||||
Print letter$="C"
|
||||
Print letter$="B"
|
||||
Print letter$="A"
|
||||
Print Empty=True
|
||||
Push 1,"OK"
|
||||
}
|
||||
Print Len(a)=2, StackItem(a, 2)=1, StackItem$(a, 1)="OK"
|
||||
Print StackType$(a, 1)="String", StackType$(a,2)="Number"
|
||||
}
|
||||
CheckStackAsLIFO
|
||||
Module CheckStackAsFIFO {
|
||||
a=stack
|
||||
Stack a {
|
||||
Data 1, 2, 3
|
||||
Print number=1
|
||||
Print number=2
|
||||
Print number=3
|
||||
Print Empty=True
|
||||
Data "A", "B", "C"
|
||||
Print letter$="A"
|
||||
Print letter$="B"
|
||||
Print letter$="C"
|
||||
Print Empty=True
|
||||
Push 1,"OK"
|
||||
}
|
||||
Print Len(a)=2, StackItem(a, 2)=1, StackItem$(a, 1)="OK"
|
||||
Print StackType$(a, 1)="String", StackType$(a,2)="Number"
|
||||
}
|
||||
CheckStackAsFIFO
|
||||
14
Task/Queue-Usage/Maple/queue-usage.maple
Normal file
14
Task/Queue-Usage/Maple/queue-usage.maple
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
q := queue[new]();
|
||||
queue[enqueue](q,1);
|
||||
queue[enqueue](q,2);
|
||||
queue[enqueue](q,3);
|
||||
queue[empty](q);
|
||||
>>>false
|
||||
queue[dequeue](q);
|
||||
>>>1
|
||||
queue[dequeue](q);
|
||||
>>>2
|
||||
queue[dequeue](q);
|
||||
>>>3
|
||||
queue[empty](q);
|
||||
>>>true
|
||||
16
Task/Queue-Usage/Mathematica/queue-usage.math
Normal file
16
Task/Queue-Usage/Mathematica/queue-usage.math
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
Empty[a_] := If[Length[a] == 0, True, False]
|
||||
SetAttributes[Push, HoldAll]; Push[a_, elem_] := AppendTo[a, elem]
|
||||
SetAttributes[Pop, HoldAllComplete]; Pop[a_] := If[EmptyQ[a], False, b = First[a]; Set[a, Most[a]]; b]
|
||||
|
||||
Queue = {}
|
||||
-> {}
|
||||
Empty[Queue]
|
||||
-> True
|
||||
Push[Queue, "1"]
|
||||
-> {"1"}
|
||||
EmptyQ[Queue]
|
||||
->False
|
||||
Pop[Queue]
|
||||
->1
|
||||
Pop[Queue]
|
||||
->False
|
||||
4
Task/Queue-Usage/Nemerle/queue-usage.nemerle
Normal file
4
Task/Queue-Usage/Nemerle/queue-usage.nemerle
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mutable q = Queue(); // or use immutable version as per Haskell example
|
||||
def empty = q.IsEmpty(); // true at this point
|
||||
q.Push(empty); // or Enqueue(), or Add()
|
||||
def a = q.Pop(); // or Dequeue() or Take()
|
||||
65
Task/Queue-Usage/NetRexx/queue-usage.netrexx
Normal file
65
Task/Queue-Usage/NetRexx/queue-usage.netrexx
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
-- Queue Usage Demonstration Program -------------------------------------------
|
||||
method main(args = String[]) public constant
|
||||
kew = RCQueueImpl()
|
||||
do
|
||||
say kew.pop()
|
||||
catch ex = IndexOutOfBoundsException
|
||||
say ex.getMessage
|
||||
say
|
||||
end
|
||||
|
||||
melancholyDane = ''
|
||||
melancholyDane[0] = 4
|
||||
melancholyDane[1] = 'To be'
|
||||
melancholyDane[2] = 'or'
|
||||
melancholyDane[3] = 'not to be?'
|
||||
melancholyDane[4] = 'That is the question.'
|
||||
|
||||
loop p_ = melancholyDane[0] to 1 by -1
|
||||
kew.push(melancholyDane[p_])
|
||||
end p_
|
||||
|
||||
loop while \kew.empty
|
||||
popped = kew.pop
|
||||
say popped '\-'
|
||||
end
|
||||
say; say
|
||||
|
||||
-- demonstrate stowing something other than a text string in the queue
|
||||
kew.push(melancholyDane)
|
||||
md = kew.pop
|
||||
loop l_ = 1 to md[0]
|
||||
say md[l_] '\-'
|
||||
end l_
|
||||
say
|
||||
|
||||
return
|
||||
|
||||
-- Queue implementation --------------------------------------------------------
|
||||
class RCQueueImpl
|
||||
properties private
|
||||
qqq = Deque
|
||||
|
||||
method RCQueueImpl() public
|
||||
qqq = ArrayDeque()
|
||||
return
|
||||
|
||||
method push(stuff) public
|
||||
qqq.push(stuff)
|
||||
return
|
||||
|
||||
method pop() public returns Rexx signals IndexOutOfBoundsException
|
||||
if qqq.isEmpty then signal IndexOutOfBoundsException('The queue is empty')
|
||||
return Rexx qqq.pop()
|
||||
|
||||
method empty() public binary returns boolean
|
||||
return qqq.isEmpty
|
||||
|
||||
method isTrue public constant binary returns boolean
|
||||
return 1 == 1
|
||||
|
||||
method isFalse public constant binary returns boolean
|
||||
return \isTrue
|
||||
12
Task/Queue-Usage/Nim/queue-usage.nim
Normal file
12
Task/Queue-Usage/Nim/queue-usage.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import deques
|
||||
|
||||
var queue = initDeque[int]()
|
||||
|
||||
queue.addLast(26)
|
||||
queue.addLast(99)
|
||||
queue.addLast(2)
|
||||
echo "Queue size: ", queue.len()
|
||||
echo "Popping: ", queue.popFirst()
|
||||
echo "Popping: ", queue.popFirst()
|
||||
echo "Popping: ", queue.popFirst()
|
||||
echo "Popping: ", queue.popFirst()
|
||||
37
Task/Queue-Usage/OCaml/queue-usage.ocaml
Normal file
37
Task/Queue-Usage/OCaml/queue-usage.ocaml
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# let q = Queue.create ();;
|
||||
val q : '_a Queue.t = <abstr>
|
||||
# Queue.is_empty q;;
|
||||
- : bool = true
|
||||
# Queue.add 1 q;;
|
||||
- : unit = ()
|
||||
# Queue.is_empty q;;
|
||||
- : bool = false
|
||||
# Queue.add 2 q;;
|
||||
- : unit = ()
|
||||
# Queue.add 3 q;;
|
||||
- : unit = ()
|
||||
# Queue.peek q;;
|
||||
- : int = 1
|
||||
# Queue.length q;;
|
||||
- : int = 3
|
||||
# Queue.iter (Printf.printf "%d, ") q; print_newline ();;
|
||||
1, 2, 3,
|
||||
- : unit = ()
|
||||
# Queue.take q;;
|
||||
- : int = 1
|
||||
# Queue.take q;;
|
||||
- : int = 2
|
||||
# Queue.peek q;;
|
||||
- : int = 3
|
||||
# Queue.length q;;
|
||||
- : int = 1
|
||||
# Queue.add 4 q;;
|
||||
- : unit = ()
|
||||
# Queue.take q;;
|
||||
- : int = 3
|
||||
# Queue.peek q;;
|
||||
- : int = 4
|
||||
# Queue.take q;;
|
||||
- : int = 4
|
||||
# Queue.is_empty q;;
|
||||
- : bool = true
|
||||
14
Task/Queue-Usage/Objeck/queue-usage.objeck
Normal file
14
Task/Queue-Usage/Objeck/queue-usage.objeck
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Test {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
q := Struct.IntQueue->New();
|
||||
q->Add(1);
|
||||
q->Add(2);
|
||||
q->Add(3);
|
||||
|
||||
q->Remove()->PrintLine();
|
||||
q->Remove()->PrintLine();
|
||||
q->Remove()->PrintLine();
|
||||
|
||||
q->IsEmpty()->PrintLine();
|
||||
}
|
||||
}
|
||||
5
Task/Queue-Usage/Oforth/queue-usage.fth
Normal file
5
Task/Queue-Usage/Oforth/queue-usage.fth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
: testQueue
|
||||
| q i |
|
||||
Queue new ->q
|
||||
20 loop: i [ i q push ]
|
||||
while ( q empty not ) [ q pop . ] ;
|
||||
5
Task/Queue-Usage/OoRexx/queue-usage.rexx
Normal file
5
Task/Queue-Usage/OoRexx/queue-usage.rexx
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
q = .queue~new -- create an instance
|
||||
q~queue(3) -- adds to the end, but this is at the front
|
||||
q~push(1) -- push on the front
|
||||
q~queue(2) -- add to the end
|
||||
say q~pull q~pull q~pull q~isempty -- should display all and be empty
|
||||
12
Task/Queue-Usage/Oz/queue-usage.oz
Normal file
12
Task/Queue-Usage/Oz/queue-usage.oz
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
declare
|
||||
[Queue] = {Link ['x-oz://system/adt/Queue.ozf']}
|
||||
MyQueue = {Queue.new}
|
||||
in
|
||||
{MyQueue.isEmpty} = true
|
||||
{MyQueue.put foo}
|
||||
{MyQueue.put bar}
|
||||
{MyQueue.put baz}
|
||||
{MyQueue.isEmpty} = false
|
||||
{Show {MyQueue.get}} %% foo
|
||||
{Show {MyQueue.get}} %% bar
|
||||
{Show {MyQueue.get}} %% baz
|
||||
10
Task/Queue-Usage/PHP/queue-usage.php
Normal file
10
Task/Queue-Usage/PHP/queue-usage.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
$queue = new SplQueue;
|
||||
echo $queue->isEmpty() ? 'true' : 'false', "\n"; // empty test - returns true
|
||||
// $queue->dequeue(); // would raise RuntimeException
|
||||
$queue->enqueue(1);
|
||||
$queue->enqueue(2);
|
||||
$queue->enqueue(3);
|
||||
echo $queue->dequeue(), "\n"; // returns 1
|
||||
echo $queue->isEmpty() ? 'true' : 'false', "\n"; // returns false
|
||||
?>
|
||||
40
Task/Queue-Usage/PL-I/queue-usage-1.pli
Normal file
40
Task/Queue-Usage/PL-I/queue-usage-1.pli
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
test: proc options (main);
|
||||
|
||||
|
||||
/* To implement a queue. */
|
||||
define structure
|
||||
1 node,
|
||||
2 value fixed,
|
||||
2 link handle(node);
|
||||
declare (head, tail, t) handle (node);
|
||||
declare null builtin;
|
||||
declare i fixed binary;
|
||||
|
||||
head, tail = bind(:node, null:);
|
||||
|
||||
do i = 1 to 10; /* Add ten items to the tail of the queue. */
|
||||
if head = bind(:node, null:) then
|
||||
do;
|
||||
head,tail = new(:node:);
|
||||
get list (head => value);
|
||||
put skip list (head => value);
|
||||
head => link = bind(:node, null:); /* A NULL link */
|
||||
end;
|
||||
else
|
||||
do;
|
||||
t = new(:node:);
|
||||
tail => link = t; /* Point the tail to the new node. */
|
||||
tail = t;
|
||||
tail => link = bind(:node, null:); /* Set the tail link to NULL */
|
||||
get list (tail => value) copy;
|
||||
put skip list (tail => value);
|
||||
end;
|
||||
end;
|
||||
|
||||
/* Pop all the items in the queue. */
|
||||
put skip list ('The queue has:');
|
||||
do while (head ^= bind(:node, null:));
|
||||
put skip list (head => value);
|
||||
head = head => link;
|
||||
end;
|
||||
end test;
|
||||
21
Task/Queue-Usage/PL-I/queue-usage-2.pli
Normal file
21
Task/Queue-Usage/PL-I/queue-usage-2.pli
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
1
|
||||
3
|
||||
5
|
||||
7
|
||||
9
|
||||
11
|
||||
13
|
||||
15
|
||||
17
|
||||
19
|
||||
The queue has:
|
||||
1
|
||||
3
|
||||
5
|
||||
7
|
||||
9
|
||||
11
|
||||
13
|
||||
15
|
||||
17
|
||||
19
|
||||
11
Task/Queue-Usage/Perl/queue-usage-1.pl
Normal file
11
Task/Queue-Usage/Perl/queue-usage-1.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
@queue = (); # we will simulate a queue in a array
|
||||
|
||||
push @queue, (1..5); # enqueue numbers from 1 to 5
|
||||
|
||||
print shift @queue,"\n"; # dequeue
|
||||
|
||||
print "array is empty\n" unless @queue; # is empty ?
|
||||
|
||||
print $n while($n = shift @queue); # dequeue all
|
||||
print "\n";
|
||||
print "array is empty\n" unless @queue; # is empty ?
|
||||
3
Task/Queue-Usage/Perl/queue-usage-2.pl
Normal file
3
Task/Queue-Usage/Perl/queue-usage-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
1
|
||||
2345
|
||||
array is empty
|
||||
10
Task/Queue-Usage/Phix/queue-usage-1.phix
Normal file
10
Task/Queue-Usage/Phix/queue-usage-1.phix
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">empty</span><span style="color: #0000FF;">())</span> <span style="color: #000080;font-style:italic;">-- true</span>
|
||||
<span style="color: #000000;">push_item</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">empty</span><span style="color: #0000FF;">())</span> <span style="color: #000080;font-style:italic;">-- false</span>
|
||||
<span style="color: #000000;">push_item</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pop_item:%v\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pop_item</span><span style="color: #0000FF;">())</span> <span style="color: #000080;font-style:italic;">-- 5</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pop_item:%v\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pop_item</span><span style="color: #0000FF;">())</span> <span style="color: #000080;font-style:italic;">-- 6</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">empty</span><span style="color: #0000FF;">())</span> <span style="color: #000080;font-style:italic;">-- true</span>
|
||||
<!--
|
||||
11
Task/Queue-Usage/Phix/queue-usage-2.phix
Normal file
11
Task/Queue-Usage/Phix/queue-usage-2.phix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">queue</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_queue</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">queue_empty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">push</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">queue_empty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">push</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pop:%v\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">pop</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"pop:%v\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">pop</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"empty:%t\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">queue_empty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">queue</span><span style="color: #0000FF;">))</span>
|
||||
<!--
|
||||
5
Task/Queue-Usage/PicoLisp/queue-usage.l
Normal file
5
Task/Queue-Usage/PicoLisp/queue-usage.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(println (fifo 'Queue)) # Retrieve the number '1'
|
||||
(println (fifo 'Queue)) # Retrieve an internal symbol 'abc'
|
||||
(println (fifo 'Queue)) # Retrieve a transient symbol "abc"
|
||||
(println (fifo 'Queue)) # and a list (abc)
|
||||
(println (fifo 'Queue)) # Queue is empty -> NIL
|
||||
6
Task/Queue-Usage/PostScript/queue-usage.ps
Normal file
6
Task/Queue-Usage/PostScript/queue-usage.ps
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[1 2 3 4 5] 6 exch tadd
|
||||
= [1 2 3 4 5 6]
|
||||
uncons
|
||||
= 1 [2 3 4 5 6]
|
||||
[] empty?
|
||||
=true
|
||||
26
Task/Queue-Usage/PowerShell/queue-usage-1.psh
Normal file
26
Task/Queue-Usage/PowerShell/queue-usage-1.psh
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
[System.Collections.ArrayList]$queue = @()
|
||||
# isEmpty?
|
||||
if ($queue.Count -eq 0) {
|
||||
"isEmpty? result : the queue is empty"
|
||||
} else {
|
||||
"isEmpty? result : the queue is not empty"
|
||||
}
|
||||
"the queue contains : $queue"
|
||||
$queue += 1 # push
|
||||
"push result : $queue"
|
||||
$queue += 2 # push
|
||||
$queue += 3 # push
|
||||
"push result : $queue"
|
||||
|
||||
$queue.RemoveAt(0) # pop
|
||||
"pop result : $queue"
|
||||
|
||||
$queue.RemoveAt(0) # pop
|
||||
"pop result : $queue"
|
||||
|
||||
if ($queue.Count -eq 0) {
|
||||
"isEmpty? result : the queue is empty"
|
||||
} else {
|
||||
"isEmpty? result : the queue is not empty"
|
||||
}
|
||||
"the queue contains : $queue"
|
||||
3
Task/Queue-Usage/PowerShell/queue-usage-2.psh
Normal file
3
Task/Queue-Usage/PowerShell/queue-usage-2.psh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$queue = New-Object -TypeName System.Collections.Queue
|
||||
#or
|
||||
$queue = [System.Collections.Queue] @()
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-3.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
Get-Member -InputObject $queue
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-4.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-4.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
1,2,3 | ForEach-Object {$queue.Enqueue($_)}
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-5.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-5.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$queue.Peek()
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-6.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-6.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$queue.Dequeue()
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-7.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-7.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$queue.Clear()
|
||||
1
Task/Queue-Usage/PowerShell/queue-usage-8.psh
Normal file
1
Task/Queue-Usage/PowerShell/queue-usage-8.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
if (-not $queue.Count) {"Queue is empty"}
|
||||
48
Task/Queue-Usage/Prolog/queue-usage.pro
Normal file
48
Task/Queue-Usage/Prolog/queue-usage.pro
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% definitions of queue
|
||||
empty(U-V) :-
|
||||
unify_with_occurs_check(U, V).
|
||||
|
||||
push(Queue, Value, NewQueue) :-
|
||||
append_dl(Queue, [Value|X]-X, NewQueue).
|
||||
|
||||
|
||||
pop([X|V]-U, X, V-U) :-
|
||||
\+empty([X|V]-U).
|
||||
|
||||
|
||||
|
||||
append_dl(X-Y, Y-Z, X-Z).
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% use of queue
|
||||
queue :-
|
||||
% create an empty queue
|
||||
empty(Q),
|
||||
format('Create queue ~w~n~n', [Q]),
|
||||
|
||||
% add numbers 1 and 2
|
||||
write('Add numbers 1 and 2 : '),
|
||||
push(Q, 1, Q1),
|
||||
push(Q1, 2, Q2),
|
||||
|
||||
% display queue
|
||||
format('~w~n~n', [Q2]),
|
||||
|
||||
% pop element
|
||||
pop(Q2, V, Q3),
|
||||
|
||||
% display results
|
||||
format('Pop : Value ~w Queue : ~w~n~n', [V, Q3]),
|
||||
|
||||
% test the queue
|
||||
write('Test of the queue : '),
|
||||
( empty(Q3) -> writeln('Queue empy'); writeln('Queue not empty')), nl,
|
||||
|
||||
% pop the elements
|
||||
write('Pop the queue : '),
|
||||
pop(Q3, V1, Q4),
|
||||
format('Value ~w Queue : ~w~n~n', [V1, Q4]),
|
||||
|
||||
write('Pop the queue : '),
|
||||
pop(Q4, _V, _Q5).
|
||||
36
Task/Queue-Usage/PureBasic/queue-usage.basic
Normal file
36
Task/Queue-Usage/PureBasic/queue-usage.basic
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
NewList MyStack()
|
||||
|
||||
Procedure Push(n)
|
||||
Shared MyStack()
|
||||
LastElement(MyStack())
|
||||
AddElement(MyStack())
|
||||
MyStack()=n
|
||||
EndProcedure
|
||||
|
||||
Procedure Pop()
|
||||
Shared MyStack()
|
||||
Protected n
|
||||
If FirstElement(MyStack()) ; e.g. Stack not empty
|
||||
n=MyStack()
|
||||
DeleteElement(MyStack(),1)
|
||||
EndIf
|
||||
ProcedureReturn n
|
||||
EndProcedure
|
||||
|
||||
Procedure Empty()
|
||||
Shared MyStack()
|
||||
If ListSize(MyStack())=0
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
ProcedureReturn #False
|
||||
EndProcedure
|
||||
|
||||
;---- Example of implementation ----
|
||||
Push(3)
|
||||
Push(1)
|
||||
Push(4)
|
||||
Push(1)
|
||||
Push(5)
|
||||
While Not Empty()
|
||||
Debug Pop()
|
||||
Wend
|
||||
8
Task/Queue-Usage/Python/queue-usage.py
Normal file
8
Task/Queue-Usage/Python/queue-usage.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import Queue
|
||||
my_queue = Queue.Queue()
|
||||
my_queue.put("foo")
|
||||
my_queue.put("bar")
|
||||
my_queue.put("baz")
|
||||
print my_queue.get() # foo
|
||||
print my_queue.get() # bar
|
||||
print my_queue.get() # baz
|
||||
7
Task/Queue-Usage/Quackery/queue-usage.quackery
Normal file
7
Task/Queue-Usage/Quackery/queue-usage.quackery
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[ [] ] is queue ( --> q )
|
||||
|
||||
[ nested join ] is push ( q x --> q )
|
||||
|
||||
[ behead ] is pop ( q --> q x )
|
||||
|
||||
[ [] = ] is empty? ( q --> b )
|
||||
15
Task/Queue-Usage/REBOL/queue-usage.rebol
Normal file
15
Task/Queue-Usage/REBOL/queue-usage.rebol
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
; 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]
|
||||
22
Task/Queue-Usage/REXX/queue-usage.rexx
Normal file
22
Task/Queue-Usage/REXX/queue-usage.rexx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/*REXX program demonstrates four queueing operations: push, pop, empty, query. */
|
||||
say '══════════════════════════════════ Pushing five values to the stack.'
|
||||
do j=1 for 4 /*a DO loop to PUSH four values. */
|
||||
call push j * 10 /*PUSH (aka: enqueue to the stack).*/
|
||||
say 'pushed value:' j * 10 /*echo the pushed value. */
|
||||
if j\==3 then iterate /*Not equal 3? Then use a new number.*/
|
||||
call push /*PUSH (aka: enqueue to the stack).*/
|
||||
say 'pushed a "null" value.' /*echo what was pushed to the stack. */
|
||||
end /*j*/
|
||||
say '══════════════════════════════════ Quering the stack (number of entries).'
|
||||
say queued() ' entries in the stack.'
|
||||
say '══════════════════════════════════ Popping all values from the stack.'
|
||||
do k=1 while \empty() /*EMPTY (returns TRUE [1] if empty).*/
|
||||
call pop /*POP (aka: dequeue from the stack).*/
|
||||
say k': popped value=' result /*echo the popped value. */
|
||||
end /*k*/
|
||||
say '══════════════════════════════════ The stack is now empty.'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
push: queue arg(1); return /*(The REXX QUEUE is FIFO.) */
|
||||
pop: procedure; parse pull x; return x /*REXX PULL removes a stack item. */
|
||||
empty: return queued()==0 /*returns the status of the stack. */
|
||||
17
Task/Queue-Usage/Racket/queue-usage.rkt
Normal file
17
Task/Queue-Usage/Racket/queue-usage.rkt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#lang racket
|
||||
|
||||
(require data/queue)
|
||||
|
||||
(define queue (make-queue))
|
||||
|
||||
(enqueue! queue 'black)
|
||||
(queue-empty? queue) ; #f
|
||||
|
||||
(enqueue! queue 'red)
|
||||
(enqueue! queue 'green)
|
||||
|
||||
(dequeue! queue) ; 'black
|
||||
(dequeue! queue) ; 'red
|
||||
(dequeue! queue) ; 'green
|
||||
|
||||
(queue-empty? queue) ; #t
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue