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
All suffixes of Enumerable   Stack objects are a type of Enumerable
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
CLEAR() None remove all elements
COPY Stack a new copy of this stack

Note

This type is serializable.

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:COPY
Type:Stack
Access:Get only

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