linked-list-typed 1.19.3 → 1.19.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -204
- package/dist/index.d.ts +8 -2
- package/dist/index.js +13 -16
- package/package.json +2 -2
- package/tsconfig.json +6 -4
- package/dist/doubly-linked-list.d.ts +0 -195
- package/dist/doubly-linked-list.js +0 -591
- package/dist/singly-linked-list.d.ts +0 -156
- package/dist/singly-linked-list.js +0 -502
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export declare class SinglyLinkedListNode<T = number> {
|
|
9
|
-
/**
|
|
10
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
|
-
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
|
12
|
-
* will be stored in the node of a linked list.
|
|
13
|
-
*/
|
|
14
|
-
constructor(val: T);
|
|
15
|
-
private _val;
|
|
16
|
-
get val(): T;
|
|
17
|
-
set val(value: T);
|
|
18
|
-
private _next;
|
|
19
|
-
get next(): SinglyLinkedListNode<T> | null;
|
|
20
|
-
set next(value: SinglyLinkedListNode<T> | null);
|
|
21
|
-
}
|
|
22
|
-
export declare class SinglyLinkedList<T = number> {
|
|
23
|
-
/**
|
|
24
|
-
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
25
|
-
*/
|
|
26
|
-
constructor();
|
|
27
|
-
private _head;
|
|
28
|
-
get head(): SinglyLinkedListNode<T> | null;
|
|
29
|
-
set head(value: SinglyLinkedListNode<T> | null);
|
|
30
|
-
private _tail;
|
|
31
|
-
get tail(): SinglyLinkedListNode<T> | null;
|
|
32
|
-
set tail(value: SinglyLinkedListNode<T> | null);
|
|
33
|
-
private _length;
|
|
34
|
-
get length(): number;
|
|
35
|
-
/**
|
|
36
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
37
|
-
* array.
|
|
38
|
-
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
39
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
40
|
-
*/
|
|
41
|
-
static fromArray<T>(data: T[]): SinglyLinkedList<T>;
|
|
42
|
-
getLength(): number;
|
|
43
|
-
/**
|
|
44
|
-
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
45
|
-
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
46
|
-
* any type (T) as specified in the generic type declaration of the class or function.
|
|
47
|
-
*/
|
|
48
|
-
push(data: T): void;
|
|
49
|
-
/**
|
|
50
|
-
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
51
|
-
* pointers accordingly.
|
|
52
|
-
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
53
|
-
* the linked list is empty, it returns `null`.
|
|
54
|
-
*/
|
|
55
|
-
pop(): T | null;
|
|
56
|
-
/**
|
|
57
|
-
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
58
|
-
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
59
|
-
*/
|
|
60
|
-
shift(): T | null;
|
|
61
|
-
/**
|
|
62
|
-
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
63
|
-
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
64
|
-
* linked list.
|
|
65
|
-
*/
|
|
66
|
-
unshift(val: T): void;
|
|
67
|
-
/**
|
|
68
|
-
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
69
|
-
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
70
|
-
* retrieve from the list.
|
|
71
|
-
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
|
72
|
-
* `null` if the index is out of bounds.
|
|
73
|
-
*/
|
|
74
|
-
getAt(index: number): T | null;
|
|
75
|
-
/**
|
|
76
|
-
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
77
|
-
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
78
|
-
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
79
|
-
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
|
80
|
-
* specified index exists, or `null` if the index is out of bounds.
|
|
81
|
-
*/
|
|
82
|
-
getNodeAt(index: number): SinglyLinkedListNode<T> | null;
|
|
83
|
-
/**
|
|
84
|
-
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
85
|
-
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
86
|
-
* data structure. It is of type number.
|
|
87
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
88
|
-
* bounds.
|
|
89
|
-
*/
|
|
90
|
-
deleteAt(index: number): T | null;
|
|
91
|
-
delete(valueOrNode: T): boolean;
|
|
92
|
-
delete(valueOrNode: SinglyLinkedListNode<T>): boolean;
|
|
93
|
-
/**
|
|
94
|
-
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
95
|
-
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
96
|
-
* linked list. It is of type number.
|
|
97
|
-
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
|
98
|
-
* specified index.
|
|
99
|
-
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
100
|
-
* if the index is out of bounds.
|
|
101
|
-
*/
|
|
102
|
-
insertAt(index: number, val: T): boolean;
|
|
103
|
-
/**
|
|
104
|
-
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
105
|
-
* whether it is empty or not.
|
|
106
|
-
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
107
|
-
*/
|
|
108
|
-
isEmpty(): boolean;
|
|
109
|
-
/**
|
|
110
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
111
|
-
*/
|
|
112
|
-
clear(): void;
|
|
113
|
-
/**
|
|
114
|
-
* The `toArray` function converts a linked list into an array.
|
|
115
|
-
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
116
|
-
*/
|
|
117
|
-
toArray(): T[];
|
|
118
|
-
/**
|
|
119
|
-
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
120
|
-
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
121
|
-
*/
|
|
122
|
-
reverse(): void;
|
|
123
|
-
/**
|
|
124
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
125
|
-
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
126
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
127
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
128
|
-
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
129
|
-
*/
|
|
130
|
-
find(callback: (val: T) => boolean): T | null;
|
|
131
|
-
/**
|
|
132
|
-
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
133
|
-
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
134
|
-
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
135
|
-
* value is not found, it returns -1.
|
|
136
|
-
*/
|
|
137
|
-
indexOf(value: T): number;
|
|
138
|
-
/**
|
|
139
|
-
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
140
|
-
* null.
|
|
141
|
-
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
|
|
142
|
-
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
|
|
143
|
-
* the specified value is found, the function returns `null`.
|
|
144
|
-
*/
|
|
145
|
-
findNode(value: T): SinglyLinkedListNode<T> | null;
|
|
146
|
-
insertBefore(existingValue: T, newValue: T): boolean;
|
|
147
|
-
insertBefore(existingValue: SinglyLinkedListNode<T>, newValue: T): boolean;
|
|
148
|
-
insertAfter(existingValueOrNode: T, newValue: T): boolean;
|
|
149
|
-
insertAfter(existingValueOrNode: SinglyLinkedListNode<T>, newValue: T): boolean;
|
|
150
|
-
/**
|
|
151
|
-
* The function counts the number of occurrences of a given value in a linked list.
|
|
152
|
-
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
153
|
-
* @returns The count of occurrences of the given value in the linked list.
|
|
154
|
-
*/
|
|
155
|
-
countOccurrences(value: T): number;
|
|
156
|
-
}
|
|
@@ -1,502 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __values = (this && this.__values) || function(o) {
|
|
3
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
-
if (m) return m.call(o);
|
|
5
|
-
if (o && typeof o.length === "number") return {
|
|
6
|
-
next: function () {
|
|
7
|
-
if (o && i >= o.length) o = void 0;
|
|
8
|
-
return { value: o && o[i++], done: !o };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
31
|
-
/**
|
|
32
|
-
* data-structure-typed
|
|
33
|
-
*
|
|
34
|
-
* @author Tyler Zeng
|
|
35
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
36
|
-
* @license MIT License
|
|
37
|
-
*/
|
|
38
|
-
var SinglyLinkedListNode = /** @class */ (function () {
|
|
39
|
-
/**
|
|
40
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
41
|
-
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
|
42
|
-
* will be stored in the node of a linked list.
|
|
43
|
-
*/
|
|
44
|
-
function SinglyLinkedListNode(val) {
|
|
45
|
-
this._val = val;
|
|
46
|
-
this._next = null;
|
|
47
|
-
}
|
|
48
|
-
Object.defineProperty(SinglyLinkedListNode.prototype, "val", {
|
|
49
|
-
get: function () {
|
|
50
|
-
return this._val;
|
|
51
|
-
},
|
|
52
|
-
set: function (value) {
|
|
53
|
-
this._val = value;
|
|
54
|
-
},
|
|
55
|
-
enumerable: false,
|
|
56
|
-
configurable: true
|
|
57
|
-
});
|
|
58
|
-
Object.defineProperty(SinglyLinkedListNode.prototype, "next", {
|
|
59
|
-
get: function () {
|
|
60
|
-
return this._next;
|
|
61
|
-
},
|
|
62
|
-
set: function (value) {
|
|
63
|
-
this._next = value;
|
|
64
|
-
},
|
|
65
|
-
enumerable: false,
|
|
66
|
-
configurable: true
|
|
67
|
-
});
|
|
68
|
-
return SinglyLinkedListNode;
|
|
69
|
-
}());
|
|
70
|
-
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
71
|
-
var SinglyLinkedList = /** @class */ (function () {
|
|
72
|
-
/**
|
|
73
|
-
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
74
|
-
*/
|
|
75
|
-
function SinglyLinkedList() {
|
|
76
|
-
this._head = null;
|
|
77
|
-
this._tail = null;
|
|
78
|
-
this._length = 0;
|
|
79
|
-
}
|
|
80
|
-
Object.defineProperty(SinglyLinkedList.prototype, "head", {
|
|
81
|
-
get: function () {
|
|
82
|
-
return this._head;
|
|
83
|
-
},
|
|
84
|
-
set: function (value) {
|
|
85
|
-
this._head = value;
|
|
86
|
-
},
|
|
87
|
-
enumerable: false,
|
|
88
|
-
configurable: true
|
|
89
|
-
});
|
|
90
|
-
Object.defineProperty(SinglyLinkedList.prototype, "tail", {
|
|
91
|
-
get: function () {
|
|
92
|
-
return this._tail;
|
|
93
|
-
},
|
|
94
|
-
set: function (value) {
|
|
95
|
-
this._tail = value;
|
|
96
|
-
},
|
|
97
|
-
enumerable: false,
|
|
98
|
-
configurable: true
|
|
99
|
-
});
|
|
100
|
-
Object.defineProperty(SinglyLinkedList.prototype, "length", {
|
|
101
|
-
get: function () {
|
|
102
|
-
return this._length;
|
|
103
|
-
},
|
|
104
|
-
enumerable: false,
|
|
105
|
-
configurable: true
|
|
106
|
-
});
|
|
107
|
-
/**
|
|
108
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
109
|
-
* array.
|
|
110
|
-
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
111
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
112
|
-
*/
|
|
113
|
-
SinglyLinkedList.fromArray = function (data) {
|
|
114
|
-
var e_1, _a;
|
|
115
|
-
var singlyLinkedList = new SinglyLinkedList();
|
|
116
|
-
try {
|
|
117
|
-
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
118
|
-
var item = data_1_1.value;
|
|
119
|
-
singlyLinkedList.push(item);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
123
|
-
finally {
|
|
124
|
-
try {
|
|
125
|
-
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
126
|
-
}
|
|
127
|
-
finally { if (e_1) throw e_1.error; }
|
|
128
|
-
}
|
|
129
|
-
return singlyLinkedList;
|
|
130
|
-
};
|
|
131
|
-
SinglyLinkedList.prototype.getLength = function () {
|
|
132
|
-
return this._length;
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
136
|
-
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
137
|
-
* any type (T) as specified in the generic type declaration of the class or function.
|
|
138
|
-
*/
|
|
139
|
-
SinglyLinkedList.prototype.push = function (data) {
|
|
140
|
-
var newNode = new SinglyLinkedListNode(data);
|
|
141
|
-
if (!this.head) {
|
|
142
|
-
this.head = newNode;
|
|
143
|
-
this.tail = newNode;
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
this.tail.next = newNode;
|
|
147
|
-
this.tail = newNode;
|
|
148
|
-
}
|
|
149
|
-
this._length++;
|
|
150
|
-
};
|
|
151
|
-
/**
|
|
152
|
-
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
153
|
-
* pointers accordingly.
|
|
154
|
-
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
155
|
-
* the linked list is empty, it returns `null`.
|
|
156
|
-
*/
|
|
157
|
-
SinglyLinkedList.prototype.pop = function () {
|
|
158
|
-
if (!this.head)
|
|
159
|
-
return null;
|
|
160
|
-
if (this.head === this.tail) {
|
|
161
|
-
var val_1 = this.head.val;
|
|
162
|
-
this.head = null;
|
|
163
|
-
this.tail = null;
|
|
164
|
-
this._length--;
|
|
165
|
-
return val_1;
|
|
166
|
-
}
|
|
167
|
-
var current = this.head;
|
|
168
|
-
while (current.next !== this.tail) {
|
|
169
|
-
current = current.next;
|
|
170
|
-
}
|
|
171
|
-
var val = this.tail.val;
|
|
172
|
-
current.next = null;
|
|
173
|
-
this.tail = current;
|
|
174
|
-
this._length--;
|
|
175
|
-
return val;
|
|
176
|
-
};
|
|
177
|
-
/**
|
|
178
|
-
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
179
|
-
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
180
|
-
*/
|
|
181
|
-
SinglyLinkedList.prototype.shift = function () {
|
|
182
|
-
if (!this.head)
|
|
183
|
-
return null;
|
|
184
|
-
var removedNode = this.head;
|
|
185
|
-
this.head = this.head.next;
|
|
186
|
-
this._length--;
|
|
187
|
-
return removedNode.val;
|
|
188
|
-
};
|
|
189
|
-
/**
|
|
190
|
-
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
191
|
-
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
192
|
-
* linked list.
|
|
193
|
-
*/
|
|
194
|
-
SinglyLinkedList.prototype.unshift = function (val) {
|
|
195
|
-
var newNode = new SinglyLinkedListNode(val);
|
|
196
|
-
if (!this.head) {
|
|
197
|
-
this.head = newNode;
|
|
198
|
-
this.tail = newNode;
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
newNode.next = this.head;
|
|
202
|
-
this.head = newNode;
|
|
203
|
-
}
|
|
204
|
-
this._length++;
|
|
205
|
-
};
|
|
206
|
-
/**
|
|
207
|
-
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
208
|
-
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
209
|
-
* retrieve from the list.
|
|
210
|
-
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
|
211
|
-
* `null` if the index is out of bounds.
|
|
212
|
-
*/
|
|
213
|
-
SinglyLinkedList.prototype.getAt = function (index) {
|
|
214
|
-
if (index < 0 || index >= this.length)
|
|
215
|
-
return null;
|
|
216
|
-
var current = this.head;
|
|
217
|
-
for (var i = 0; i < index; i++) {
|
|
218
|
-
current = current.next;
|
|
219
|
-
}
|
|
220
|
-
return current.val;
|
|
221
|
-
};
|
|
222
|
-
/**
|
|
223
|
-
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
224
|
-
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
225
|
-
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
226
|
-
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
|
227
|
-
* specified index exists, or `null` if the index is out of bounds.
|
|
228
|
-
*/
|
|
229
|
-
SinglyLinkedList.prototype.getNodeAt = function (index) {
|
|
230
|
-
var current = this.head;
|
|
231
|
-
for (var i = 0; i < index; i++) {
|
|
232
|
-
current = current.next;
|
|
233
|
-
}
|
|
234
|
-
return current;
|
|
235
|
-
};
|
|
236
|
-
/**
|
|
237
|
-
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
238
|
-
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
239
|
-
* data structure. It is of type number.
|
|
240
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
241
|
-
* bounds.
|
|
242
|
-
*/
|
|
243
|
-
SinglyLinkedList.prototype.deleteAt = function (index) {
|
|
244
|
-
if (index < 0 || index >= this.length)
|
|
245
|
-
return null;
|
|
246
|
-
if (index === 0)
|
|
247
|
-
return this.shift();
|
|
248
|
-
if (index === this.length - 1)
|
|
249
|
-
return this.pop();
|
|
250
|
-
var prevNode = this.getNodeAt(index - 1);
|
|
251
|
-
var removedNode = prevNode.next;
|
|
252
|
-
prevNode.next = removedNode.next;
|
|
253
|
-
this._length--;
|
|
254
|
-
return removedNode.val;
|
|
255
|
-
};
|
|
256
|
-
/**
|
|
257
|
-
* The delete function removes a node with a specific value from a singly linked list.
|
|
258
|
-
* @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
|
|
259
|
-
* or a `SinglyLinkedListNode<T>` object.
|
|
260
|
-
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
261
|
-
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
262
|
-
*/
|
|
263
|
-
SinglyLinkedList.prototype.delete = function (valueOrNode) {
|
|
264
|
-
var value;
|
|
265
|
-
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
266
|
-
value = valueOrNode.val;
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
value = valueOrNode;
|
|
270
|
-
}
|
|
271
|
-
var current = this.head, prev = null;
|
|
272
|
-
while (current) {
|
|
273
|
-
if (current.val === value) {
|
|
274
|
-
if (prev === null) {
|
|
275
|
-
this.head = current.next;
|
|
276
|
-
if (current === this.tail) {
|
|
277
|
-
this.tail = null;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
prev.next = current.next;
|
|
282
|
-
if (current === this.tail) {
|
|
283
|
-
this.tail = prev;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
this._length--;
|
|
287
|
-
return true;
|
|
288
|
-
}
|
|
289
|
-
prev = current;
|
|
290
|
-
current = current.next;
|
|
291
|
-
}
|
|
292
|
-
return false;
|
|
293
|
-
};
|
|
294
|
-
/**
|
|
295
|
-
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
296
|
-
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
297
|
-
* linked list. It is of type number.
|
|
298
|
-
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
|
299
|
-
* specified index.
|
|
300
|
-
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
301
|
-
* if the index is out of bounds.
|
|
302
|
-
*/
|
|
303
|
-
SinglyLinkedList.prototype.insertAt = function (index, val) {
|
|
304
|
-
if (index < 0 || index > this.length)
|
|
305
|
-
return false;
|
|
306
|
-
if (index === 0) {
|
|
307
|
-
this.unshift(val);
|
|
308
|
-
return true;
|
|
309
|
-
}
|
|
310
|
-
if (index === this.length) {
|
|
311
|
-
this.push(val);
|
|
312
|
-
return true;
|
|
313
|
-
}
|
|
314
|
-
var newNode = new SinglyLinkedListNode(val);
|
|
315
|
-
var prevNode = this.getNodeAt(index - 1);
|
|
316
|
-
newNode.next = prevNode.next;
|
|
317
|
-
prevNode.next = newNode;
|
|
318
|
-
this._length++;
|
|
319
|
-
return true;
|
|
320
|
-
};
|
|
321
|
-
/**
|
|
322
|
-
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
323
|
-
* whether it is empty or not.
|
|
324
|
-
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
325
|
-
*/
|
|
326
|
-
SinglyLinkedList.prototype.isEmpty = function () {
|
|
327
|
-
return this.length === 0;
|
|
328
|
-
};
|
|
329
|
-
/**
|
|
330
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
331
|
-
*/
|
|
332
|
-
SinglyLinkedList.prototype.clear = function () {
|
|
333
|
-
this._head = null;
|
|
334
|
-
this._tail = null;
|
|
335
|
-
this._length = 0;
|
|
336
|
-
};
|
|
337
|
-
/**
|
|
338
|
-
* The `toArray` function converts a linked list into an array.
|
|
339
|
-
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
340
|
-
*/
|
|
341
|
-
SinglyLinkedList.prototype.toArray = function () {
|
|
342
|
-
var array = [];
|
|
343
|
-
var current = this.head;
|
|
344
|
-
while (current) {
|
|
345
|
-
array.push(current.val);
|
|
346
|
-
current = current.next;
|
|
347
|
-
}
|
|
348
|
-
return array;
|
|
349
|
-
};
|
|
350
|
-
/**
|
|
351
|
-
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
352
|
-
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
353
|
-
*/
|
|
354
|
-
SinglyLinkedList.prototype.reverse = function () {
|
|
355
|
-
var _a;
|
|
356
|
-
if (!this.head || this.head === this.tail)
|
|
357
|
-
return;
|
|
358
|
-
var prev = null;
|
|
359
|
-
var current = this.head;
|
|
360
|
-
var next = null;
|
|
361
|
-
while (current) {
|
|
362
|
-
next = current.next;
|
|
363
|
-
current.next = prev;
|
|
364
|
-
prev = current;
|
|
365
|
-
current = next;
|
|
366
|
-
}
|
|
367
|
-
_a = __read([this.tail, this.head], 2), this.head = _a[0], this.tail = _a[1];
|
|
368
|
-
};
|
|
369
|
-
/**
|
|
370
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
371
|
-
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
372
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
373
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
374
|
-
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
375
|
-
*/
|
|
376
|
-
SinglyLinkedList.prototype.find = function (callback) {
|
|
377
|
-
var current = this.head;
|
|
378
|
-
while (current) {
|
|
379
|
-
if (callback(current.val)) {
|
|
380
|
-
return current.val;
|
|
381
|
-
}
|
|
382
|
-
current = current.next;
|
|
383
|
-
}
|
|
384
|
-
return null;
|
|
385
|
-
};
|
|
386
|
-
/**
|
|
387
|
-
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
388
|
-
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
389
|
-
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
390
|
-
* value is not found, it returns -1.
|
|
391
|
-
*/
|
|
392
|
-
SinglyLinkedList.prototype.indexOf = function (value) {
|
|
393
|
-
var index = 0;
|
|
394
|
-
var current = this.head;
|
|
395
|
-
while (current) {
|
|
396
|
-
if (current.val === value) {
|
|
397
|
-
return index;
|
|
398
|
-
}
|
|
399
|
-
index++;
|
|
400
|
-
current = current.next;
|
|
401
|
-
}
|
|
402
|
-
return -1;
|
|
403
|
-
};
|
|
404
|
-
/**
|
|
405
|
-
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
406
|
-
* null.
|
|
407
|
-
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
|
|
408
|
-
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
|
|
409
|
-
* the specified value is found, the function returns `null`.
|
|
410
|
-
*/
|
|
411
|
-
SinglyLinkedList.prototype.findNode = function (value) {
|
|
412
|
-
var current = this.head;
|
|
413
|
-
while (current) {
|
|
414
|
-
if (current.val === value) {
|
|
415
|
-
return current;
|
|
416
|
-
}
|
|
417
|
-
current = current.next;
|
|
418
|
-
}
|
|
419
|
-
return null;
|
|
420
|
-
};
|
|
421
|
-
/**
|
|
422
|
-
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
423
|
-
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
|
|
424
|
-
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
425
|
-
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
426
|
-
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
427
|
-
* inserted before the existing value, and `false` otherwise.
|
|
428
|
-
*/
|
|
429
|
-
SinglyLinkedList.prototype.insertBefore = function (existingValueOrNode, newValue) {
|
|
430
|
-
if (!this.head)
|
|
431
|
-
return false;
|
|
432
|
-
var existingValue;
|
|
433
|
-
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
434
|
-
existingValue = existingValueOrNode.val;
|
|
435
|
-
}
|
|
436
|
-
else {
|
|
437
|
-
existingValue = existingValueOrNode;
|
|
438
|
-
}
|
|
439
|
-
if (this.head.val === existingValue) {
|
|
440
|
-
this.unshift(newValue);
|
|
441
|
-
return true;
|
|
442
|
-
}
|
|
443
|
-
var current = this.head;
|
|
444
|
-
while (current.next) {
|
|
445
|
-
if (current.next.val === existingValue) {
|
|
446
|
-
var newNode = new SinglyLinkedListNode(newValue);
|
|
447
|
-
newNode.next = current.next;
|
|
448
|
-
current.next = newNode;
|
|
449
|
-
this._length++;
|
|
450
|
-
return true;
|
|
451
|
-
}
|
|
452
|
-
current = current.next;
|
|
453
|
-
}
|
|
454
|
-
return false;
|
|
455
|
-
};
|
|
456
|
-
/**
|
|
457
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
458
|
-
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
|
|
459
|
-
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
460
|
-
* @param {T} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
461
|
-
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
462
|
-
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
463
|
-
*/
|
|
464
|
-
SinglyLinkedList.prototype.insertAfter = function (existingValueOrNode, newValue) {
|
|
465
|
-
var existingNode;
|
|
466
|
-
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
467
|
-
existingNode = existingValueOrNode;
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
471
|
-
}
|
|
472
|
-
if (existingNode) {
|
|
473
|
-
var newNode = new SinglyLinkedListNode(newValue);
|
|
474
|
-
newNode.next = existingNode.next;
|
|
475
|
-
existingNode.next = newNode;
|
|
476
|
-
if (existingNode === this.tail) {
|
|
477
|
-
this.tail = newNode;
|
|
478
|
-
}
|
|
479
|
-
this._length++;
|
|
480
|
-
return true;
|
|
481
|
-
}
|
|
482
|
-
return false;
|
|
483
|
-
};
|
|
484
|
-
/**
|
|
485
|
-
* The function counts the number of occurrences of a given value in a linked list.
|
|
486
|
-
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
487
|
-
* @returns The count of occurrences of the given value in the linked list.
|
|
488
|
-
*/
|
|
489
|
-
SinglyLinkedList.prototype.countOccurrences = function (value) {
|
|
490
|
-
var count = 0;
|
|
491
|
-
var current = this.head;
|
|
492
|
-
while (current) {
|
|
493
|
-
if (current.val === value) {
|
|
494
|
-
count++;
|
|
495
|
-
}
|
|
496
|
-
current = current.next;
|
|
497
|
-
}
|
|
498
|
-
return count;
|
|
499
|
-
};
|
|
500
|
-
return SinglyLinkedList;
|
|
501
|
-
}());
|
|
502
|
-
exports.SinglyLinkedList = SinglyLinkedList;
|