认识双向链表
单向链表:
- 只能从头遍历到尾或者从尾遍历到头(一般从头到尾)
- 也就是链表相连的过程是单向的.
- 实现的原理是上一个链表中有一个指向下一个的引用.
单向链表有一个比较明显的缺点:
- 我们可以轻松的到达下一个节点,但是回到前一个节点是很难的.但是,在实际开发中,经常会遇到需要回到上一个节点的情况
- 举个例子 :假设一个文本编辑用链表来存储文本.每一行用一个String对象存储在链表的一个节点中,当编辑器用户向下移动光标时,链表直接操作到下一个节点即可.但是当用于将光标向上移动呢?这个时候为了回到上一个节点,我们可能需要从first开始,依次走到想要的节点上。
双向链表:
- 既可以从头遍历到尾,又可以从尾遍历到头
- 也就是链表相连的过程是双向的.那么它的实现原理,你能猜到吗?
- 一个节点既有向前连接的引用,也有一个向后连接的引用.
- 双向链表可以有效的解决单向链表中提到的问题.
双向链表有什么缺点呢?
- 每次在插入或删除某个节点时,需要处理四个引用,而不是两个,也就是实现起来要困难一些
- 并且相当于单向链表,必然占用内存空间更大一些
- 但是这些缺点和我们使用起来的方便程度相比,是微不足道的.
双向链表图解
双向链表长什么样子呢?
双向链表的特点:
- 可以使用一个head和一个tail分别指向头部和尾部的节点
- 每个节点都由三部分组成:前个节点的指针(prev)/保存的元素(item)/后个节点的指针(next)
- 双向链表的第一个节点的prev是null
- 双向链表的最后的节点的next是null
双向链表结构的封装
// 封装双向链表
function DoublyLinkedList() {
// 内部类:节点类
function Node(data) {
this.data = data
this.prev = null
this.next = null
}
// 属性
this.head = null // 头部
this.tail = null // 尾部
this.length = 0
// 常见的操作:方法
}
双向链表的常见操作
我们先来认识一下, 双向链表中应该有哪些常见的操作
- append(element) :向列表尾部添加一个新的项
- insert(position, element) :向列表的特定位置插入一个新的项。
- get(position) :获取对应位置的元素
- indexOf(element) :返回元素在列表中的索引。如果列表中没有该元素则返回-1.
- update(position, element) :修改某个位置的元素
- removeAt(position) :从列表的特定位置移除项。
- remove(element) :从列表中移除项。
- isEmpty() :如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false.
- size() :返回链表包含的元素个数。与数组的length属性类似。
- toString() :由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值。
- forwardString() :返回正向遍历的节点字符串形式
- backwordString() :返回反向遍历的节点字符串形式
1.append方法
// 1.append方法
DoublyLinkedList.prototype.append = function (data) {
// 1.根据data创建节点
let 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
}
// 3.length+1
this.length += 1
}
2.insert方法
// 3. insert方法
DoublyLinkedList.prototype.insert = function (position, data) {
// 1.越界判断
if (position < 0 || position > this.length) return false
// 2.根据data创建新的节点
let 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
newNode.next = this.head
this.head = newNode
} else if (position == this.length) { // 3.2.position==length
newNode.prev = this.tail
this.tail.next = newNode
this.tail = newNode
}else { //3.3.其他情况
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
// 修改指针
newNode.next = current
newNode.prev = current.prev
current.prev.next = newNode
current.prev = newNode
}
}
// 4.length+1
this.length += 1
return true
}
3.get方法
// 4.get方法
DoublyLinkedList.prototype.get = (position) => {
// 越界判断
if (position < 0 || position > this.length) return null
// this.length / 2 > position: 从头向后遍历
// this.length / 2 < position: 从后向前遍历
// 2.获取元素
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
return current.data
}
4.indexOf方法
// 5.indexOf方法
DoublyLinkedList.prototype.indexOf = data => {
// 定义变量
let current = this.head
let index = 0
// 2.查找和data相同的节点
while (current) {
if (current.data == data) {
return index
}
current = current.next
index ++
}
return -1
}
5.update方法
// 6.update方法
DoublyLinkedList.prototype.update = (position, newData) => {
// 1.越界判断
if (position < 0 || position > this.length) return false
// 2.寻找正确的节点
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
// 3.修改找到节点的data信息
current.data = newData
return true
}
6.removeAt方法
// 7.removeAt方法
DoublyLinkedList.prototype.removeAt = position => {
// 1.越界判断
if (position < 0 || position > this.length) return null
// 2.判断是否只有一个节点
let 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) { // 最后节点
let current = this.tail
this.tail.prev.next = null
this.tail = this.tail.prev
} else {
let index = 0
while (index++ < position){
current = current.next
}
current.prev.next = current.next
current.next.prev = current.prev
}
}
// 3.length-1
this.length -= 1
return current.data
}
7.其他方法
// 封装双向链表
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方法
DoublyLinkedList.prototype.append = function (data) {
// 1.根据data创建节点
let 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
}
// 3.length+1
this.length += 1
}
// 2.将链表转成字符串形式
// 2.1. toString方法
DoublyLinkedList.prototype.toString = function () {
return this.backwardString()
}
// 2.2. forwardString方法
DoublyLinkedList.prototype.forwardString = function () {
// 1.定义变量
let current = this.tail
let resultString = ""
// 2.依次向前遍历,获取每一个节点
while (current) {
resultString += current.data + " "
current = current.prev
}
return resultString
}
// 2.3. backwardString方法
DoublyLinkedList.prototype.backwardString = function () {
// 1.定义变量
let current = this.head
let resultString = ""
// 2.依次向后遍历,获取每一个节点
while (current) {
resultString += current.data + " "
current = current.next
}
return resultString
}
// 3. insert方法
DoublyLinkedList.prototype.insert = function (position, data) {
// 1.越界判断
if (position < 0 || position > this.length) return false
// 2.根据data创建新的节点
let 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
newNode.next = this.head
this.head = newNode
} else if (position == this.length) { // 3.2.position==length
newNode.prev = this.tail
this.tail.next = newNode
this.tail = newNode
}else { //3.3.其他情况
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
// 修改指针
newNode.next = current
newNode.prev = current.prev
current.prev.next = newNode
current.prev = newNode
}
}
// 4.length+1
this.length += 1
return true
}
// 4.get方法
DoublyLinkedList.prototype.get = (position) => {
// 越界判断
if (position < 0 || position > this.length) return null
// this.length / 2 > position: 从头向后遍历
// this.length / 2 < position: 从后向前遍历
// 2.获取元素
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
return current.data
}
// 5.indexOf方法
DoublyLinkedList.prototype.indexOf = data => {
// 定义变量
let current = this.head
let index = 0
// 2.查找和data相同的节点
while (current) {
if (current.data == data) {
return index
}
current = current.next
index ++
}
return -1
}
// 6.update方法
DoublyLinkedList.prototype.update = (position, newData) => {
// 1.越界判断
if (position < 0 || position > this.length) return false
// 2.寻找正确的节点
let current = this.head
let index = 0
while (index++ < position) {
current = current.next
}
// 3.修改找到节点的data信息
current.data = newData
return true
}
// 7.removeAt方法
DoublyLinkedList.prototype.removeAt = position => {
// 1.越界判断
if (position < 0 || position > this.length) return null
// 2.判断是否只有一个节点
let 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) { // 最后节点
let current = this.tail
this.tail.prev.next = null
this.tail = this.tail.prev
} else {
let index = 0
while (index++ < position){
current = current.next
}
current.prev.next = current.next
current.next.prev = current.prev
}
}
// 3.length-1
this.length -= 1
return current.data
}
// 8.remove方法
DoublyLinkedList.prototype.remove = data => {
// 根据data获取下标值
let index = this.indexOf(data)
// 根据index删除对应位置的节点
return this.removeAt(index)
}
// 9.isEmpty方法
DoublyLinkedList.prototype.isEmpty = () => {
return this.length == 0
}
// 10.size方法
DoublyLinkedList.prototype.size = () => {
return this.length
}
// 11.获取链表的第一个元素
DoublyLinkedList.prototype.getFirst = () => {
return this.head.data
}
// 12.获取链表的最后一个元素
DoublyLinkedList.prototype.getLast = () => {
return this.tail.data
}
}
// 测试代码
let list = new DoublyLinkedList();
list.append("aaa")
list.append("bbb")
list.append("ccc")
// 2.测试转成字符串的方法
// alert(list)
// alert(list.forwardString())
// alert(list.backwardString())
// 3.测试insert方法
list.insert(0, 'ddd')
list.insert(4, 'ddd')
list.insert(2, 'ddd')
// alert(list)
// 4.测试get方法
// alert(list.get(2))
// alert(list.get(3))
// alert(list.get(1))
// 5.测试indexOf方法
// alert(list.indexOf('ccc'))
// alert(list.indexOf('abc'))
// alert(list.indexOf('bbb'))
// alert(list.indexOf('ddd'))
// 6.测试update方法
list.update(0, 'mmm')
list.update(2, 'ppp')
alert(list)
// 7.测试removeAt方法
alert(list.removeAt(2))
alert(list)
// 8.测试remove方法
alert(list.remove('aaa'))
alert(list)
alert(list.remove('bbb'))
alert(list)
// 9.其他方法
alert(list.isEmpty())
alert(list.size())
alert(list.getFirst())
alert(list.getLast())