@gabrielrufino/cube 1.0.22 → 1.0.23
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.
|
@@ -4,10 +4,10 @@ interface ILinkedList<T> {
|
|
|
4
4
|
get size(): number;
|
|
5
5
|
get isEmpty(): boolean;
|
|
6
6
|
push(_element: T): T;
|
|
7
|
-
getFromPosition(_position: number): ILinkedListItem<T> |
|
|
7
|
+
getFromPosition(_position: number): ILinkedListItem<T> | null;
|
|
8
8
|
positionOf(_element: T): number | undefined;
|
|
9
9
|
insertInPosition(_element: T, _position: number): T | undefined;
|
|
10
|
-
remove(_element: T): T |
|
|
11
|
-
removeFromPosition(_position: number): T |
|
|
10
|
+
remove(_element: T): T | null;
|
|
11
|
+
removeFromPosition(_position: number): T | null;
|
|
12
12
|
}
|
|
13
13
|
export default ILinkedList;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import ILinkedList from './ILinkedList';
|
|
2
|
+
import ILinkedListItem from './ILinkedListItem';
|
|
2
3
|
export default class LinkedList<T = number> implements ILinkedList<T> {
|
|
3
4
|
private readonly _FIRST_POSITION;
|
|
4
5
|
private _head;
|
|
@@ -12,13 +13,10 @@ export default class LinkedList<T = number> implements ILinkedList<T> {
|
|
|
12
13
|
get isEmpty(): boolean;
|
|
13
14
|
positionOf(element: T): number | undefined;
|
|
14
15
|
push(element: T): T;
|
|
15
|
-
remove(element: T): T |
|
|
16
|
+
remove(element: T): T | null;
|
|
16
17
|
insertInPosition(element: T, position: number): T | undefined;
|
|
17
|
-
getFromPosition(position: number):
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} | undefined;
|
|
21
|
-
removeFromPosition(position: number): T | undefined;
|
|
22
|
-
private getNodeFromPosition;
|
|
18
|
+
getFromPosition(position: number): ILinkedListItem<T> | null;
|
|
19
|
+
removeFromPosition(position: number): T | null;
|
|
20
|
+
private _getNodeFromPosition;
|
|
23
21
|
private [Symbol.toPrimitive];
|
|
24
22
|
}
|
|
@@ -55,7 +55,7 @@ var LinkedList = /** @class */ (function () {
|
|
|
55
55
|
var current = this._head;
|
|
56
56
|
var position = 0;
|
|
57
57
|
while (current && current.value !== element) {
|
|
58
|
-
current = current
|
|
58
|
+
current = current.next;
|
|
59
59
|
position += 1;
|
|
60
60
|
}
|
|
61
61
|
if (current) {
|
|
@@ -81,6 +81,7 @@ var LinkedList = /** @class */ (function () {
|
|
|
81
81
|
if (position) {
|
|
82
82
|
return this.removeFromPosition(position);
|
|
83
83
|
}
|
|
84
|
+
return null;
|
|
84
85
|
};
|
|
85
86
|
LinkedList.prototype.insertInPosition = function (element, position) {
|
|
86
87
|
if (position < this._FIRST_POSITION || position > this.size) {
|
|
@@ -92,7 +93,7 @@ var LinkedList = /** @class */ (function () {
|
|
|
92
93
|
this._head = node;
|
|
93
94
|
}
|
|
94
95
|
else {
|
|
95
|
-
var before = this.
|
|
96
|
+
var before = this._getNodeFromPosition(position - 1);
|
|
96
97
|
var after = (before && before.next) || null;
|
|
97
98
|
if (before) {
|
|
98
99
|
before.next = node;
|
|
@@ -104,51 +105,45 @@ var LinkedList = /** @class */ (function () {
|
|
|
104
105
|
};
|
|
105
106
|
LinkedList.prototype.getFromPosition = function (position) {
|
|
106
107
|
var _a;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
var node = this._head;
|
|
111
|
-
for (var i = 0; i < position; i++) {
|
|
112
|
-
node = (node === null || node === void 0 ? void 0 : node.next) || null;
|
|
108
|
+
var node = this._getNodeFromPosition(position);
|
|
109
|
+
if (!node) {
|
|
110
|
+
return null;
|
|
113
111
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return undefined;
|
|
112
|
+
return {
|
|
113
|
+
value: node.value,
|
|
114
|
+
next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
|
|
115
|
+
};
|
|
121
116
|
};
|
|
122
117
|
LinkedList.prototype.removeFromPosition = function (position) {
|
|
123
|
-
if (position < this._FIRST_POSITION || position
|
|
124
|
-
return
|
|
118
|
+
if (position < this._FIRST_POSITION || position >= this.size) {
|
|
119
|
+
return null;
|
|
125
120
|
}
|
|
126
121
|
var current = this._head;
|
|
127
122
|
if (position === this._FIRST_POSITION) {
|
|
128
|
-
this._head =
|
|
123
|
+
this._head = current.next;
|
|
129
124
|
}
|
|
130
125
|
else {
|
|
131
126
|
var previous = void 0;
|
|
132
|
-
for (var i = 0; i < position; i++) {
|
|
127
|
+
for (var i = 0; i < position && current; i++) {
|
|
133
128
|
previous = current;
|
|
134
|
-
current =
|
|
129
|
+
current = current.next;
|
|
135
130
|
}
|
|
136
131
|
if (previous) {
|
|
137
|
-
previous.next =
|
|
132
|
+
previous.next = current.next;
|
|
138
133
|
}
|
|
139
134
|
}
|
|
140
135
|
this._size -= 1;
|
|
141
|
-
return current
|
|
136
|
+
return current.value;
|
|
142
137
|
};
|
|
143
|
-
LinkedList.prototype.
|
|
144
|
-
if (position < this._FIRST_POSITION || position > this.size - 1) {
|
|
145
|
-
return
|
|
138
|
+
LinkedList.prototype._getNodeFromPosition = function (position) {
|
|
139
|
+
if (position < this._FIRST_POSITION || position > (this.size - 1)) {
|
|
140
|
+
return null;
|
|
146
141
|
}
|
|
147
142
|
var node = this._head;
|
|
148
|
-
for (var i = 0; i < position; i++) {
|
|
149
|
-
node =
|
|
143
|
+
for (var i = 0; i < position && (node === null || node === void 0 ? void 0 : node.next); i++) {
|
|
144
|
+
node = node.next;
|
|
150
145
|
}
|
|
151
|
-
return node
|
|
146
|
+
return node;
|
|
152
147
|
};
|
|
153
148
|
LinkedList.prototype[Symbol.toPrimitive] = function (type) {
|
|
154
149
|
var primitives = {
|