The Stack data structure is implemented under the classname Stack
.
It fully works under LIFO (First in First out), which means the API only
exposes pushing and popping.
This uses a Linked List in the background.
The implementation supports a wide variaty of operations. Below are a few use-cases.
// Creating a Stack
const stack = new Stack<number>();
// Checking if the stack is empty
stack.isEmpty();
// Peeking the first element
stack.peek();
// Pushing a new element to the top:
stack.push(10);
stack.push(5)
// Popping that same element off of the stack:
stack.pop(); // 5
The Stack
class implements the Streamable
interface
Note: Keep in mind that the stack iterates using the pop
Method. That means
that iterating is a destructive action and discards used elements.
// The list supports iteration
for (item in stack) {
console.log(item);
}
// Alternatively the iterator can be called explicitly
let stream = stack.stream()
stream.next();
stream.next();