src/stack/stack.ts
Properties |
Methods |
Accessors |
store |
Type : T[]
|
Default value : []
|
Defined in src/stack/stack.ts:3
|
peek |
peek()
|
Defined in src/stack/stack.ts:30
|
Get head element of the stack
Returns :
T
T |
pop |
pop()
|
Defined in src/stack/stack.ts:17
|
To get the head element of the stack and remove the head
Returns :
T
T |
push | ||||||
push(item: T)
|
||||||
Defined in src/stack/stack.ts:9
|
||||||
To add item to the stack
Parameters :
Returns :
void
|
isEmpty |
getisEmpty()
|
Defined in src/stack/stack.ts:41
|
Get if the stack is empty or not |
size |
getsize()
|
Defined in src/stack/stack.ts:49
|
Get the size of stack |
import { Errors } from '../errors.enum';
export default class Stack<T> {
store: T[] = [];
/**
* To add item to the stack
* @param item
*/
push(item: T) {
this.store.push(item);
}
/**
* To get the head element of the stack and remove the head
* @returns T
*/
pop(): T {
if (this.isEmpty) {
throw new Error(Errors.STACK_EMPTY);
}
const topItem = this.peek();
this.store.splice(this.size - 1);
return topItem;
}
/**
* Get head element of the stack
* @returns T
*/
peek(): T {
if (this.isEmpty) {
throw new Error(Errors.STACK_EMPTY);
}
return this.store[this.size - 1];
}
/**
* Get if the stack is empty or not
* @returns bool
*/
get isEmpty() {
return this.size === 0;
}
/**
* Get the size of stack
* @returns length
*/
get size() {
return this.store.length;
}
}