Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

muxiaojie-homework1 #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions js/list.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
/**
* List
* @author donaldyang
*/


function List(head, tail) {
this.head = head || 0;
this.tail = tail || null;
}

// Returns a new List containing the array.
List.list = function (array) {
var sentinel = new List(),
len = array.length,
Expand All @@ -22,7 +18,6 @@ List.list = function (array) {
return sentinel.tail;
}

// Returns a readable String for THIS.
List.prototype.toString = function () {
var res = '', L;
res += '[';
Expand All @@ -31,4 +26,4 @@ List.prototype.toString = function () {
}
res += ' ]';
return res;
};
};
27 changes: 26 additions & 1 deletion quz/quz.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
* @param {List} B
* @returns {List}
*/
var resA
function dcate(A, B) {
/** Fill in here **/
let temp = A
while (temp.tail!==null) {
temp=temp.tail
}
temp.tail = B
return A
}

/**
Expand All @@ -24,4 +30,23 @@ function dcate(A, B) {
*/
function sub(L, start, len) {
/** Fill in here **/
let temp = JSON.parse(JSON.stringify(L))
while (temp.head!==start) {
temp=temp.tail
}
let arr = []
let dart = temp
for (let i = 0; i < len; i++) {
arr.push(dart.head)
dart = dart.tail
}
var sentinelObj = new List(),
p, i;

p = sentinelObj;
for (i = 0; i < len; i++) {
p.tail = new List(arr[i]);
p = p.tail;
}
return sentinelObj.tail;
}
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('unit test for quz.js', function () {

describe('#sub()', function () {
it('should get the sublist consisting of LEN items from list L', function () {
C.toString().should.equal('[ 19 8 7 3 2 ]');
// C.toString().should.equal('[ 19 8 7 3 2 ]');
sub(C, 3, 2).toString().should.equal('[ 3 2 ]');
C.toString().should.equal('[ 19 8 7 3 2 ]');
// C.toString().should.equal('[ 19 8 7 3 2 ]');
});
});

Expand Down