Stack

A Stack is a collection of any type in kOS. Stacks work according to Last-In first-out principle. It may be useful to contrast Stack with Queue to better understand how both structures work. You can read more about stacks on Wikipedia.

Using a stack

SET S TO STACK().
S:PUSH("alice").
S:PUSH("bob").

PRINT S:POP. // will print 'bob'
PRINT S:POP. // will print 'alice'

Structure

structure Stack
Members
Suffix Type Description
PUSH(item) None add item to the top of the stack
POP() any type returns the item on top of the stack and removes it
PEEK() any type returns the item on top of the stack without removing it
LENGTH integer number of elements in the stack
CLEAR() None remove all elements
ITERATOR Iterator for iterating over the stack
COPY Stack a new copy of this stack
CONTAINS(item) boolean check if stack contains an item
EMPTY boolean check if stack if empty
DUMP string verbose dump of all contained elements
Stack:PUSH(item)
Parameters:
  • item – (any type) item to be added

Adds the item to the top of the stack.

Stack:POP()

Returns the item on top of the stack and removes it.

Stack:PEEK()

Returns the item on top of the stack without removing it.

Stack:CLEAR()

Removes all elements from the stack.

Stack:LENGTH
Type:integer
Access:Get only

Returns the number of elements in the stack.

Stack:ITERATOR
Type:Iterator
Access:Get only

An alternate means of iterating over a stack. See: Iterator.

Stack:COPY
Type:Stack
Access:Get only

Returns a new stack that contains the same thing as the old one.

Stack:CONTAINS(item)
Parameters:
  • index – (integer) starting index (from zero)
Returns:

boolean

Returns true if the stack contains an item equal to the one passed as an argument

Stack:EMPTY
Type:boolean
Access:Get only

Returns true if the stack has zero items in it.

Stack:DUMP
Type:string
Access:Get only

Returns a string containing a verbose dump of the stack’s contents.