Stack

class Stack<T> : Collection<T>

Generic Stack

A simple stack implementation.

Example:

// Create a new stack
val stack = Stack<Int>()

// Push 3, 2, then 1 onto the stack.
for (i in 3 downTo 1)
stack.push(i)

// Pop 1, 2, then 3 from the stack.
for (i in 1 .. 3)
require(stack.pop() == i)

Author

Elizabeth Paige Harper - https://github.com/foxcapades

Since

0.1.0

Parameters

T

Type of items that will be pushed onto this stack.

Constructors

Link copied to clipboard
fun Stack(initialCapacity: Int = 8, scaleFactor: Float = 1.5f, maxSize: Int = Int.MAX_VALUE)

Constructs a new Stack instance.

Functions

Link copied to clipboard
fun clear()

Removes all items from this stack.

Link copied to clipboard
open operator override fun contains(element: T): Boolean

Returns true if this stack contains the given element.

Link copied to clipboard
open override fun containsAll(elements: Collection<T>): Boolean

Returns true if this stack contains all the given elements.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Returns whether this stack equals the given other value.

Link copied to clipboard
operator fun get(index: Int): T

Operator function allowing array style access of the stack.

Link copied to clipboard
open override fun hashCode(): Int

Returns the hash code value for this stack.

Link copied to clipboard
open override fun isEmpty(): Boolean

Returns true if this stack contains zero items.

Link copied to clipboard

Returns true if this stack contains one or more items.

Link copied to clipboard
open operator override fun iterator(): Iterator<T>

Returns a new, destructive, consuming iterator over this stack's contents that pops items from this stack as it is iterated.

Link copied to clipboard
fun peek(): T

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

Link copied to clipboard
fun pop(): T

Removes and returns the item currently on top of this stack.

Link copied to clipboard
fun push(value: T)

Pushes the given item onto the top of this stack.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of this stack containing the stack's current size.

Properties

Link copied to clipboard

Current capacity of the stack's underlying data container.

Link copied to clipboard

Max capacity this stack will permit.

Link copied to clipboard
open override var size: Int = 0

Current number of items currently on the stack.