data-structures

Stack

Class

Example

      
const stack = new Stack([1, 2, 3, 4, 5]);

Members

add

number

Appends new elements to the end of the sequence, and returns the new length of the sequence.

@complexityO(n)
@paramnodeT[]
@returnsnumber
      
const stack = new Stack([1, 2, 3, 4, 5]);
console.log(stack.remove()) //[1, 2, 3, 4]

Removes and returns the last element in the stack.

@complexityO(n)
@returnsT

Returns the item located at the specified index.

@complexity𝛩(1)
@paramindexnumber

The zero-based index of the desired code unit. A negative index will count back from the last item.

@returnsT

length

number

Gets or sets the length of the sequence. This is a number one higher than the highest index in the sequence.

      
console.log(stack.setAt(1,6)) //1,6,3,4,5

Overwrites the value at the provided index with the given value. If the index is negative, then it replaces from the end of the sequence.

@complexity𝛩(1)
@paramindexnumber

The index of the value to overwrite. If the index is negative, then it replaces from the end of the sequence.

@paramvalueT

The value to write into the provided index v.

@returns

The updated sequence.