function List(maxSize) {
this.maxSize = maxSize;
this.length = 0;
this.data = new Array(maxSize);
this.empty = function () {
return this.length === 0;
};
this.full = function () {
return this.length === this.maxSize;
};
this.clear = function () {
this.data = [];
this.length = 0;
};
this.get = function (idx) {
if (idx < 0 || idx > this.length) {
throw new Error("数组角标越界");
}
return this.data[idx];
};
this.insert = function (idx, value) {
if (this.full() === true) {
throw new Error("顺序表已满");
}
if (idx < 0) {
throw new Error("数组角标越界");
}
if (idx < this.length) {
for (let i = this.length; i > idx; i--) {
this.data[i] = this.data[i - 1];
}
} else {
idx = this.length;
}
this.data[idx] = value;
this.length++;
};
this.delete = function (idx) {
if (idx < 0 || idx >= this.length) {
throw new Error("数组角标越界");
}
for (let i = idx; i < this.length; i++) {
this.data[i] = this.data[i + 1];
}
this.length--;
};
this.exist = function (value) {
for (let i = 0; i < this.length; i++) {
if (this.data[i] === value) {
return true;
}
}
return false;
};
this.add = function (value) {
if (this.full() === true) {
throw new Error("顺序表已满");
}
for (let i = this.length; i > 0; i--) {
this.data[i] = this.data[i - 1];
}
this.data[0] = value;
this.length++;
};
this.append = function (value) {
if (this.full() === true) {
throw new Error("顺序表已满");
}
this.data[this.length] = value;
this.length++;
};
this.index = function (value) {
for (let i = 0; i < this.length; i++) {
if (this.data[i] === value) {
return i;
}
}
return -1;
};
}
var l = new List(10);
l.insert(0, 1)
l.insert(0, 2)
l.insert(0, 3)
l.insert(0, 4)
l.delete(0)
l.delete(4)
l.add(1)
l.append(10)
console.log(l.exist(4))
console.log(l.full());
console.log(l.length);
console.log(l.data)
console.log(l.index(1))
console.log(l.get(1))
l.clear()
console.log(l.empty())