diff --git a/README.md b/README.md
index db8ef67..cc1218b 100644
--- a/README.md
+++ b/README.md
@@ -1005,10 +1005,10 @@ avl2.print();
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & pop | 80.37 | 12.44 | 0.00 |
100,000 add & dfs | 36.20 | 27.63 | 0.00 |
10,000 fib add & pop | 362.24 | 2.76 | 0.00 |
doubly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 216.09 | 4.63 | 0.06 |
1,000,000 unshift | 220.68 | 4.53 | 0.02 |
1,000,000 unshift & shift | 172.93 | 5.78 | 0.04 |
1,000,000 insertBefore | 332.25 | 3.01 | 0.08 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 216.09 | 4.63 | 0.06 |
1,000,000 unshift | 220.68 | 4.53 | 0.02 |
1,000,000 unshift & shift | 172.93 | 5.78 | 0.04 |
1,000,000 addBefore | 332.25 | 3.01 | 0.08 |
singly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 222.99 | 4.48 | 0.10 |
10,000 push & pop | 214.82 | 4.66 | 0.01 |
10,000 insertBefore | 251.24 | 3.98 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 222.99 | 4.48 | 0.10 |
10,000 push & pop | 214.82 | 4.66 | 0.01 |
10,000 addBefore | 251.24 | 3.98 | 0.01 |
max-priority-queue
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 8.91 | 112.19 | 1.57e-4 |
diff --git a/README_zh-CN.md b/README_zh-CN.md
index 131d899..97709e0 100644
--- a/README_zh-CN.md
+++ b/README_zh-CN.md
@@ -990,10 +990,10 @@ avl2.print();
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & pop | 90.67 | 11.03 | 0.02 |
100,000 add & dfs | 40.30 | 24.81 | 0.01 |
10,000 fib add & pop | 414.94 | 2.41 | 0.02 |
doubly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 290.62 | 3.44 | 0.10 |
1,000,000 unshift | 253.88 | 3.94 | 0.10 |
1,000,000 unshift & shift | 259.65 | 3.85 | 0.14 |
1,000,000 insertBefore | 463.16 | 2.16 | 0.10 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 290.62 | 3.44 | 0.10 |
1,000,000 unshift | 253.88 | 3.94 | 0.10 |
1,000,000 unshift & shift | 259.65 | 3.85 | 0.14 |
1,000,000 addBefore | 463.16 | 2.16 | 0.10 |
singly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 250.27 | 4.00 | 0.08 |
10,000 push & pop | 261.13 | 3.83 | 0.03 |
10,000 insertBefore | 282.46 | 3.54 | 0.02 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 250.27 | 4.00 | 0.08 |
10,000 push & pop | 261.13 | 3.83 | 0.03 |
10,000 addBefore | 282.46 | 3.54 | 0.02 |
max-priority-queue
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 10.49 | 95.29 | 0.00 |
diff --git a/src/data-structures/base/iterable-base.ts b/src/data-structures/base/iterable-base.ts
index 66ac832..1efc399 100644
--- a/src/data-structures/base/iterable-base.ts
+++ b/src/data-structures/base/iterable-base.ts
@@ -181,6 +181,21 @@ export abstract class IterableEntryBase
{
return accumulator;
}
+ hasValue(value: V): boolean {
+ for (const [, elementValue] of this) {
+ if (elementValue === value) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+ print(): void {
+ console.log([...this])
+ }
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
}
@@ -325,5 +340,14 @@ export abstract class IterableElementBase {
return accumulator;
}
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+ print(): void {
+ console.log([...this])
+ }
+
protected abstract _getIterator(...args: any[]): IterableIterator;
}
diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts
index 60cf520..5f04edc 100644
--- a/src/data-structures/hash/hash-map.ts
+++ b/src/data-structures/hash/hash-map.ts
@@ -35,7 +35,6 @@ export class HashMap extends IterableEntryBase {
const { hashFn } = options;
if (hashFn) {
this._hashFn = hashFn;
-
}
}
if (elements) {
@@ -68,8 +67,7 @@ export class HashMap extends IterableEntryBase {
* @param {V} value - The value parameter represents the value that you want to associate with the
* key in the data structure.
*/
- set(key: K, value: V) {
-
+ set(key: K, value: V): boolean {
if (this._isObjKey(key)) {
if (!this._objMap.has(key)) {
this._size++;
@@ -83,6 +81,7 @@ export class HashMap extends IterableEntryBase {
}
this._store[strKey] = { key, value };
}
+ return true;
}
/**
@@ -90,8 +89,10 @@ export class HashMap extends IterableEntryBase {
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
* key-value pair is represented as an array with two elements: the key and the value.
*/
- setMany(elements: Iterable<[K, V]>) {
- for (const [key, value] of elements) this.set(key, value);
+ setMany(elements: Iterable<[K, V]>): boolean[] {
+ const results: boolean[] = [];
+ for (const [key, value] of elements) results.push(this.set(key, value));
+ return results;
}
/**
@@ -215,6 +216,10 @@ export class HashMap extends IterableEntryBase {
console.log([...this.entries()]);
}
+ put(key: K, value: V): boolean {
+ return this.set(key, value);
+ }
+
/**
* The function returns an iterator that yields key-value pairs from both an object store and an
* object map.
@@ -286,7 +291,6 @@ export class LinkedHashMap extends IterableEntryBase {
this.set(el[0], el[1]);
}
}
-
}
protected _size = 0;
@@ -356,7 +360,7 @@ export class LinkedHashMap extends IterableEntryBase {
* value associated with the key being set in the data structure.
* @returns the size of the data structure after the key-value pair has been set.
*/
- set(key: K, value?: V) {
+ set(key: K, value?: V): boolean {
let node;
const isNewKey = !this.has(key); // Check if the key is new
@@ -398,7 +402,7 @@ export class LinkedHashMap extends IterableEntryBase {
this._size++;
}
- return this._size;
+ return true;
}
has(key: K): boolean {
@@ -411,18 +415,13 @@ export class LinkedHashMap extends IterableEntryBase {
}
}
- hasValue(value: V): boolean {
- for (const [, elementValue] of this) {
- if (elementValue === value) return true;
- }
- return false;
- }
-
- setMany(entries: Iterable<[K, V]>): void {
+ setMany(entries: Iterable<[K, V]>): boolean[] {
+ const results: boolean[] = [];
for (const entry of entries) {
const [key, value] = entry;
- this.set(key, value);
+ results.push(this.set(key, value));
}
+ return results;
}
/**
@@ -461,13 +460,13 @@ export class LinkedHashMap extends IterableEntryBase {
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
* where `K` is the key and `V` is the value.
*/
- getAt(index: number) {
+ getAt(index: number): V | undefined {
rangeCheck(index, 0, this._size - 1);
let node = this._head;
while (index--) {
node = node.next;
}
- return <[K, V]>[node.key, node.value];
+ return node.value;
}
/**
@@ -480,7 +479,7 @@ export class LinkedHashMap extends IterableEntryBase {
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
* was not found.
*/
- delete(key: K) {
+ delete(key: K): boolean {
let node;
if (isWeakKey(key)) {
@@ -521,14 +520,13 @@ export class LinkedHashMap extends IterableEntryBase {
* deleted in the linked list.
* @returns The size of the list after deleting the element at the specified index.
*/
- deleteAt(index: number) {
+ deleteAt(index: number): boolean {
rangeCheck(index, 0, this._size - 1);
let node = this._head;
while (index--) {
node = node.next;
}
- this._deleteNode(node);
- return this._size;
+ return this._deleteNode(node);
}
/**
@@ -539,7 +537,7 @@ export class LinkedHashMap extends IterableEntryBase {
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
* not.
*/
- isEmpty() {
+ isEmpty(): boolean {
return this._size === 0;
}
@@ -549,7 +547,7 @@ export class LinkedHashMap extends IterableEntryBase {
*
* The `clear` function clears all the elements in a data structure and resets its properties.
*/
- clear() {
+ clear(): void {
this._noObjMap = {};
this._size = 0;
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
@@ -629,8 +627,8 @@ export class LinkedHashMap extends IterableEntryBase {
return mappedMap;
}
- print() {
- console.log([...this]);
+ put(key: K, value: V): boolean {
+ return this.set(key, value);
}
/**
@@ -657,7 +655,7 @@ export class LinkedHashMap extends IterableEntryBase {
* represents a node in a linked list. It contains a key-value pair and references to the previous
* and next nodes in the list.
*/
- protected _deleteNode(node: HashMapLinkedNode) {
+ protected _deleteNode(node: HashMapLinkedNode): boolean {
const { prev, next } = node;
prev.next = next;
next.prev = prev;
@@ -671,5 +669,6 @@ export class LinkedHashMap extends IterableEntryBase {
}
this._size -= 1;
+ return true;
}
}
diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts
index 13aed22..c2bd1bf 100644
--- a/src/data-structures/heap/heap.ts
+++ b/src/data-structures/heap/heap.ts
@@ -42,7 +42,7 @@ export class Heap extends IterableElementBase {
if (elements) {
for (const el of elements) {
- this.push(el);
+ this.add(el);
}
// this.fix();
}
@@ -91,26 +91,9 @@ export class Heap extends IterableElementBase {
* Insert an element into the heap and maintain the heap properties.
* @param element - The element to be inserted.
*/
- add(element: E): Heap {
- return this.push(element);
- }
-
- /**
- * Time Complexity: O(log n), where n is the number of elements in the heap.
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(log n), where n is the number of elements in the heap.
- * Space Complexity: O(1)
- *
- * Insert an element into the heap and maintain the heap properties.
- * @param element - The element to be inserted.
- */
- push(element: E): Heap {
+ add(element: E): boolean {
this._elements.push(element);
- this._bubbleUp(this.elements.length - 1);
- return this;
+ return this._bubbleUp(this.elements.length - 1);
}
/**
@@ -136,22 +119,6 @@ export class Heap extends IterableElementBase {
return value;
}
- /**
- * Time Complexity: O(log n), where n is the number of elements in the heap.
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(log n), where n is the number of elements in the heap.
- * Space Complexity: O(1)
- *
- * Remove and return the top element (smallest or largest element) from the heap.
- * @returns The top element or undefined if the heap is empty.
- */
- pop(): E | undefined {
- return this.poll();
- }
-
/**
* Peek at the top element of the heap without removing it.
* @returns The top element or undefined if the heap is empty.
@@ -164,14 +131,14 @@ export class Heap extends IterableElementBase {
* Check if the heap is empty.
* @returns True if the heap is empty, otherwise false.
*/
- isEmpty() {
+ isEmpty(): boolean {
return this.size === 0;
}
/**
* Reset the elements of the heap. Make the elements empty.
*/
- clear() {
+ clear(): void {
this._elements = [];
}
@@ -187,9 +154,9 @@ export class Heap extends IterableElementBase {
* Clear and add elements of the heap
* @param elements
*/
- refill(elements: E[]) {
+ refill(elements: E[]): boolean[] {
this._elements = elements;
- this.fix();
+ return this.fix();
}
/**
@@ -225,11 +192,11 @@ export class Heap extends IterableElementBase {
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
* successfully deleted from the array, and `false` if the element was not found in the array.
*/
- delete(element: E) {
+ delete(element: E): boolean {
const index = this.elements.indexOf(element);
if (index < 0) return false;
if (index === 0) {
- this.pop();
+ this.poll();
} else if (index === this.elements.length - 1) {
this.elements.pop();
} else {
@@ -348,8 +315,10 @@ export class Heap extends IterableElementBase {
*
* Fix the entire heap to maintain heap properties.
*/
- fix() {
- for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
+ fix(): boolean[] {
+ const results: boolean[] = [];
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) results.push(this._sinkDown(i, this.elements.length >> 1));
+ return results;
}
/**
@@ -378,7 +347,7 @@ export class Heap extends IterableElementBase {
let index = 0;
for (const current of this) {
if (callback.call(thisArg, current, index, this)) {
- filteredList.push(current);
+ filteredList.add(current);
}
index++;
}
@@ -421,16 +390,7 @@ export class Heap extends IterableElementBase {
return mappedHeap;
}
- /**
- * Time Complexity: O(log n)
- * Space Complexity: O(1)
- */
-
- print(): void {
- console.log([...this]);
- }
-
- protected* _getIterator() {
+ protected* _getIterator(): IterableIterator {
for (const element of this.elements) {
yield element;
}
@@ -448,7 +408,7 @@ export class Heap extends IterableElementBase {
* Float operation to maintain heap properties after adding an element.
* @param index - The index of the newly added element.
*/
- protected _bubbleUp(index: number) {
+ protected _bubbleUp(index: number): boolean {
const element = this.elements[index];
while (index > 0) {
const parent = (index - 1) >> 1;
@@ -458,6 +418,7 @@ export class Heap extends IterableElementBase {
index = parent;
}
this.elements[index] = element;
+ return true;
}
/**
@@ -468,7 +429,7 @@ export class Heap extends IterableElementBase {
* @param index - The index from which to start sinking.
* @param halfLength
*/
- protected _sinkDown(index: number, halfLength: number) {
+ protected _sinkDown(index: number, halfLength: number): boolean {
const element = this.elements[index];
while (index < halfLength) {
let left = index << 1 | 1;
@@ -486,6 +447,7 @@ export class Heap extends IterableElementBase {
index = left;
}
this.elements[index] = element;
+ return true;
}
}
diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts
index ed187de..62094a4 100644
--- a/src/data-structures/linked-list/doubly-linked-list.ts
+++ b/src/data-structures/linked-list/doubly-linked-list.ts
@@ -33,13 +33,13 @@ export class DoublyLinkedListNode {
*/
export class DoublyLinkedList extends IterableElementBase {
/**
- * The constructor initializes the linked list with an empty head, tail, and length.
+ * The constructor initializes the linked list with an empty head, tail, and size.
*/
constructor(elements?: Iterable) {
super();
this._head = undefined;
this._tail = undefined;
- this._length = 0;
+ this._size = 0;
if (elements) {
for (const el of elements) {
this.push(el);
@@ -59,23 +59,19 @@ export class DoublyLinkedList extends IterableElementBase {
return this._tail;
}
- protected _length: number;
-
- get length(): number {
- return this._length;
- }
+ protected _size: number;
get size(): number {
- return this.length;
+ return this._size;
}
/**
- * Time Complexity: O(n), where n is the length of the input array.
+ * Time Complexity: O(n), where n is the size of the input array.
* Space Complexity: O(n)
*/
/**
- * Time Complexity: O(n), where n is the length of the input array.
+ * Time Complexity: O(n), where n is the size of the input array.
* Space Complexity: O(n)
*
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
@@ -103,7 +99,7 @@ export class DoublyLinkedList extends IterableElementBase {
* The push function adds a new node with the given value to the end of the doubly linked list.
* @param {E} value - The value to be added to the linked list.
*/
- push(value: E): void {
+ push(value: E): boolean {
const newNode = new DoublyLinkedListNode(value);
if (!this.head) {
this._head = newNode;
@@ -113,23 +109,8 @@ export class DoublyLinkedList extends IterableElementBase {
this.tail!.next = newNode;
this._tail = newNode;
}
- this._length++;
- }
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- *
- * The addLast function adds a new node with the given value to the end of the doubly linked list.
- * @param {E} value - The value to be added to the linked list.
- */
- addLast(value: E): void {
- this.push(value);
+ this._size++;
+ return true;
}
/**
@@ -155,27 +136,10 @@ export class DoublyLinkedList extends IterableElementBase {
this._tail = removedNode.prev;
this.tail!.next = undefined;
}
- this._length--;
+ this._size--;
return removedNode.value;
}
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- *
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
- * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
- * list is empty, it returns undefined.
- */
- pollLast(): E | undefined {
- return this.pop();
- }
-
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@@ -199,27 +163,10 @@ export class DoublyLinkedList extends IterableElementBase {
this._head = removedNode.next;
this.head!.prev = undefined;
}
- this._length--;
+ this._size--;
return removedNode.value;
}
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- *
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
- * list.
- */
- pollFirst(): E | undefined {
- return this.shift();
- }
-
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@@ -233,7 +180,7 @@ export class DoublyLinkedList extends IterableElementBase {
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
* doubly linked list.
*/
- unshift(value: E): void {
+ unshift(value: E): boolean {
const newNode = new DoublyLinkedListNode(value);
if (!this.head) {
this._head = newNode;
@@ -243,56 +190,8 @@ export class DoublyLinkedList extends IterableElementBase {
this.head!.prev = newNode;
this._head = newNode;
}
- this._length++;
- }
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(1)
- *
- * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
- * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
- * doubly linked list.
- */
- addFirst(value: E): void {
- this.unshift(value);
- }
-
- /**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(1)
- *
- * The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
- * @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
- */
- getFirst(): E | undefined {
- return this.head?.value;
- }
-
- /**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(1)
- */
-
- /**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(1)
- *
- * The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
- * @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
- */
- getLast(): E | undefined {
- return this.tail?.value;
+ this._size++;
+ return true;
}
/**
@@ -311,7 +210,7 @@ export class DoublyLinkedList extends IterableElementBase {
* or the linked list is empty, it will return undefined.
*/
getAt(index: number): E | undefined {
- if (index < 0 || index >= this.length) return undefined;
+ if (index < 0 || index >= this.size) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
@@ -336,7 +235,7 @@ export class DoublyLinkedList extends IterableElementBase {
* valid range of the linked list, otherwise it returns `undefined`.
*/
getNodeAt(index: number): DoublyLinkedListNode | undefined {
- if (index < 0 || index >= this.length) return undefined;
+ if (index < 0 || index >= this.size) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
@@ -389,13 +288,13 @@ export class DoublyLinkedList extends IterableElementBase {
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
* if the index is out of bounds.
*/
- insertAt(index: number, value: E): boolean {
- if (index < 0 || index > this.length) return false;
+ addAt(index: number, value: E): boolean {
+ if (index < 0 || index > this.size) return false;
if (index === 0) {
this.unshift(value);
return true;
}
- if (index === this.length) {
+ if (index === this.size) {
this.push(value);
return true;
}
@@ -407,7 +306,7 @@ export class DoublyLinkedList extends IterableElementBase {
newNode.next = nextNode;
prevNode!.next = newNode;
nextNode!.prev = newNode;
- this._length++;
+ this._size++;
return true;
}
@@ -420,7 +319,7 @@ export class DoublyLinkedList extends IterableElementBase {
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
*
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
+ * The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
* @param {E | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
@@ -429,7 +328,7 @@ export class DoublyLinkedList extends IterableElementBase {
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
* insertion fails.
*/
- insertBefore(existingValueOrNode: E | DoublyLinkedListNode, newValue: E): boolean {
+ addBefore(existingValueOrNode: E | DoublyLinkedListNode, newValue: E): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode) {
@@ -449,7 +348,7 @@ export class DoublyLinkedList extends IterableElementBase {
if (existingNode === this.head) {
this._head = newNode;
}
- this._length++;
+ this._size++;
return true;
}
@@ -465,7 +364,7 @@ export class DoublyLinkedList extends IterableElementBase {
* Time Complexity: O(n), where n is the number of elements in the linked list.
* Space Complexity: O(1)
*
- * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
+ * The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
* @param {E | DoublyLinkedListNode} existingValueOrNode - The existing value or node in the doubly linked list
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
@@ -473,7 +372,7 @@ export class DoublyLinkedList extends IterableElementBase {
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
* existing value or node is not found in the doubly linked list.
*/
- insertAfter(existingValueOrNode: E | DoublyLinkedListNode, newValue: E): boolean {
+ addAfter(existingValueOrNode: E | DoublyLinkedListNode, newValue: E): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode) {
@@ -493,7 +392,7 @@ export class DoublyLinkedList extends IterableElementBase {
if (existingNode === this.tail) {
this._tail = newNode;
}
- this._length++;
+ this._size++;
return true;
}
@@ -515,18 +414,24 @@ export class DoublyLinkedList extends IterableElementBase {
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
* bounds.
*/
- deleteAt(index: number): E | undefined {
- if (index < 0 || index >= this.length) return undefined;
- if (index === 0) return this.shift();
- if (index === this.length - 1) return this.pop();
+ deleteAt(index: number): boolean {
+ if (index < 0 || index >= this.size) return false;
+ if (index === 0) {
+ this.shift();
+ return true;
+ }
+ if (index === this.size - 1) {
+ this.pop();
+ return true;
+ }
const removedNode = this.getNodeAt(index);
const prevNode = removedNode!.prev;
const nextNode = removedNode!.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
- this._length--;
- return removedNode!.value;
+ this._size--;
+ return true;
}
/**
@@ -563,7 +468,7 @@ export class DoublyLinkedList extends IterableElementBase {
const nextNode = node.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
- this._length--;
+ this._size--;
}
return true;
}
@@ -571,20 +476,20 @@ export class DoublyLinkedList extends IterableElementBase {
}
/**
- * The function checks if a variable has a length greater than zero and returns a boolean value.
+ * The function checks if a variable has a size greater than zero and returns a boolean value.
* @returns A boolean value is being returned.
*/
isEmpty(): boolean {
- return this.length === 0;
+ return this.size === 0;
}
/**
- * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
+ * The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
*/
clear(): void {
this._head = undefined;
this._tail = undefined;
- this._length = 0;
+ this._size = 0;
}
/**
@@ -679,7 +584,7 @@ export class DoublyLinkedList extends IterableElementBase {
*
* The `reverse` function reverses the order of the elements in a doubly linked list.
*/
- reverse(): void {
+ reverse(): this {
let current = this.head;
[this._head, this._tail] = [this.tail, this.head];
while (current) {
@@ -687,6 +592,7 @@ export class DoublyLinkedList extends IterableElementBase {
[current.prev, current.next] = [current.next, current.prev];
current = next;
}
+ return this;
}
/**
@@ -801,12 +707,102 @@ export class DoublyLinkedList extends IterableElementBase {
}
/**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(n)
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
- print(): void {
- console.log([...this]);
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
+ * The addLast function adds a new node with the given value to the end of the doubly linked list.
+ * @param {E} value - The value to be added to the linked list.
+ */
+ addLast(value: E): boolean {
+ return this.push(value);
+ }
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
+ * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
+ * list is empty, it returns undefined.
+ */
+ pollLast(): E | undefined {
+ return this.pop();
+ }
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
+ * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
+ * list.
+ */
+ pollFirst(): E | undefined {
+ return this.shift();
+ }
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
+ * The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
+ * @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
+ * doubly linked list.
+ */
+ addFirst(value: E): void {
+ this.unshift(value);
+ }
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
+ * The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
+ * @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
+ */
+ get first(): E | undefined {
+ return this.head?.value;
+ }
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
+ * Space Complexity: O(1)
+ *
+ * The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
+ * @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
+ */
+ get last(): E | undefined {
+ return this.tail?.value;
}
/**
diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts
index d585714..041b407 100644
--- a/src/data-structures/linked-list/singly-linked-list.ts
+++ b/src/data-structures/linked-list/singly-linked-list.ts
@@ -31,7 +31,7 @@ export class SinglyLinkedList extends IterableElementBase {
super();
this._head = undefined;
this._tail = undefined;
- this._length = 0;
+ this._size = 0;
if (elements) {
for (const el of elements)
this.push(el);
@@ -50,10 +50,10 @@ export class SinglyLinkedList extends IterableElementBase {
return this._tail;
}
- protected _length: number;
+ protected _size: number;
- get length(): number {
- return this._length;
+ get size(): number {
+ return this._size;
}
/**
@@ -91,7 +91,7 @@ export class SinglyLinkedList extends IterableElementBase {
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
*/
- push(value: E): void {
+ push(value: E): boolean {
const newNode = new SinglyLinkedListNode(value);
if (!this.head) {
this._head = newNode;
@@ -100,7 +100,8 @@ export class SinglyLinkedList extends IterableElementBase {
this.tail!.next = newNode;
this._tail = newNode;
}
- this._length++;
+ this._size++;
+ return true;
}
/**
@@ -116,8 +117,8 @@ export class SinglyLinkedList extends IterableElementBase {
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
* any type (E) as specified in the generic type declaration of the class or function.
*/
- addLast(value: E): void {
- this.push(value);
+ addLast(value: E): boolean {
+ return this.push(value);
}
/**
@@ -140,7 +141,7 @@ export class SinglyLinkedList extends IterableElementBase {
const value = this.head.value;
this._head = undefined;
this._tail = undefined;
- this._length--;
+ this._size--;
return value;
}
@@ -151,7 +152,7 @@ export class SinglyLinkedList extends IterableElementBase {
const value = this.tail!.value;
current.next = undefined;
this._tail = current;
- this._length--;
+ this._size--;
return value;
}
@@ -189,7 +190,7 @@ export class SinglyLinkedList extends IterableElementBase {
if (!this.head) return undefined;
const removedNode = this.head;
this._head = this.head.next;
- this._length--;
+ this._size--;
return removedNode.value;
}
@@ -222,7 +223,7 @@ export class SinglyLinkedList extends IterableElementBase {
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
* linked list.
*/
- unshift(value: E): void {
+ unshift(value: E): boolean {
const newNode = new SinglyLinkedListNode(value);
if (!this.head) {
this._head = newNode;
@@ -231,7 +232,8 @@ export class SinglyLinkedList extends IterableElementBase {
newNode.next = this.head;
this._head = newNode;
}
- this._length++;
+ this._size++;
+ return true;
}
/**
@@ -247,8 +249,8 @@ export class SinglyLinkedList extends IterableElementBase {
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
* linked list.
*/
- addFirst(value: E): void {
- this.unshift(value);
+ addFirst(value: E): boolean {
+ return this.unshift(value);
}
/**
@@ -267,7 +269,7 @@ export class SinglyLinkedList extends IterableElementBase {
* `undefined` if the index is out of bounds.
*/
getAt(index: number): E | undefined {
- if (index < 0 || index >= this.length) return undefined;
+ if (index < 0 || index >= this.size) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
@@ -313,16 +315,22 @@ export class SinglyLinkedList extends IterableElementBase {
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
* bounds.
*/
- deleteAt(index: number): E | undefined {
- if (index < 0 || index >= this.length) return undefined;
- if (index === 0) return this.shift();
- if (index === this.length - 1) return this.pop();
+ deleteAt(index: number): boolean {
+ if (index < 0 || index >= this.size) return false;
+ if (index === 0) {
+ this.shift();
+ return true;
+ }
+ if (index === this.size - 1) {
+ this.pop();
+ return true;
+ }
const prevNode = this.getNodeAt(index - 1);
const removedNode = prevNode!.next;
prevNode!.next = removedNode!.next;
- this._length--;
- return removedNode!.value;
+ this._size--;
+ return true;
}
/**
@@ -340,7 +348,7 @@ export class SinglyLinkedList extends IterableElementBase {
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
*/
- delete(valueOrNode: E | SinglyLinkedListNode | undefined | undefined): boolean {
+ delete(valueOrNode: E | SinglyLinkedListNode | undefined ): boolean {
if (!valueOrNode) return false;
let value: E;
if (valueOrNode instanceof SinglyLinkedListNode) {
@@ -364,7 +372,7 @@ export class SinglyLinkedList extends IterableElementBase {
this._tail = prev;
}
}
- this._length--;
+ this._size--;
return true;
}
prev = current;
@@ -383,7 +391,7 @@ export class SinglyLinkedList extends IterableElementBase {
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
* Space Complexity: O(1) - Constant space.
*
- * The `insertAt` function inserts a value at a specified index in a singly linked list.
+ * The `addAt` function inserts a value at a specified index in a singly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
* linked list. It is of type number.
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
@@ -391,13 +399,13 @@ export class SinglyLinkedList extends IterableElementBase {
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
* if the index is out of bounds.
*/
- insertAt(index: number, value: E): boolean {
- if (index < 0 || index > this.length) return false;
+ addAt(index: number, value: E): boolean {
+ if (index < 0 || index > this.size) return false;
if (index === 0) {
this.unshift(value);
return true;
}
- if (index === this.length) {
+ if (index === this.size) {
this.push(value);
return true;
}
@@ -406,7 +414,7 @@ export class SinglyLinkedList extends IterableElementBase {
const prevNode = this.getNodeAt(index - 1);
newNode.next = prevNode!.next;
prevNode!.next = newNode;
- this._length++;
+ this._size++;
return true;
}
@@ -416,7 +424,7 @@ export class SinglyLinkedList extends IterableElementBase {
* @returns A boolean value indicating whether the length of the object is equal to 0.
*/
isEmpty(): boolean {
- return this.length === 0;
+ return this.size === 0;
}
/**
@@ -425,7 +433,7 @@ export class SinglyLinkedList extends IterableElementBase {
clear(): void {
this._head = undefined;
this._tail = undefined;
- this._length = 0;
+ this._size = 0;
}
/**
@@ -462,8 +470,8 @@ export class SinglyLinkedList extends IterableElementBase {
* The `reverse` function reverses the order of the nodes in a singly linked list.
* @returns The reverse() method does not return anything. It has a return type of void.
*/
- reverse(): void {
- if (!this.head || this.head === this.tail) return;
+ reverse(): this {
+ if (!this.head || this.head === this.tail) return this;
let prev: SinglyLinkedListNode | undefined = undefined;
let current: SinglyLinkedListNode | undefined = this.head;
@@ -477,6 +485,7 @@ export class SinglyLinkedList extends IterableElementBase {
}
[this._head, this._tail] = [this.tail!, this.head!];
+ return this;
}
/**
@@ -571,14 +580,14 @@ export class SinglyLinkedList extends IterableElementBase {
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
*
- * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
+ * The `addBefore` function inserts a new value before an existing value in a singly linked list.
* @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node that you want to insert the
* new value before. It can be either the value itself or a node containing the value in the linked list.
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
- * @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
+ * @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
* inserted before the existing value, and `false` otherwise.
*/
- insertBefore(existingValueOrNode: E | SinglyLinkedListNode, newValue: E): boolean {
+ addBefore(existingValueOrNode: E | SinglyLinkedListNode, newValue: E): boolean {
if (!this.head) return false;
let existingValue: E;
@@ -598,7 +607,7 @@ export class SinglyLinkedList extends IterableElementBase {
const newNode = new SinglyLinkedListNode(newValue);
newNode.next = current.next;
current.next = newNode;
- this._length++;
+ this._size++;
return true;
}
current = current.next;
@@ -616,14 +625,14 @@ export class SinglyLinkedList extends IterableElementBase {
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
* Space Complexity: O(1) - Constant space.
*
- * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
+ * The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
* @param {E | SinglyLinkedListNode} existingValueOrNode - The existing value or node in the linked list after which
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
* existing value or node, and false if the existing value or node was not found in the linked list.
*/
- insertAfter(existingValueOrNode: E | SinglyLinkedListNode, newValue: E): boolean {
+ addAfter(existingValueOrNode: E | SinglyLinkedListNode, newValue: E): boolean {
let existingNode: E | SinglyLinkedListNode | undefined;
if (existingValueOrNode instanceof SinglyLinkedListNode) {
@@ -639,7 +648,7 @@ export class SinglyLinkedList extends IterableElementBase {
if (existingNode === this.tail) {
this._tail = newNode;
}
- this._length++;
+ this._size++;
return true;
}
@@ -737,15 +746,6 @@ export class SinglyLinkedList extends IterableElementBase {
return mappedList;
}
- /**
- * Time Complexity: O(n), where n is the number of elements in the linked list.
- * Space Complexity: O(n)
- */
-
- print(): void {
- console.log([...this]);
- }
-
protected* _getIterator(): IterableIterator {
let current = this.head;
diff --git a/src/data-structures/linked-list/skip-linked-list.ts b/src/data-structures/linked-list/skip-linked-list.ts
index 9631f02..360fcb9 100644
--- a/src/data-structures/linked-list/skip-linked-list.ts
+++ b/src/data-structures/linked-list/skip-linked-list.ts
@@ -193,7 +193,7 @@ export class SkipList {
* Get the value of the first element (the smallest element) in the Skip List.
* @returns The value of the first element, or undefined if the Skip List is empty.
*/
- getFirst(): V | undefined {
+ get first(): V | undefined {
const firstNode = this.head.forward[0];
return firstNode ? firstNode.value : undefined;
}
@@ -210,7 +210,7 @@ export class SkipList {
* Get the value of the last element (the largest element) in the Skip List.
* @returns The value of the last element, or undefined if the Skip List is empty.
*/
- getLast(): V | undefined {
+ get last(): V | undefined {
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i]) {
diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts
index 91f603a..c6f690c 100644
--- a/src/data-structures/queue/deque.ts
+++ b/src/data-structures/queue/deque.ts
@@ -83,106 +83,6 @@ export class Deque extends IterableElementBase {
return this._buckets[this._bucketLast][this._lastInBucket];
}
- /**
- * Time Complexity: O(1) - Removes the last element.
- * Space Complexity: O(1) - Operates in-place.
- */
-
- isEmpty() {
- return this.size === 0;
- }
-
- /**
- * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
- * Space Complexity: O(n) - Due to potential resizing.
- */
-
- /**
- * Time Complexity: O(1)
- * Space Complexity: O(n) - In worst case, resizing doubles the array size.
- *
- * The addLast function adds an element to the end of an array.
- * @param {E} element - The element parameter represents the element that you want to add to the end of the
- * data structure.
- */
- addLast(element: E): void {
- this.push(element);
- }
-
- /**
- * Time Complexity: O(1) - Removes the first element.
- * Space Complexity: O(1) - In-place operation.
- */
-
- /**
- * Time Complexity: O(1) - Removes the last element.
- * Space Complexity: O(1) - Operates in-place.
- *
- * The function "pollLast" removes and returns the last element of an array.
- * @returns The last element of the array is being returned.
- */
- pollLast(): E | undefined {
- return this.pop();
- }
-
- /**
- * Time Complexity: O(1).
- * Space Complexity: O(n) - Due to potential resizing.
- *
- * The "addFirst" function adds an element to the beginning of an array.
- * @param {E} element - The parameter "element" represents the element that you want to add to the
- * beginning of the data structure.
- */
- addFirst(element: E): void {
- this.unshift(element);
- }
-
- /**
- * Time Complexity: O(1) - Removes the first element.
- * Space Complexity: O(1) - In-place operation.
- *
- * The function "pollFirst" removes and returns the first element of an array.
- * @returns The method `pollFirst()` is returning the first element of the array after removing it
- * from the beginning. If the array is empty, it will return `undefined`.
- */
- pollFirst(): E | undefined {
- return this.shift();
- }
-
- /**
- * The clear() function resets the state of the object by initializing all variables to their default
- * values.
- */
- clear() {
- this._buckets = [new Array(this._bucketSize)];
- this._bucketCount = 1;
- this._bucketFirst = this._bucketLast = this._size = 0;
- this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
- }
-
- /**
- * The below function is a generator that yields elements from a collection one by one.
- */
- * begin(): Generator {
- let index = 0;
- while (index < this.size) {
- yield this.getAt(index);
- index++;
- }
- }
-
- /**
- * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
- * the last element.
- */
- * reverseBegin(): Generator {
- let index = this.size - 1;
- while (index >= 0) {
- yield this.getAt(index);
- index--;
- }
- }
-
/**
* Time Complexity - Amortized O(1) (possible reallocation)
* Space Complexity - O(n) (due to potential resizing).
@@ -197,7 +97,7 @@ export class Deque extends IterableElementBase {
* structure.
* @returns The size of the data structure after the element has been pushed.
*/
- push(element: E) {
+ push(element: E): boolean {
if (this.size) {
if (this._lastInBucket < this._bucketSize - 1) {
this._lastInBucket += 1;
@@ -215,7 +115,7 @@ export class Deque extends IterableElementBase {
}
this._size += 1;
this._buckets[this._bucketLast][this._lastInBucket] = element;
- return this.size;
+ return true;
}
/**
@@ -231,7 +131,7 @@ export class Deque extends IterableElementBase {
* internal state variables accordingly.
* @returns The element that was removed from the data structure is being returned.
*/
- pop() {
+ pop(): E | undefined {
if (this.size === 0) return;
const element = this._buckets[this._bucketLast][this._lastInBucket];
if (this.size !== 1) {
@@ -264,7 +164,7 @@ export class Deque extends IterableElementBase {
* beginning of the data structure.
* @returns The size of the data structure after the element has been added.
*/
- unshift(element: E) {
+ unshift(element: E): boolean {
if (this.size) {
if (this._firstInBucket > 0) {
this._firstInBucket -= 1;
@@ -282,10 +182,9 @@ export class Deque extends IterableElementBase {
}
this._size += 1;
this._buckets[this._bucketFirst][this._firstInBucket] = element;
- return this.size;
+ return true;
}
-
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
@@ -300,7 +199,7 @@ export class Deque extends IterableElementBase {
* @returns The element that is being removed from the beginning of the data structure is being
* returned.
*/
- shift() {
+ shift(): E | undefined {
if (this.size === 0) return;
const element = this._buckets[this._bucketFirst][this._firstInBucket];
if (this.size !== 1) {
@@ -318,6 +217,49 @@ export class Deque extends IterableElementBase {
return element;
}
+ /**
+ * Time Complexity: O(1) - Removes the last element.
+ * Space Complexity: O(1) - Operates in-place.
+ */
+
+ isEmpty(): boolean {
+ return this.size === 0;
+ }
+
+ /**
+ * The clear() function resets the state of the object by initializing all variables to their default
+ * values.
+ */
+ clear(): void {
+ this._buckets = [new Array(this._bucketSize)];
+ this._bucketCount = 1;
+ this._bucketFirst = this._bucketLast = this._size = 0;
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
+ }
+
+ /**
+ * The below function is a generator that yields elements from a collection one by one.
+ */
+ * begin(): Generator {
+ let index = 0;
+ while (index < this.size) {
+ yield this.getAt(index);
+ index++;
+ }
+ }
+
+ /**
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
+ * the last element.
+ */
+ * reverseBegin(): Generator {
+ let index = this.size - 1;
+ while (index >= 0) {
+ yield this.getAt(index);
+ index--;
+ }
+ }
+
/**
* Time Complexity: O(1)
@@ -359,13 +301,14 @@ export class Deque extends IterableElementBase {
* @param {E} element - The `element` parameter is the value that you want to set at the specified
* position in the data structure.
*/
- setAt(pos: number, element: E) {
+ setAt(pos: number, element: E): boolean {
rangeCheck(pos, 0, this.size - 1);
const {
bucketIndex,
indexInBucket
} = this._getBucketAndPosition(pos);
this._buckets[bucketIndex][indexInBucket] = element;
+ return true;
}
/**
@@ -377,7 +320,7 @@ export class Deque extends IterableElementBase {
* Time Complexity: O(n)
* Space Complexity: O(n)
*
- * The `insertAt` function inserts one or more elements at a specified position in an array-like data
+ * The `addAt` function inserts one or more elements at a specified position in an array-like data
* structure.
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
* be inserted. It is of type `number`.
@@ -388,7 +331,7 @@ export class Deque extends IterableElementBase {
* will be inserted once. However, you can provide a different value for `num` if you want
* @returns The size of the array after the insertion is being returned.
*/
- insertAt(pos: number, element: E, num = 1) {
+ addAt(pos: number, element: E, num = 1): boolean {
const length = this.size;
rangeCheck(pos, 0, length);
if (pos === 0) {
@@ -404,7 +347,7 @@ export class Deque extends IterableElementBase {
for (let i = 0; i < num; ++i) this.push(element);
for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
}
- return this.size;
+ return true;
}
/**
@@ -422,7 +365,7 @@ export class Deque extends IterableElementBase {
* cut. It is a number that indicates the index of the character where the cut should be made.
* @returns The method is returning the updated size of the data structure.
*/
- cut(pos: number) {
+ cut(pos: number): number {
if (pos < 0) {
this.clear();
return 0;
@@ -453,7 +396,7 @@ export class Deque extends IterableElementBase {
* the index of the element to be deleted.
* @returns The size of the data structure after the deletion operation is performed.
*/
- deleteAt(pos: number) {
+ deleteAt(pos: number): boolean {
rangeCheck(pos, 0, this.size - 1);
if (pos === 0) this.shift();
else if (pos === this.size - 1) this.pop();
@@ -474,7 +417,7 @@ export class Deque extends IterableElementBase {
}
this.pop();
}
- return this.size;
+ return true;
}
/**
@@ -492,9 +435,9 @@ export class Deque extends IterableElementBase {
* the data structure.
* @returns The size of the data structure after the element has been deleted.
*/
- delete(element: E) {
+ delete(element: E): boolean {
const size = this.size;
- if (size === 0) return 0;
+ if (size === 0) return false;
let i = 0;
let index = 0;
while (i < size) {
@@ -506,7 +449,7 @@ export class Deque extends IterableElementBase {
i += 1;
}
this.cut(index - 1);
- return this.size;
+ return true;
}
/**
@@ -523,7 +466,7 @@ export class Deque extends IterableElementBase {
* @returns The reverse() method is returning the object itself (this) after performing the reverse
* operation on the buckets and updating the relevant properties.
*/
- reverse() {
+ reverse(): this {
this._buckets.reverse().forEach(function (bucket) {
bucket.reverse();
});
@@ -548,9 +491,9 @@ export class Deque extends IterableElementBase {
* the number of unique elements.
* @returns The size of the modified array is being returned.
*/
- unique() {
+ unique(): this {
if (this.size <= 1) {
- return this.size;
+ return this;
}
let index = 1;
let prev = this.getAt(0);
@@ -562,7 +505,7 @@ export class Deque extends IterableElementBase {
}
}
this.cut(index - 1);
- return this.size;
+ return this;
}
/**
@@ -578,9 +521,9 @@ export class Deque extends IterableElementBase {
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
* the elements in the sorted array.
- * @returns The method is returning the sorted instance of the object on which the method is called.
+ * @returns Deque
*/
- sort(comparator?: (x: E, y: E) => number) {
+ sort(comparator?: (x: E, y: E) => number): this {
const arr: E[] = [];
for (let i = 0; i < this.size; ++i) {
arr.push(this.getAt(i));
@@ -606,7 +549,7 @@ export class Deque extends IterableElementBase {
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
* `this.size` is 0, but it does not return any value.
*/
- shrinkToFit() {
+ shrinkToFit(): void {
if (this.size === 0) return;
const newBuckets = [];
if (this._bucketFirst === this._bucketLast) return;
@@ -650,7 +593,7 @@ export class Deque extends IterableElementBase {
return element;
}
}
- return undefined;
+ return;
}
/**
@@ -691,11 +634,7 @@ export class Deque extends IterableElementBase {
* @returns The `toArray()` method is returning an array of elements of type `E`.
*/
toArray(): E[] {
- const arr: E[] = [];
- for (let i = 0; i < this.size; ++i) {
- arr.push(this.getAt(i));
- }
- return arr;
+ return [...this];
}
/**
@@ -758,12 +697,60 @@ export class Deque extends IterableElementBase {
}
/**
- * Time Complexity: O(n)
- * Space Complexity: O(n)
+ * Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
+ * Space Complexity: O(n) - Due to potential resizing.
*/
- print(): void {
- console.log([...this])
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(n) - In worst case, resizing doubles the array size.
+ *
+ * The addLast function adds an element to the end of an array.
+ * @param {E} element - The element parameter represents the element that you want to add to the end of the
+ * data structure.
+ */
+ addLast(element: E): boolean {
+ return this.push(element);
+ }
+
+ /**
+ * Time Complexity: O(1) - Removes the first element.
+ * Space Complexity: O(1) - In-place operation.
+ */
+
+ /**
+ * Time Complexity: O(1) - Removes the last element.
+ * Space Complexity: O(1) - Operates in-place.
+ *
+ * The function "pollLast" removes and returns the last element of an array.
+ * @returns The last element of the array is being returned.
+ */
+ pollLast(): E | undefined {
+ return this.pop();
+ }
+
+ /**
+ * Time Complexity: O(1).
+ * Space Complexity: O(n) - Due to potential resizing.
+ *
+ * The "addFirst" function adds an element to the beginning of an array.
+ * @param {E} element - The parameter "element" represents the element that you want to add to the
+ * beginning of the data structure.
+ */
+ addFirst(element: E): boolean {
+ return this.unshift(element);
+ }
+
+ /**
+ * Time Complexity: O(1) - Removes the first element.
+ * Space Complexity: O(1) - In-place operation.
+ *
+ * The function "pollFirst" removes and returns the first element of an array.
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
+ * from the beginning. If the array is empty, it will return `undefined`.
+ */
+ pollFirst(): E | undefined {
+ return this.shift();
}
/**
@@ -773,7 +760,7 @@ export class Deque extends IterableElementBase {
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
* object to be iterated over using a for...of loop.
*/
- protected* _getIterator() {
+ protected* _getIterator(): IterableIterator {
for (let i = 0; i < this.size; ++i) {
yield this.getAt(i);
}
diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts
index 7e9b664..c0cb9d5 100644
--- a/src/data-structures/queue/queue.ts
+++ b/src/data-structures/queue/queue.ts
@@ -74,9 +74,9 @@ export class Queue extends IterableElementBase