Skip to content

Commit 3bcce49

Browse files
committed
update
1 parent b6a4ce2 commit 3bcce49

File tree

3 files changed

+84
-77
lines changed

3 files changed

+84
-77
lines changed

src/List/LinkedList.js

+13-57
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Node {
1010
}
1111

1212
export default class LinkedList {
13-
constructor(sqList){
13+
constructor(sqList = []){
1414
this.head = null;
1515
this.tail = null;
1616

@@ -20,6 +20,12 @@ export default class LinkedList {
2020
}
2121
}
2222

23+
/**
24+
* merge list, note: this operation will delete a and b nodes.
25+
* @param {LinkedList} a
26+
* @param {LinkedList} b
27+
* @param {*} compare
28+
*/
2329
static mergeList (a, b, compare = compFn) {
2430
let ha = a.head;
2531
let hb = b.head;
@@ -233,6 +239,11 @@ export default class LinkedList {
233239
return str;
234240
}
235241

242+
/**
243+
* insert element by order
244+
* @param {*} data
245+
* @param {Function} cmp
246+
*/
236247
orderInsert (data, cmp) {
237248
cmp = typeof cmp === 'function' ? cmp : (a, b) => {
238249
if (a > b)
@@ -335,6 +346,7 @@ export default class LinkedList {
335346
let p = this.head;
336347
let q = p.next;
337348
let s = q.next;
349+
this.tail = p;
338350
p.next = null;
339351

340352
while (s.next) {
@@ -438,56 +450,6 @@ function intersect_delete(list, b, c) {
438450
list.tail = r;
439451
}
440452

441-
var list = new LinkedList();
442-
list.push('b');
443-
list.unshift('a');
444-
list.insertAfter('b', 'c');
445-
console.log(list.item(2));
446-
console.log(JSON.stringify(list));
447-
list.each(function (node) {
448-
if (node.data === 'b') {
449-
console.log('get b in each');
450-
}
451-
});
452-
list.remove('c');
453-
list.remove('a');
454-
console.log(list);
455-
456-
let list2 = new LinkedList();
457-
list2.push('c');
458-
list2.unshift('d');
459-
list2.insertAfter('d', 'b');
460-
console.log(JSON.stringify(list2));
461-
462-
let list3 = LinkedList.mergeList(list, list2);
463-
console.log(list3);
464-
465-
466-
var list = new LinkedList();
467-
468-
list.orderInsert(5);
469-
list.orderInsert(2);
470-
list.orderInsert(3);
471-
list.orderInsert(1);
472-
list.orderInsert(4);
473-
list.orderInsert(4);
474-
list.orderInsert(6);
475-
list.orderInsert(6);
476-
list.orderInsert(7);
477-
478-
list.delete_between(5, 8);
479-
console.log('delete-between: ');
480-
console.log(list);
481-
482-
list.orderInsert(2);
483-
list.orderInsert(3);
484-
list.orderInsert(1);
485-
486-
list.delete_equal();
487-
console.log(list);
488-
489-
list.reverse();
490-
console.log(list);
491453

492454
let a = new LinkedList();
493455
a.orderInsert(1);
@@ -524,9 +486,3 @@ test.orderInsert(9);
524486

525487
intersect_delete(test, a, b);
526488
console.log(test);
527-
528-
var popTest = new LinkedList();
529-
popTest.push(1);
530-
popTest.push(2);
531-
popTest.pop();
532-
popTest.pop();

src/List/Polynomial.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
var List = require('./complete-LinkedList');
22

3-
function Term(coef, expn) {
4-
// 系数
5-
this.coef = coef || null;
6-
// 指数
7-
this.expn = expn || null;
3+
class Term {
4+
constructor(coef = null, expn = null){
5+
// 系数
6+
this.coef = coef;
7+
// 指数
8+
this.expn = expn;
9+
}
810
}
911

1012
// 一元多项式
11-
function Polynomial() {
12-
List.call(this);
13-
}
14-
Polynomial.prototype = {
15-
__proto__: List.prototype,
13+
class Polynomial extends List {
14+
constructor(...args) {
15+
super(...args);
16+
}
1617

17-
locateElem: function (elem, compare) {
18+
locateElem(elem, compare) {
1819
var current = this.head;
1920
var prev = current;
2021
var obj;
@@ -37,23 +38,23 @@ Polynomial.prototype = {
3738
}
3839

3940
return obj;
40-
},
41-
initList: function () {
41+
}
42+
initList() {
4243
this.head = List.makeNode();
4344
this.head.data = new Term();
4445
this.tail = this.head;
45-
},
46-
cmp: function (a, b) {
46+
}
47+
cmp(a, b) {
4748
if (a.expn < b.expn) {
4849
return -1;
4950
} else if (a.expn === b.expn) {
5051
return 0;
5152
} else {
5253
return 1;
5354
}
54-
},
55+
}
5556
// 输入m项的系数和指数,建立表示一元多项式的有序链表p
56-
createPolyn: function (elems, elems2) {
57+
createPolyn(elems, elems2) {
5758
var m = elems.length;
5859
this.initList();
5960
var h = this.head;
@@ -72,9 +73,9 @@ Polynomial.prototype = {
7273

7374
e = {};
7475
}
75-
},
76+
}
7677
// 多项式加法,a = a + b
77-
addPolyn: function (b) {
78+
addPolyn(b) {
7879
var a = this;
7980
// ha, hb分别指向头结点
8081
var ha = a.head;
@@ -125,7 +126,8 @@ Polynomial.prototype = {
125126
a.append(qb);
126127
}
127128
}
128-
};
129+
}
130+
129131

130132
var test = new Polynomial();
131133
test.createPolyn([-1, 2, 4], [1, 2, 3]);

test/List/LinkedList.js

+49
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ describe('linkedList tests', function () {
1010
assert.equal(list.head.data, 'b');
1111
assert.equal(list.tail.next, null);
1212

13+
list.push('c');
14+
list.pop();
15+
assert.equal(list.head.data, 'b');
16+
assert.equal(list.tail.next, null);
17+
1318
list.unshift('a');
1419
assert.equal(list.head.data, 'a');
1520
assert.equal(list.head.next.data, 'b');
@@ -70,5 +75,49 @@ describe('linkedList tests', function () {
7075
list.each(function (node, index) {
7176
assert.equal(node.data, arr[index]);
7277
});
78+
79+
for (let [index, node] of list){
80+
assert.equal(node.data, arr[index]);
81+
}
82+
});
83+
84+
var list = new List();
85+
86+
it('LinkedList orderInsert', function(){
87+
list.orderInsert(5);
88+
list.orderInsert(2);
89+
list.orderInsert(3);
90+
list.orderInsert(1);
91+
list.orderInsert(4);
92+
list.orderInsert(4);
93+
list.orderInsert(6);
94+
list.orderInsert(6);
95+
list.orderInsert(7);
96+
97+
var result = new List([1, 2, 3, 4, 4, 5, 6, 6, 7]);
98+
99+
assert.deepEqual(list, result);
73100
});
101+
102+
it('LinkedList delete_between', function(){
103+
list.delete_between(5, 8);
104+
105+
var result = new List([1, 2, 3, 4, 4, 5]);
106+
assert.deepEqual(list, result);
107+
});
108+
109+
it('LinkedList delete_equal', function () {
110+
list.delete_equal();
111+
112+
var result = new List([1, 2, 3, 4, 5]);
113+
assert.deepEqual(list, result);
114+
});
115+
116+
it('LinkedList reverse', function () {
117+
list.reverse();
118+
119+
var result = new List([5, 4, 3, 2, 1]);
120+
assert.deepEqual(list, result);
121+
});
122+
74123
});

0 commit comments

Comments
 (0)