typescript-algorithms-and-datastructures
Version:
Useful algorithms and Data structures written in typescript.
48 lines • 1.44 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Stack {
constructor() {
this.stack = [];
}
clear() {
this.stack.length = 0;
}
isEmpty() {
return this.stack.length === 0;
}
search(element) {
const index = this.stack.lastIndexOf(element);
return (index < 0) ? -1 : this.stack.length - 1 - index;
}
peek() {
if (this.isEmpty()) {
throw new Error('The stack is empty');
}
return this.stack[this.stack.length - 1];
}
pop() {
if (this.isEmpty()) {
throw new Error('The stack is empty');
}
return this.stack.pop();
}
push(element) {
this.stack.push(element);
return this;
}
size() {
return this.stack.length;
}
}
exports.Stack = Stack;
});
//# sourceMappingURL=Stack.js.map