数据结构和算法
数组及单链表的缺点

单链表的优势

单链表到底是什么?

单链表封装
- 单链表常见操作

- 单链表封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单向链表</title>
</head>
<body>
<script>
function LinkedList() {
function Node(data) {
this.data = data;
this.next = null;
}
this.head = null;
this.length = 0;
LinkedList.prototype.append = function (data) {
var newNode = new Node(data);
if(this.length === 0) {
this.head = newNode;
} else {
var current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
this.length += 1;
}
LinkedList.prototype.toString = function () {
var linkStr = "";
var current = this.head;
while (current) {
linkStr += ' ' + current.data;
current = current.next;
}
return linkStr.trim();
}
LinkedList.prototype.insert = function (position, data) {
if (position < 0 || position > this.length) {
return false
}
var newNode = new Node(data);
if (position === 0) {
newNode.next = this.head;
this.head = newNode;
} else {
var index = 0;
var current = this.head;
var previous = null;
while(index < position) {
previous = current;
current = current.next;
index ++;
}
newNode.next = current;
previous.next = newNode;
}
this.length += 1;
return true;
}
LinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length) {
return null;
}
var index = 0;
var current = this.head;
while (index < position) {
current = current.next;
index ++;
}
return current;
}
LinkedList.prototype.indexOf = function (data) {
var current = this.head;
var index = 0;
while (current) {
if (current.data === data) {
return index;
}
current = current.next;
index += 1;
}
return -1;
}
LinkedList.prototype.update = function (position, data) {
if (position < 0 || position >= this.length) {
return false;
}
var current = this.head;
var index = 0;
while (index < position) {
current = current.next;
index += 1;
}
current.data = data;
}
LinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) {
return false;
}
var current = this.head;
if (position === 0) {
this.head = current.next;
} else {
var index = 0;
var prevNode = null;
while (index < position) {
prevNode = current;
current = current.next;
index += 1;
}
prevNode.next = current.next;
}
this.length -= 1;
return true;
}
LinkedList.prototype.remove = function (data) {
var current = this.head;
var prevNode = null;
var index = 0;
while (current) {
if (current.data === data) {
if (index === 0) {
this.head = current.next;
} else {
prevNode.next = current.next;
}
return true;
}
prevNode = current;
current = current.next;
index += 1;
}
return false;
}
LinkedList.prototype.isEmpty = function () {
return this.length === 0;
}
LinkedList.prototype.size = function () {
return this.length;
}
}
var list = new LinkedList();
list.append(123);
list.append(234);
console.log("list.length", list.length);
console.log("list.head", list.head);
console.log("list.toString", list.toString())
list.insert(0, "开头");
console.log("开头插入新节点", list.toString())
list.insert(2, "中间");
console.log("中间插入新节点", list.toString())
list.insert(4, "末尾");
console.log("末尾插入新节点", list.toString())
console.log(list.get(0))
console.log(list.get(1))
console.log(list.indexOf("中间"))
console.log(list.indexOf("末尾"))
console.log(list.indexOf(123))
console.log("末尾插入新节点", list.toString())
list.update(0, "hello")
console.log(list.toString())
list.removeAt(0);
console.log(list.length)
list.removeAt(4);
console.log(list.toString())
</script>
</body>
</html>