-
-
Notifications
You must be signed in to change notification settings - Fork 0
Stack
This module provides an adapter for a list so it can be used with a stack-like interface. See the Wikipedia's Stack article for more information.
Legend for tables
-
API
-
value- the value to be stored in the stack -
values- an iterable that provides values -
iterator- an Iterator instance or an object with an iterator/iterable protocol
-
-
Complexity
- O - complexity of an operation
- O(1) - constant time
- O(n) - linear time proportional to the stack size
- O(k) - linear time proportional to the argument size
This module provides an adapter for a list so it can be used with a stack-like interface. By default it uses ValueList:
import Stack from 'list-toolkit/stack.js';
const stack = new Stack();The following properties are available:
| Member | Return type | Description | O |
|---|---|---|---|
constructor(UnderlyingList = ValueList) |
this |
constructs a new Stack
|
O(1) |
isEmpty |
boolean | checks if the stack is empty | O(1) |
size |
number | the stack's size | O(1) |
list |
list | the underlying list | O(1) |
top |
value or undefined
|
the stack's top element | O(1) |
peek() |
value or undefined
|
the stack's top element | O(1) |
push(value) |
this |
push an element onto the stack | O(1) |
pushFront(value) |
this |
an alias for push(value)
|
O(1) |
pop() |
value or undefined
|
remove and return the stack's top element | O(1) |
pushValues(values) |
this |
push values onto the stack sequentially | O(k) |
clear() |
this |
remove all elements from the stack | O(1) |
[Symbol.iterator]() |
iterator | return the default iterator starting from the top element | O(1) |
getReverseIterator() |
iterator or undefined
|
return the reverse iterator starting from the bottom element | O(1) |
constructor(UnderlyingList) takes a list class (not an instance) as an argument. By default,
it uses ValueList. A new instance of the list is created internally.
top and peek() is the same code packaged as a getter and as an argument-less function.
They both assume that a front node has the value property, which is returned as a result.
All value-based lists satisfy this requirement. If your nodes don't have value,
you can always access the node directly with list.front.
push() uses pushFront() and pop() uses popFront() on the underlying list.
For value-based lists they operate with values, not nodes. pop() returns the value
property of the removed node.
If the underlying list is SLL, getReverseIterator() will return undefined.
Stack is exported as Stack and as the default export.
Concepts
DLL: doubly linked lists
SLL: singly linked lists
Unrolled list
List utilities
Caches
Heaps
Queue, Stack, and Deque
Trees
Skip list
Timer wheel
Free list