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 f9e9010

Browse files
committed
refactor insert method
1 parent 6af1c97 commit f9e9010

File tree

4 files changed

+97
-66
lines changed

4 files changed

+97
-66
lines changed

‎dist/datastructures/DoublyLinkedList.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ class DoublyLinkedList extends SinglyLinkedList_1.default {
4242
};
4343
this.shift = () => {
4444
if (this.head && this.head.next) {
45-
// set new head's prev to null
4645
this.head.next.prev = null;
4746
}
4847
return super.shift();
4948
};
5049
this.get = (index) => {
5150
if (index < (this.length / 2)) {
51+
// start at head
5252
return super.get(index);
5353
}
5454
else {
55+
// count backwards from tail
5556
if (!this.tail || (index > this.length - 1 || index < 0))
5657
return null;
5758
let counter = this.length - 1;
@@ -65,9 +66,27 @@ class DoublyLinkedList extends SinglyLinkedList_1.default {
6566
return node;
6667
}
6768
};
68-
this.insert = (value, index) => super.insert(value, index, { prevEnabled: true });
69+
this.insert = (value, index) => {
70+
if (index === 0)
71+
return this.unshift(value);
72+
if (index === this.length)
73+
return this.push(value);
74+
return super.insert(value, index, { prevEnabled: true });
75+
};
6976
}
7077
;
7178
}
7279
;
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+
}
7392
exports.default = DoublyLinkedList;

‎dist/datastructures/SinglyLinkedList.js‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class SinglyLinkedList {
3939
}
4040
pop() {
4141
// remove last node from list
42-
// O(n) time since we have to iterate over the whole list to find the new tail, and make the new tail's next point to null.
42+
// O(n) time since we have to iterate over the whole list to find the new tail,
43+
// and make the new tail's next point to null.
4344
if (!this.head)
4445
return null;
4546
const node = this.head;
@@ -107,42 +108,37 @@ class SinglyLinkedList {
107108
return selectedNode;
108109
}
109110
insert(value, index, options = { prevEnabled: false }) {
110-
// would be nice to refactor out the prevEnabled parts to the DoublyLinkedList insert method,
111-
// but setting the prev props for surrounding nodes has to be done one way...
112-
// * this method should be cleaned up and reorganized anyways since it's too long.
111+
if (index === 0)
112+
return this.unshift(value);
113+
if (index === this.length)
114+
return this.push(value);
113115
const { prevEnabled } = options;
114116
let newNode = new Node_1.default(value);
115-
if (index === 0) {
116-
// insert as head node
117-
if (prevEnabled)
118-
newNode.prev = null;
119-
newNode.next = this.head;
120-
this.head = newNode;
121-
this._length++;
122-
return newNode;
123-
}
124-
else if (index === this.length) {
125-
// insert as tail node
126-
if (prevEnabled)
127-
newNode.prev = this.tail;
128-
this.tail.next = newNode;
129-
this.tail = newNode;
130-
}
131-
else {
132-
const prevNode = this.get(index - 1);
133-
if (!prevNode)
134-
return null; // TODO: throw indexError ? user tried to insert at out of range index.
135-
// maybe move the out of bounds errors to top of function?
136-
const targetNode = prevNode.next;
137-
if (prevEnabled) {
138-
newNode.prev = prevNode;
139-
targetNode.prev = newNode; // assert exists because ^ insert as new tail node is covered
140-
}
141-
prevNode.next = newNode;
142-
newNode.next = targetNode;
117+
const prevNode = this.get(index - 1);
118+
if (!prevNode)
119+
return null; // TODO: throw indexError ? user tried to insert at out of range index.
120+
// maybe move the out of bounds errors to top of function?
121+
const targetNode = prevNode.next;
122+
if (prevEnabled) {
123+
newNode.prev = prevNode;
124+
targetNode.prev = newNode; // assert exists because ^ insert as new tail node is covered
143125
}
126+
prevNode.next = newNode;
127+
newNode.next = targetNode;
144128
this._length++;
145129
return newNode;
146130
}
147131
}
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+
// }
148144
exports.default = SinglyLinkedList;

‎src/datastructures/DoublyLinkedList.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ class DoublyLinkedList<T> extends SinglyLinkedList<T> {
4242

4343
shift = (): _Node<T> | null => {
4444
if (this.head && this.head.next) {
45-
// set new head's prev to null
4645
this.head.next.prev = null;
4746
}
4847
return super.shift();
4948
}
5049
get = (index: number): _Node<T> | null => {
5150
if (index < (this.length / 2)) {
51+
// start at head
5252
return super.get(index);
5353
}
5454
else {
55+
// count backwards from tail
5556
if (!this.tail || (index > this.length - 1 || index < 0)) return null;
5657
let counter = this.length - 1;
5758
let node = this.tail;
@@ -64,8 +65,25 @@ class DoublyLinkedList<T> extends SinglyLinkedList<T> {
6465
}
6566
}
6667

67-
insert = (value: T, index: number) => super.insert(value, index, { prevEnabled: true });
68+
insert = (value: T, index: number) => {
69+
if (index === 0) return this.unshift(value);
70+
if (index === this.length) return this.push(value);
71+
return super.insert(value, index, { prevEnabled: true });
72+
};
6873
};
6974

70-
export default DoublyLinkedList;
7175

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;
86+
}
87+
}
88+
89+
export default DoublyLinkedList;

‎src/datastructures/SinglyLinkedList.ts‎

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class SinglyLinkedList<T> {
4848

4949
pop(): _Node<T> | null {
5050
// remove last node from list
51-
// O(n) time since we have to iterate over the whole list to find the new tail, and make the new tail's next point to null.
51+
// O(n) time since we have to iterate over the whole list to find the new tail,
52+
// and make the new tail's next point to null.
5253
if (!this.head) return null;
5354
const node = this.head;
5455
if (!node.next) {
@@ -115,41 +116,38 @@ class SinglyLinkedList<T> {
115116
}
116117

117118
insert(value: T, index: number, options: ListMethodOptions = { prevEnabled: false }): _Node<T> | null {
119+
if (index === 0) return this.unshift(value);
120+
if (index === this.length) return this.push(value);
118121

119-
// would be nice to refactor out the prevEnabled parts to the DoublyLinkedList insert method,
120-
// but setting the prev props for surrounding nodes has to be done one way...
121-
// * this method should be cleaned up and reorganized anyways since it's too long.
122122
const { prevEnabled } = options;
123123
let newNode = new _Node(value);
124-
if (index === 0) {
125-
// insert as head node
126-
if (prevEnabled) newNode.prev = null;
127-
newNode.next = this.head;
128-
this.head = newNode;
129-
this._length++;
130-
return newNode;
131-
}
132-
else if (index === this.length) {
133-
// insert as tail node
134-
if (prevEnabled) newNode.prev = this.tail;
135-
this.tail!.next = newNode;
136-
this.tail = newNode;
137-
}
138-
else {
139-
const prevNode = this.get(index - 1);
140-
if (!prevNode) return null; // TODO: throw indexError ? user tried to insert at out of range index.
141-
// maybe move the out of bounds errors to top of function?
142-
const targetNode = prevNode.next;
143-
if (prevEnabled) {
144-
newNode.prev = prevNode;
145-
targetNode!.prev = newNode; // assert exists because ^ insert as new tail node is covered
146-
}
147-
prevNode.next = newNode;
148-
newNode.next = targetNode;
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+
// maybe move the out of bounds errors to top of function?
127+
const targetNode = prevNode.next;
128+
if (prevEnabled) {
129+
newNode.prev = prevNode;
130+
targetNode!.prev = newNode; // assert exists because ^ insert as new tail node is covered
149131
}
132+
prevNode.next = newNode;
133+
newNode.next = targetNode;
150134
this._length++;
151135
return newNode;
152136
}
153137
}
154138

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+
// }
151+
152+
155153
export default SinglyLinkedList;

0 commit comments

Comments
(0)

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