0.0.1 • Published 2 years ago

data-structuring v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

Data Structures library

Stack

Stack is a data structure that can be used to store and manipulate a list of elements in a LIFO (last in, first out) fashion.

Constructor

Stack initialization is done by passing an array of elements to the constructor. Second parameter contains stack options

Options

NameDescriptionTypeDefault value
sizeSize of the stacknumberInfinity
strictSizeSets size to be maximum size. Throws an error while attempt to push to full stackbooleanfalse

Stack(items?: Array<T> | T, options: TStackOptions = defaultOptions)

const stack = new Stack(
    [1, 2, 3],
    {
      size: 5,
      strictSize: true,
    }
);

Push

Push an element to the stack.

stack.push(4); // stack: [1, 2, 3, 4]

Pop

Pop an element from the stack.

stack.pop(); // returns 4, stack: [1, 2, 3]

Is Empty

Check if the stack is empty.

stack.isEmpty(); // false

Is Full

Check if the stack is full.

stack.isFull(); // false

Peek

Peek the last element of the stack without removing it.

stack.peek(); // returns 3, stack: [1, 2, 3]

Swap

Swaps the two elements from the params.

stack.swap(1, 2); // stack: [2, 1, 3]

Swap by index

Swaps the two elements from the params by their indexes.

stack.swapByIndex(0, 1); // stack: [1, 2, 3]

Getters

NameReturns
itemsArray of items in the stack
sizeSize of the stack
strictSizeIs array strict size