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 bde2d44

Browse files
committed
add removeIndex method to singly/doubly linked lists
1 parent f9e9010 commit bde2d44

File tree

4 files changed

+60
-59
lines changed

4 files changed

+60
-59
lines changed

‎dist/datastructures/DoublyLinkedList.js‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,20 @@ class DoublyLinkedList extends SinglyLinkedList_1.default {
6868
};
6969
this.insert = (value, index) => {
7070
if (index === 0)
71-
return this.unshift(value);
71+
return this.unshift(value);// uses the doublylinkedlist's unshift/push/shift/pop as
7272
if (index === this.length)
73-
return this.push(value);
73+
return this.push(value);// they already cover logic for dealing with prev
7474
return super.insert(value, index, { prevEnabled: true });
7575
};
76+
this.removeIndex = (index) => {
77+
if (index === 0)
78+
return this.shift();
79+
if (index === this.length)
80+
return this.pop();
81+
return super.removeIndex(index, { prevEnabled: true });
82+
};
7683
}
7784
;
7885
}
7986
;
80-
const list = new DoublyLinkedList();
81-
list.push('Ron');
82-
list.push('Harry');
83-
list.push('Hermione');
84-
list.insert('Hagrid', 3);
85-
let node = list.head;
86-
for (let i = 0; i < list.length; i++) {
87-
console.log(node);
88-
if (node) {
89-
node = node.next;
90-
}
91-
}
9287
exports.default = DoublyLinkedList;

‎dist/datastructures/SinglyLinkedList.js‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ class SinglyLinkedList {
108108
return selectedNode;
109109
}
110110
insert(value, index, options = { prevEnabled: false }) {
111+
if (index > this.length - 1 || index < 0)
112+
return null; // TODO: throw indexError ? user tried to insert at out of range index.
111113
if (index === 0)
112114
return this.unshift(value);
113115
if (index === this.length)
114116
return this.push(value);
115117
const { prevEnabled } = options;
116118
let newNode = new Node_1.default(value);
117119
const prevNode = this.get(index - 1);
118-
if (!prevNode)
119-
return null; // TODO: throw indexError ? user tried to insert at out of range index.
120120
// maybe move the out of bounds errors to top of function?
121121
const targetNode = prevNode.next;
122122
if (prevEnabled) {
@@ -128,17 +128,25 @@ class SinglyLinkedList {
128128
this._length++;
129129
return newNode;
130130
}
131+
removeIndex(index, options = { prevEnabled: false }) {
132+
if (index > this.length - 1 || index < 0)
133+
return null;
134+
if (index === 0)
135+
return this.shift();
136+
if (index === this.length - 1)
137+
return this.pop();
138+
const { prevEnabled } = options;
139+
const prevNode = this.get(index - 1);
140+
const targetNode = prevNode.next; // all three assert exists will be true
141+
const nextNode = targetNode.next; // because of first line's return null
142+
prevNode.next = nextNode;
143+
if (prevEnabled) {
144+
nextNode.prev = prevNode;
145+
targetNode.prev = null;
146+
}
147+
targetNode.next = null;
148+
this._length--;
149+
return targetNode;
150+
}
131151
}
132-
// const list = new SinglyLinkedList();
133-
// list.push('Ron');
134-
// list.push('Harry');
135-
// list.push('Hermione');
136-
// list.insert('Hagrid', 0);
137-
// let node = list.head;
138-
// for (let i = 0; i < list.length; i++) {
139-
// console.log(node);
140-
// if (node) {
141-
// node = node.next;
142-
// }
143-
// }
144152
exports.default = SinglyLinkedList;

‎src/datastructures/DoublyLinkedList.ts‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DoublyLinkedList<T> extends SinglyLinkedList<T> {
3131
}
3232

3333
unshift = (value: T): _Node<T> => {
34-
let oldHead = this.head;
34+
let oldHead: _Node<T>|null = this.head;
3535
let node = super.unshift(value);
3636
if (oldHead) {
3737
oldHead.prev = node;
@@ -66,24 +66,16 @@ class DoublyLinkedList<T> extends SinglyLinkedList<T> {
6666
}
6767

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

76-
const list = new DoublyLinkedList();
77-
list.push('Ron');
78-
list.push('Harry');
79-
list.push('Hermione');
80-
list.insert('Hagrid', 3);
81-
let node = list.head;
82-
for (let i = 0; i < list.length; i++) {
83-
console.log(node);
84-
if (node) {
85-
node = node.next;
74+
removeIndex = (index: number): _Node<T> | null => {
75+
if (index === 0) return this.shift();
76+
if (index === this.length) return this.pop();
77+
return super.removeIndex(index, { prevEnabled: true });
8678
}
87-
}
79+
};
8880

8981
export default DoublyLinkedList;

‎src/datastructures/SinglyLinkedList.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _Node from './Node';
2+
import { listenerCount } from 'cluster';
23

34
interface SinglyLinkedList<T> {
45
head: null | _Node<T>;
@@ -116,13 +117,13 @@ class SinglyLinkedList<T> {
116117
}
117118

118119
insert(value: T, index: number, options: ListMethodOptions = { prevEnabled: false }): _Node<T> | null {
120+
if (index > this.length - 1 || index < 0) return null; // TODO: throw indexError ? user tried to insert at out of range index.
119121
if (index === 0) return this.unshift(value);
120122
if (index === this.length) return this.push(value);
121123

122124
const { prevEnabled } = options;
123125
let newNode = new _Node(value);
124-
const prevNode = this.get(index - 1);
125-
if (!prevNode) return null; // TODO: throw indexError ? user tried to insert at out of range index.
126+
const prevNode = this.get(index - 1)!;
126127
// maybe move the out of bounds errors to top of function?
127128
const targetNode = prevNode.next;
128129
if (prevEnabled) {
@@ -134,20 +135,25 @@ class SinglyLinkedList<T> {
134135
this._length++;
135136
return newNode;
136137
}
137-
}
138138

139-
// const list = new SinglyLinkedList();
140-
// list.push('Ron');
141-
// list.push('Harry');
142-
// list.push('Hermione');
143-
// list.insert('Hagrid', 0);
144-
// let node = list.head;
145-
// for (let i = 0; i < list.length; i++) {
146-
// console.log(node);
147-
// if (node) {
148-
// node = node.next;
149-
// }
150-
// }
139+
removeIndex(index: number, options: ListMethodOptions = { prevEnabled: false }): _Node<T> | null {
140+
if (index > this.length - 1 || index < 0) return null;
141+
if (index === 0) return this.shift();
142+
if (index === this.length - 1) return this.pop();
151143

144+
const { prevEnabled } = options;
145+
const prevNode = this.get(index - 1)!;
146+
const targetNode = prevNode.next!; // all three assert exists will be true
147+
const nextNode = targetNode.next!; // because of first line's return null
148+
prevNode.next = nextNode;
149+
if (prevEnabled) {
150+
nextNode.prev = prevNode;
151+
targetNode.prev = null;
152+
}
153+
targetNode.next = null;
154+
this._length--;
155+
return targetNode;
156+
}
157+
}
152158

153159
export default SinglyLinkedList;

0 commit comments

Comments
(0)

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