1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| class SequenceList { constructor() { this._list = []; }
append(value) { this._list[this._list.length] = value; }
insert(index, value) { if (this._list.length === 0) { return this._list = [value]; } if (this._list.length === index) { return this._list[this._list.length] = value; } if (this._list.length < index) { return; } for (let i = 0; i < this._list.length; i++) { if (i === index) { for (let j = this._list.length; j >= i; j--) { this._list[j] = this._list[j - 1]; } this._list[index] = value; } } }
getLength() { return this._list.length; }
clear() { this._list = []; }
isEmpty() { return this._list.length === 0; }
toString() { let result = ''; for (let i = 0; i < this._list.length; i++) { result += this._list[i] + ', '; } return result; }
getItem(index) { if (index < 0 || index >= this._list.length) { return null; } return this._list[index]; }
locate(value) { for (let i = 0; i < this._list.length; i++) { if (this._list[i] === value) { return i; } } return -1; }
pop() { let last = this._list[this._list.length - 1]; this._list.length = this._list.length - 1; return last; }
remove(index) { if (index === this._list.length - 1) { let last = this._list[this._list.length - 1]; return this._list.length = this._list.length - 1; } for (let i = 0; i < this._list.length; i++) { if (i === index) { for (let j = i; j < this._list.length - 1; j++) { this._list[j] = this._list[j + 1]; } this._list.length = this._list.length - 1; return this._list; } } return this._list; } }
|