Represents a stack collection of elements. A stack allows elements to be
added and removed from the end of the collection only, following the
LIFO (Last In, First Out) principle. This interface extends the Collection
interface and specifies additional stack-specific operations.
Stacks are commonly used in scenarios where you need to temporarily store and retrieve elements in a reverse order of their addition, such as in undo mechanisms, parsing algorithms, and temporary buffers.
Name |
---|
V |
-
Collection
<number
,V
>↳
Stack
↳↳
Deque
• get
[toStringTag](): string
A string tag for the Collection
class that is used in Object.prototype.toString
.
string
Collection.[toStringTag]
• get
size(): number
Gets the number of elements contained in the Collection
.
number
Collection.size
▸ [iterator](): IterableIterator
<V
>
Returns the default iterator through the stack's elements.
IterableIterator
<V
>
▸ clear(): void
Removes all elements from the Collection
, effectively resetting it.
void
▸ entries(): IterableIterator
<[number
, V
]>
Returns a new iterator object that contains an array of [key, value]
pairs for each element in the Collection
.
IterableIterator
<[number
, V
]>
An iterable iterator for the entries of the collection.
▸ forEach(callbackfn
, thisArg?
): void
Executes a provided function once for each key-value pair in the Collection
.
Name | Type | Description |
---|---|---|
callbackfn |
(value : V , key : number , collection : this ) => void |
A function to execute for each element, receiving the value, key, and collection instance as arguments. |
thisArg? |
unknown |
An optional value to use as this when executing callbackfn . |
void
▸ has(value
): boolean
Determines whether an element exists within the stack.
Name | Type | Description |
---|---|---|
value |
V |
The value to locate. |
boolean
true
if the value exists, 'false' otherwise.
▸ keys(): IterableIterator
<number
>
Returns a new iterator that contains the keys for each element
in the Collection
.
IterableIterator
<number
>
An iterable iterator for the keys of the collection.
▸ last(): undefined
| V
Retrieves the last element added to the stack without removing it.
undefined
| V
The last element of the stack, or undefined
if the stack is empty.
▸ pop(): undefined
| V
Removes the last element added to the stack and returns it.
undefined
| V
The last element of the stack, or undefined
if the stack is empty.
▸ push(...values
): number
Adds one or more elements to the end of the stack and returns the stack's new length.
Name | Type | Description |
---|---|---|
...values |
V [] |
The elements to add. |
number
The new length of the stack.
▸ top(): undefined
| V
Retrieves the element at the top of the stack without removing it.
This method provides semantic clarity in contexts where the term "top" is preferred over "last" to describe the most recently added element.
undefined
| V
The element at the top of the stack, or undefined
if the stack is empty.
▸ values(): IterableIterator
<V
>
Returns a new iterator that contains the values for each element
in the Collection
.
IterableIterator
<V
>
An iterable iterator for the values of the collection.