Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 799be75

Browse files
committed
small refactors, add IndexError message
1 parent 353b2ae commit 799be75

File tree

9 files changed

+50
-41
lines changed

9 files changed

+50
-41
lines changed

‎dist/datastructures/DoublyLinkedList.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ function testTraversalDLL(list) {
1111
return true; // empty list
1212
let node = list.tail;
1313
let countedLength = 1;
14-
while (node) {
14+
while (node.prev) {
1515
// console.log(node.data)
16-
if (node.prev) {
17-
node = node.prev;
18-
countedLength++;
19-
}
20-
else
21-
break;
16+
node = node.prev;
17+
countedLength++;
2218
}
2319
return (node === list.head && countedLength === list.length);
2420
}
@@ -76,11 +72,15 @@ test(`Able to traverse a list mutated by pushes, pops, shifts, unshifts, inserts
7672
if (i % 7 === 0) {
7773
list.shift();
7874
}
75+
if (i === 1)
76+
list.insert(`insert: ${i}`);
7977
if (i % 9 === 0) {
8078
list.insert(`insert: ${i}`, Math.floor(Math.random() * list.length));
8179
}
80+
if (i === 20)
81+
list.removeIndex(list.length - 1);
8282
if (i % 11 === 0) {
83-
list.removeIndex(Math.floor(Math.random() * list.length));
83+
list.removeIndex(Math.floor(Math.random() * list.length-1));
8484
}
8585
}
8686
expect(SinglyLinkedList_test_1.testTraverseSLL(list)).toBe(true);

‎dist/datastructures/SinglyLinkedList.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class SinglyLinkedList {
9696
return oldHead;
9797
}
9898
get(index) {
99-
if (!this.head || (index > this.length || index < 0))
100-
throw new IndexError_1.default;
99+
if (!this.head || (index > this.length -1|| index < 0))
100+
throw new IndexError_1.default('list index out of range.');
101101
let counter = 0;
102102
let node = this.head;
103103
while (counter < index) {
@@ -118,8 +118,8 @@ class SinglyLinkedList {
118118
insert(value, index, options = { prevEnabled: false }) {
119119
if (!index)
120120
return this.push(value);
121-
if (index > this.length || index < 0)
122-
throw new IndexError_1.default;
121+
if (index > this.length -1|| index < 0)
122+
throw new IndexError_1.default('list index out of range.');
123123
if (index === 0)
124124
return this.unshift(value);
125125
if (index === this.length)
@@ -139,8 +139,8 @@ class SinglyLinkedList {
139139
return newNode;
140140
}
141141
removeIndex(index, options = { prevEnabled: false }) {
142-
if (index > this.length || index < 0)
143-
throw new IndexError_1.default;
142+
if (index > this.length -1|| index < 0)
143+
throw new IndexError_1.default('list index out of range.');
144144
if (index === 0)
145145
return this.shift();
146146
if (index === this.length - 1)
@@ -161,7 +161,7 @@ class SinglyLinkedList {
161161
log(beginning = 0, end = this.length - 1) {
162162
if ((beginning > this.length - 1 || beginning < 0)
163163
|| (end > this.length - 1 || end < 0))
164-
throw new IndexError_1.default;
164+
throw new IndexError_1.default('list index out of range.');
165165
let node = this.get(beginning);
166166
let count = beginning;
167167
while (node) {
@@ -177,7 +177,7 @@ class SinglyLinkedList {
177177
toString(beginning = 0, end = this.length - 1) {
178178
if ((beginning > this.length - 1 || beginning < 0)
179179
|| (end > this.length - 1 || end < 0))
180-
throw new IndexError_1.default;
180+
throw new IndexError_1.default('list index out of range.');
181181
let node = this.get(beginning);
182182
while (node) {
183183
console.log(node.data);

‎dist/datastructures/SinglyLinkedList.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ function testTraverseSLL(list) {
99
return true; // empty list
1010
let node = list.head;
1111
let countedLength = 1;
12-
while (node) {
12+
while (node.next) {
1313
// console.log(node.data)
14-
if (node.next) {
15-
node = node.next;
16-
countedLength++;
17-
}
18-
else
19-
break;
14+
node = node.next;
15+
countedLength++;
2016
}
2117
return (node === list.tail && countedLength === list.length);
2218
}
@@ -73,9 +69,13 @@ test(`Able to traverse a list mutated by pushes, pops, shifts, unshifts, inserts
7369
if (i % 7 === 0) {
7470
list.shift();
7571
}
72+
if (i === 1)
73+
list.insert(`insert: ${i}`);
7674
if (i % 9 === 0) {
7775
list.insert(`insert: ${i}`, Math.floor(Math.random() * list.length));
7876
}
77+
if (i === 20)
78+
list.removeIndex(list.length - 1);
7979
if (i % 11 === 0) {
8080
list.removeIndex(Math.floor(Math.random() * list.length));
8181
}

‎dist/utils/IndexError.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
class IndexError extends Error {
4+
constructor(message = "") {
5+
super(message);
6+
this.name = 'IndexError';
7+
}
48
}
59
exports.default = IndexError;

‎src/datastructures/DoublyLinkedList.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ export function testTraversalDLL<T>(list: DoublyLinkedList<T>): boolean {
66
if (!list.tail) return true; // empty list
77
let node = list.tail!;
88
let countedLength = 1;
9-
while (node) {
9+
while (node.prev) {
1010
// console.log(node.data)
11-
if (node.prev) {
12-
node = node.prev;
13-
countedLength++;
14-
} else break;
11+
node = node.prev;
12+
countedLength++;
1513
}
1614
return (node === list.head && countedLength === list.length);
1715
}
@@ -75,11 +73,13 @@ test(`Able to traverse a list mutated by pushes, pops, shifts, unshifts, inserts
7573
if (i % 7 === 0) {
7674
list.shift();
7775
}
76+
if (i === 1) list.insert(`insert: ${i}`);
7877
if (i % 9 === 0) {
7978
list.insert(`insert: ${i}`, Math.floor(Math.random() * list.length));
8079
}
80+
if (i === 20) list.removeIndex(list.length - 1);
8181
if (i % 11 === 0) {
82-
list.removeIndex(Math.floor(Math.random() * list.length));
82+
list.removeIndex(Math.floor(Math.random() * list.length-1));
8383
}
8484
}
8585
expect(testTraverseSLL(list)).toBe(true);

‎src/datastructures/DoublyLinkedList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DoublyLinkedList<T> extends SinglyLinkedList<T> {
6565
}
6666
}
6767

68-
insert = (value: T, index: number) => {
68+
insert = (value: T, index?: number) => {
6969
if (index === 0) return this.unshift(value); // uses the doublylinkedlist's unshift/push/shift/pop as
7070
if (index === this.length) return this.push(value); // they already cover logic for dealing with prev
7171
return super.insert(value, index, { prevEnabled: true });

‎src/datastructures/SinglyLinkedList.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ export function testTraverseSLL<T>(list: SinglyLinkedList<T>): boolean {
44
if (!list.head) return true; // empty list
55
let node = list.head!;
66
let countedLength = 1;
7-
while (node) {
7+
while (node.next) {
88
// console.log(node.data)
9-
if (node.next) {
10-
node = node.next;
11-
countedLength++;
12-
} else break;
9+
node = node.next;
10+
countedLength++;
1311
}
1412
return (node === list.tail && countedLength === list.length);
1513
}
@@ -72,9 +70,11 @@ test(`Able to traverse a list mutated by pushes, pops, shifts, unshifts, inserts
7270
if (i % 7 === 0) {
7371
list.shift();
7472
}
73+
if (i === 1) list.insert(`insert: ${i}`);
7574
if (i % 9 === 0) {
7675
list.insert(`insert: ${i}`, Math.floor(Math.random() * list.length));
7776
}
77+
if (i === 20) list.removeIndex(list.length - 1);
7878
if (i % 11 === 0) {
7979
list.removeIndex(Math.floor(Math.random() * list.length));
8080
}

‎src/datastructures/SinglyLinkedList.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class SinglyLinkedList<T> {
107107
}
108108

109109
get(index: number): _Node<T> | null {
110-
if (!this.head || (index > this.length || index < 0)) throw new IndexError;
110+
if (!this.head || (index > this.length -1|| index < 0)) throw new IndexError('list index out of range.');
111111
let counter = 0;
112112
let node = this.head;
113113
while (counter < index) {
@@ -128,7 +128,7 @@ class SinglyLinkedList<T> {
128128

129129
insert(value: T, index?: number, options: ListMethodOptions = { prevEnabled: false }): _Node<T> | null {
130130
if (!index) return this.push(value);
131-
if (index > this.length || index < 0) throw new IndexError;
131+
if (index > this.length -1|| index < 0) throw new IndexError('list index out of range.');
132132
if (index === 0) return this.unshift(value);
133133
if (index === this.length) return this.push(value);
134134
const { prevEnabled } = options;
@@ -147,7 +147,7 @@ class SinglyLinkedList<T> {
147147
}
148148

149149
removeIndex(index: number, options: ListMethodOptions = { prevEnabled: false }): _Node<T> | void {
150-
if (index > this.length || index < 0) throw new IndexError;
150+
if (index > this.length -1|| index < 0) throw new IndexError('list index out of range.');
151151
if (index === 0) return this.shift();
152152
if (index === this.length - 1) return this.pop();
153153

@@ -166,7 +166,7 @@ class SinglyLinkedList<T> {
166166
}
167167
log(beginning: number = 0, end: number = this.length - 1) {
168168
if ((beginning > this.length - 1 || beginning < 0)
169-
|| (end > this.length - 1 || end < 0)) throw new IndexError;
169+
|| (end > this.length - 1 || end < 0)) throw new IndexError('list index out of range.');
170170
let node = this.get(beginning);
171171
let count = beginning;
172172
while (node) {
@@ -179,7 +179,7 @@ class SinglyLinkedList<T> {
179179
}
180180
toString(beginning: number = 0, end: number = this.length - 1) {
181181
if ((beginning > this.length - 1 || beginning < 0)
182-
|| (end > this.length - 1 || end < 0)) throw new IndexError;
182+
|| (end > this.length - 1 || end < 0)) throw new IndexError('list index out of range.');
183183
let node = this.get(beginning);
184184
while (node) {
185185
console.log(node.data)

‎src/utils/IndexError.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export default class IndexError extends Error { }
1+
export default class IndexError extends Error {
2+
constructor(message: string = "") {
3+
super(message);
4+
this.name = 'IndexError';
5+
}
6+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /