Documentation
Stack

Stack

A Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.

Ultify's stack is a wrapper around Javascript array push/pop with a standard stack interface.

Operations

  • Push: Adds an element to the top of the stack.
  • Pop: Removes the top element from the stack.
  • Peek: Returns the top element without removing it.
  • Clear: Empty the stack.
  • IsEmpty: Checks if the stack is empty.
  • toArray: Convert stack to an array.

APIs

Constructor

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack()
console.log("Empty stack:", foo.toArray())

// Create a stack with pre-filled values
const bar = new Stack(1, 2, 3, 4)
console.log("Pre-filled stack:", bar.toArray())

fromArray (static)

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = Stack.fromArray([1, 2, 3])
console.log(foo.toArray())

foo.push(1)
foo.push(2)

console.log(foo.toArray())

toArray

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack(1, 2, 3)

console.log(foo.toArray())

Push

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack()
console.log(foo.toArray())

foo.push(1)
foo.push(2)

console.log(foo.toArray())

Pop

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack()

foo.push(1)
foo.push(2)

console.log(foo.toArray())

console.log(foo.pop())

console.log(foo.toArray())

Peak

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack()

foo.push(1)
foo.push(2)

console.log(foo.toArray())

console.log("Peek:", foo.peek())

console.log(foo.toArray())

Clear

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack()

foo.push(1)
foo.push(2)

console.log(foo.toArray())

foo.clear()

console.log(foo.toArray())

isEmpty

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(0, 1, 2, 3)
console.log(foo.size)

console.log(foo.isEmpty())

size (readonly)

import { Stack } from '@ultify/datastructure'

// Create an empty stack
const foo = new Stack(0, 1, 2, 3)
console.log(foo.size)

foo.push(4)
console.log(foo.size)