function LinkedList() {
function Node(data) {
this.data = data;
this.next = null;
}
this.head = null;
this.len = 0;
LinkedList.prototype.append = function (data) {
let node = new Node(data)
if (this.len) {
let cur = this.head;
while (cur.next) {
cur = cur.next;
}
cur.next = node
} else {
this.head = node;
}
this.len += 1;
}
LinkedList.prototype.insert = function (ind, data) {
if (ind < 0 || ind > this.len) {
console.log('ind overflow, insert fail');
return false;
} else {
let node = new Node(data)
if (!ind) {
node.next = this.head
this.head = node
} else {
let cur = this.head
for (let i = 0; i < ind - 1; i++) {
cur = cur.next
}
node.next = cur.next
cur.next = node
}
this.len += 1
return true
}
}
LinkedList.prototype.indexOf = function (data) {
let ind = 0
let cur = this.head
while (cur) {
if (cur.data == data) {
console.log(`item ${data} found at ${ind}`);
return ind
}
ind++
cur = cur.next
}
console.log(`item ${data} not found`);
return false
}
LinkedList.prototype.getItemAt = function (ind) {
if (ind < 0 || ind > this.len - 1) {
console.log('ind overflow, item get fail');
return false;
} else {
let cur = this.head
for (let i = 0; i < ind; i++) {
cur = cur.next
}
console.log('data at ', ind, 'found:', cur.data);
return cur.data
}
}
LinkedList.prototype.delItemAt = function (ind) {
if (ind < 0 || ind > this.len) {
console.log('ind overflow, delete fail');
return false;
} else {
if (!ind) {
this.head = this.head.next
} else {
let cur = this.head
for (let i = 0; i < ind - 1; i++) {
cur = cur.next
}
cur.next = cur.next.next
}
this.len -= 1
console.log(`item at index ${ind} deleted`);
return true
}
}
LinkedList.prototype.del = function (data) {
let ind = this.indexOf(data)
if (ind) {
this.delItemAt(ind)
} else {
console.log(`item ${data} not found`);
return false
}
}
LinkedList.prototype.toString = function () {
let cur = this.head
let str = ''
while (cur) {
str += cur.data + ' ';
cur = cur.next
}
console.log(this.len + ' item(s):', str);
return str
}
}