来自视频学习:
一、单向链表
只能从头到尾或者从尾遍历到头
封装单向链表:
function LinkedList() {
//内部的类:节点类
function Node(data) {
this.data = data;
this.next = null
}
//属性
this.head = null;
this.length = 0;
}
常见操作
1. append(element) :向列表尾部添加一个新的项
2. insert(position,element): 向列表的特定位置插入一个新的项
3. get(position): 获取对应位置的元素
4. indexOf(element): 返回元素在列表中的索引。如果没有该元素返回-1
5. updata(position,element) : 修改某个位置的元素
6. removeAt(position):从列表的特定位置移除某一项
7. remove(element):从列表中移除一项
8. isEmpty():如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false
9. size() :返回链表包含的元素个数。与数组的length属性类似
10. toString(): 由于列表使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
链表和数组非常类似,因为链表本身就是一种可以替代数组的结构
//1。append:向列表尾部添加一个新的项
LinkedList.prototype.append = function(data) {
//1.创建新的节点
var newNode = new Node(data)
//2.判断是否添加的第一个节点
if (this.length == 0) { //2.1 是第一个节点
this.head = newNode
} else { //2.2 不是第一个节点
//找到最后一个节点
var current = this.head //先指向第一个节点
while (current.next) { //判断这个节点是否为空,如果为空,代表是最后一个节点;通过while循环一个一个判断是否为最后一个节点
current = current.next
}
//最后节点的next指向新的节点
current.next = newNode
}
//3. length+1
this.length += 1
}
// 2. toString方法 只输出链表的值
LinkedList.prototype.toString = function() {
//1.定义变量
var current = this.head
var listString = ""
//2,循环获取一个个的节点
while (current) {
listString += current.data + " "
current = current.next
}
return listString
}
//3.insert 方法
LinkedList.prototype.insert = function(position, data) {
//1.对position进行跨界判断
if (position < 0 || position > this.length) return false
//2.根据data 创建Node
var newNode = new Node(data)
//3.判断插入的位置是否是第一个
if (position == 0) {
// this.head = newNode
// newNode.next =要指向原来的第一个,如何实现?改变这两句位置
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
}
newNode.next = current
previous.next = newNode
}
//4. length+1
this.length += 1;
return true
}
//4.get 获取对应位置的元素
LinkedList.prototype.get = function(position) {
if (position < 0 || position >= this.length) return null
//2.获取对应的数据
var current = this.head;
var index = 0
// 假设position=2
// 第一次循环 index = 0, current指向第一个,
// 2. index = 1, current指向第二个,
// 3. index = 2, 不小于2
while (index++ < position)
current = current.next
return current.data
}
//5. 修改某个位置的元素
LinkedList.prototype.updata = function(position, newData) {
if (position < 0 || position >= this.length) return null
//2.
var current = this.head
var index = 0;
while (index++ < position) {
current = current.next
}
current.data = newData
return true
}
//6.从列表的特定位置移除某一项
LinkedList.prototype.removeAt = function(position) {
//1. 越界判断
if (position < 0 || position >= this.length) return null
//2.判断是否删除的是第一个节点
if (position == 0) {
this.head = this.head.next;
} else {
var index = 0;
var current = this.head;
var previous = null;
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next
}
this.length -= 1;
return true
//return current.data,把current定义在外面
}
//7.remove方法
LinkedList.prototype.remove = function(data) {
//1.获取data在列表中的位置
var position = this.indexOf(data)
//2.根据位置删除节点
return this.removeAt(position)
}
//8.isEmpty方法
LinkedList.prototype.isEmpty = function() {
return this.length == 0
}
//9.size方法
LinkedList.prototype.size = function() {
return this.length
}
二、双向链表
1. 既可以从头遍历到尾,又可以从尾遍历到头
2. 链表相连的过程是双向的
封装双向链表
//封装双向链表
function DoublyLinkedList() {
//内部类:节点类
function Node(data) {
this.data = data;
this.prev = null;
this.next = null;
}
//属性
this.head = null;
this.tail = null;
this.length = 0;
}
常见操作
1. append(element) :向列表尾部添加一个新的项
2. insert(position,element): 向列表的特定位置插入一个新的项
3. get(position): 获取对应位置的元素
4. indexOf(element): 返回元素在列表中的索引。如果没有该元素返回-1
5. updata(position,element) : 修改某个位置的元素
6. removeAt(position):从列表的特定位置移除某一项
7. remove(element):从列表中移除一项
8. isEmpty():如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false
9. size() :返回链表包含的元素个数。与数组的length属性类似
10. toString(): 由于列表使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
11. forwardString():返回正向遍历的节点字符串形式
12. backwardString():返回反向遍历的节点字符串形式
//常见的操作封装
DoublyLinkedList.prototype.append = function(data) {
//1.根据data创建节点
var newNode = new Node(data)
//2.判断添加的是否是第一个节点
if (this.length == 0) {
this.head = newNode
this.tail = newNode
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.length += 1
}
//将链表转换成字符串形式
//2.1 toString
DoublyLinkedList.prototype.toString = function() {
return this.backwardString()
}
//2.2 forwardString方法
DoublyLinkedList.prototype.forwardString = function() {
//1.定义变量
var current = this.tail;
var resultString = ""
//2.依次向前遍历,获取每一个节点
while (current) {
resultString += current.data + " ";
current = current.prev
}
return resultString
}
//2.3 backwardString方法
DoublyLinkedList.prototype.backwardString = function() {
//1. 定义变量
var current = this.head; //指向了第一个
var resultString = "";
//2.依次向后遍历,获取每一个节点
while (current) { //只要current有值
resultString += current.data + " ";
current = current.next;
}
return resultString
}
//insert
DoublyLinkedList.prototype.insert = function(position, data) {
//1. 越界判断
if (position < 0 || position > this.length) return false
//2.根据data创建新的节点
var newNode = new Node(data)
//3. 判断原来的列表是否为空
if (this.length == 0) {
this.head = newNode
this.tail = newNode
} else {
if (position == 0) { //3.1 判断position是否为0
this.head.prev = newNode; //原来的第一个节点的prev指向新插入的节点
newNode.next = this.head; //指向原来的第一个节点
this.head = newNode;
} else if (position == this.length) { //插入到最后一个节点
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
} else { //插入到中间节点
var current = this.head;
var index = 0;
while (index++ < position) {
current = current.next;
}
//修改指针
newNode.next = current;
newNode.prev = current.prev;
current.prev.next = newNode;
current.prev = newNode;
}
}
this.length += 1;
return true
}
//get方法
DoublyLinkedList.prototype.get = function(position) {
//1. 越界判断,注意后面有=
if (position < 0 || position >= this.length) return null
// 思路:this.length/2 >position 从头向后遍历
// this.length/2 <position 从后向头遍历
//2.获取元素
var index = 0;
var current = this.head;
while (index++ < position) {
current = current.next;
}
return current.data
}
//indexOf
DoublyLinkedList.prototype.indexOf = function(element) {
var current = this.head;
var index = 0;
while (current) {
if (current.data == element) {
return index
} else {
current = current.next
index += 1
}
return -1
}
}
//updata方法
DoublyLinkedList.prototype.updata = function(position, newData) {
//1. 越界判断,注意后面有=
if (position < 0 || position >= this.length) return false
var current = this.head;
var index = 0;
while (index++ < position) {
current = current.next
}
current.data = newData
return true
}
//removeAt
DoublyLinkedList.prototype.removeAt = function(position) {
//1. 越界判断,注意后面有=
if (position < 0 || position >= this.length) return null
//2.判断是否只有一个节点
var current = this.head;
if (this.length == 1) {
this.head = null
this.tail = null
} else {
//判断是否删除的是第一个节点
if (position == 0) {
this.head.next.prev = null;
this.head = this.head.next
} else if (position == this.length - 1) {
current = this.tail
this.tail.prev.next = null;
this.tail = this.tail.prev
} else {
var index = 0;
while (index++ < position) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
//3.
this.length -= 1;
return current.data
}
//remove方法
DoublyLinkedList.prototype.remove = function(data) {
//1.根据data获取下标
var index = this.indexOf(data)
//2. 根据index删除对应位置的节点
return this.removeAt(index)
}
//
DoublyLinkedList.prototype.isEmpty = function() {
return this.length == 0
}
DoublyLinkedList.prototype.size = function() {
return this.length
}
DoublyLinkedList.prototype.getHead = function() {
return this.head.data
}
DoublyLinkedList.prototype.getTail = function() {
return this.tail.data
}