Queue¶
A Queue
is a collection of any type in kOS. Queues work according to First-In first-out principle. It may be useful to contrast
Queue
with Stack
to better understand how both structures work. You can read more about queues on Wikipedia.
Using a queue¶
SET Q TO QUEUE().
Q:PUSH("alice").
Q:PUSH("bob").
PRINT Q:POP. // will print 'alice'
PRINT Q:POP. // will print 'bob'
Structure¶
-
structure
Queue
¶ ¶ Suffix Type Description PUSH(item)
None add item to the end of the queue POP()
any type returns the first element in the queue and removes it PEEK()
any type returns the first element in the queue without removing it LENGTH
integer number of elements in the queue CLEAR()
None remove all elements ITERATOR
Iterator
for iterating over the queue COPY
Queue
a new copy of this queue CONTAINS(item)
boolean check if queue contains an item EMPTY
boolean check if queue if empty DUMP
string verbose dump of all contained elements
-
Queue:
PUSH
(item)¶ Parameters: - item – (any type) item to be added
Adds the item to the end of the queue.
-
Queue:
POP
()¶ Returns the item in the front of the queue and removes it.
-
Queue:
PEEK
()¶ Returns the item in the front of the queue without removing it.
-
Queue:
CLEAR
()¶ Removes all elements from the queue.
-
Queue:
LENGTH
¶ Type: integer Access: Get only Returns the number of elements in the queue.
-
Queue:
ITERATOR
¶ Type: Iterator
Access: Get only An alternate means of iterating over a queue. See:
Iterator
.
-
Queue:
COPY
¶ Type: Queue
Access: Get only Returns a new queue that contains the same thing as the old one.
-
Queue:
CONTAINS
(item)¶ Parameters: - index – (integer) starting index (from zero)
Returns: boolean
Returns true if the queue contains an item equal to the one passed as an argument
-
Queue:
EMPTY
¶ Type: boolean Access: Get only Returns true if the queue has zero items in it.
-
Queue:
DUMP
¶ Type: string Access: Get only Returns a string containing a verbose dump of the queue’s contents.