数据结构之---双向链表(js)
单项链表和双向链表的区别

双向链表的图解


双向链表的常用方法

简单双向链表的代码实现
```javascript
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>function DoublyLinkedList() {function Node(data) {this.data = data;this.prev = null;this.next = null;}this.head = null;this.tail = null;this.length = 0;DoublyLinkedList.prototype.append = function(data) {var newNode = new Node(data);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;}DoublyLinkedList.prototype.toString = function() {var current = this.head;var linkedString = "";while (current) {linkedString += current.data + " ";current = current.next;}return linkedString;}DoublyLinkedList.prototype.forwordString = function() {var current = this.head;var linkedString = "";while (current) {linkedString += current.data + " ";current = current.next;}return linkedString;}DoublyLinkedList.prototype.backwordString = function() {var current = this.tail;var linkedString = "";while (current) {linkedString += current.data + " ";current = current.prev;}return linkedString;}DoublyLinkedList.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;}return current.data;}DoublyLinkedList.prototype.insert = function(position, data) {if (position < 0 || position > this.length) return false;var current = this.head;var newNode = new Node(data);if (this.length == 0) {this.head = newNode;this.tail = newNode; //区分:this.length==0的时候是指没有一个结点。} else {if (position == 0) { //position==0的时候 是要在第一个位置插入元素;this.head.prev = newNode;newNode.next = this.head;this.head = newNode;} else if (this.length == position) {newNode.prev = this.tail;this.tail.next = newNode;this.tail = newNode;} else {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;}DoublyLinkedList.prototype.updata = function(position, newdata) {if (position < 0 || position >= this.length) return null;var index = 0;var current = this.head;while (index++ < position) {current = current.next;}current.data = newdata;return true;}DoublyLinkedList.prototype.indexOf = function(newdata) {var index = 0;var current = this.head;while (current) {if (current.data == newdata) {return index;}current = current.next;index += 1;}return -1;}DoublyLinkedList.prototype.removeAt = function(position) {if (position < 0 || position >= this.length) return false;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) {this.tail.prev.next = null;this.tail = this.tail.prev;} else {var index = 0;var current = this.head;while (index++ < position) {current = current.next;}current.prev.next = current.next;current.next.prev = current.prev;}}this.length -= 1;return true;}DoublyLinkedList.prototype.remove = function(olddata) {var index = this.indexOf(olddata);return this.removeAt(index);}DoublyLinkedList.prototype.isEmpty = function() {return this.length == 0;}DoublyLinkedList.prototype.size = function() {return this.length;}}// 测试代码var linked = new DoublyLinkedList();linked.append("aaa");linked.append("bbb");linked.append("ccc");linked.append("ddd");// 1、toString测试代码// linked.insert(0, "ooo");alert(linked.toString());// alert(linked.forwordString());// alert(linked.backwordString());//2、get测试代码// alert(linked.get(3));// 3、insert测试代码linked.insert(4, "ooo");// linked.removeAt(2);linked.remove("aaa");// linked.updata(4, "aaa");alert(linked.toString());// alert(linked.indexOf("bbb"));alert(linked.size());</script>
</body></html>
总结:双向链表对比单项链表来说,不仅仅增加了指向上一个结点的指针,还增加了指向最后一个结点指针,这就使得双向链表的指针指向问题更加复杂,在刚开始学习双向链表的时候,建议可以用笔在纸上画出以上图解,在插入或删除操作的时候一步步改变指针的指向,这样才不会被绕晕。
以上文章部分转载于B站coderwhy老师,个人认为他的数据结构与算法讲得非常不错,通俗易懂,想要学习的同学可以上B站搜索一下哦!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
