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¶
¶ Suffix
Type
Description
All suffixes of
Enumerable
Stack
objects are a type ofEnumerable
None
add item to the top of the stack
any type
returns the item on top of the stack and removes it
any type
returns the item on top of the stack without removing it
None
remove all elements
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.