(js描述的)数据结构[双向链表](5)

一.单向链表的缺点

1.只能按顺序查找,即从上一个到下一个,不能反过来。

二.双向链表的优点

1.可以双向查找

三.双向链表的缺点

1.结构较单向链表复杂。

2.占用内存比单项链表多。

四.双向链表的结构

五.双向链表的代码实现

function DoublyLinkedList() {
// 内部节点类
function Node(data) {
this.data = data
this.next = null
this.prev = null
}
//属性
this.head = null
this.tail = null
this.length = 0 // 1.append方法
DoublyLinkedList.prototype.append = function(data) {
var node = new Node(data)
if (this.length == 0) {
this.head = node
this.tail = node
} else {
node.prev = this.tail
this.tail.next = node
this.tail = node
}
this.length++
} //2.将链表转化成字符串形式
// 2.1 toString
DoublyLinkedList.prototype.toString = function() {
return this.backwardString()
}
// 2.2 forwardString
DoublyLinkedList.prototype.forwardString = function() {
var current = this.tail
var resultString = ''
while (current) {
resultString += current.data + ' '
current = current.prev
}
return resultString
}
// 2.3 backwardString
DoublyLinkedList.prototype.backwardString = function() {
var current = this.head
var resultString = ''
while (current) {
resultString += current.data + ' '
current = current.next
}
return resultString
} // 3.insert方法
DoublyLinkedList.prototype.insert = function(position, data) {
//越界判断
if (position<0 || position > this.length) return false var node = new Node(data)
if (this.length == 0) { //列表的长度为0
this.head = node
this.tail = node
} else { //列表的长度不为0
if (position == 0) { //插入第一个位置
node.next = this.head
this.head.prev = node
this.head = node
} else if (this.length == position) { //插入最后一个位置
node.prev = this.tail
this.tail.next = node
this.tail = node
} else { //插入其他位置
var current = this.head
var index = 0
while ( index++ < position) {
current = current.next
}
node.prev = current.prev
node.next = current
current.prev.next = node
current.prev = node
}
}
this.lengt++
}
//4. get方法
DoublyLinkedList.prototype.get = function(position) {
if (position<0 || position >= this.length) return false
if (this.length / 2 > position) {
var current = this.head
var index = 0
while(index++ < position) {
current = current.next
}
return current.data
} else {
var current = this.tail
var index = 0
while (index++ < this.length - position - 1) {
current = current.prev
}
return current.data
}
}
// 5. indexOf 方法
DoublyLinkedList.prototype.indexOf = function(data) {
var index = 0
var current = this.head
while (current) {
if (current.data === data) {
return index
}
current = current.next
index++
}
return -1
}
// 6.updata方法
DoublyLinkedList.prototype.updata = function(position,data) {
if (position< 0 || position >= this.length) return false var index = 0
var current = this.head
while (index++ < position) {
current = current.next
}
current.data = data
return true
}
// 7.removeAt方法
DoublyLinkedList.prototype.removeAt = function(position) {
if (position < 0 || position >= this.length) return null var current = this.head
if (this.length == 1) {
this.head =null
this.tail =null
} else {
if (position == this.length - 1) {
current = this.tail
this.tail.prev.next = null
this.tail = this.tail.prev
} else if(position == 0) {
this.head.next.prev = null
this.head = this.head.next
} else {
var index = 0
while (index++ < position) {
current = current.next
}
current.prev.next = current.next
current.next.prev = current.prev
}
}
this.length--
return current
}
// 8.remove方法
DoublyLinkedList.prototype.remove = function(data) {
var index = this.indexOf(data)
return this.removeAt(index)
}
// 9.isEmpty方法
DoublyLinkedList.prototype.isEmpty = function() {
return this.length == 0
}
// 10.size方法
DoublyLinkedList.prototype.size = function() {
return this.length
} //11.获取链表第一个元素
DoublyLinkedList.prototype.getHead = function() {
return this.head
}
//12.获取链表最后元素
DoublyLinkedList.prototype.gettail = function() {
return this.tail
}
}

(js描述的)数据结构[双向链表](5)的更多相关文章

  1. (js描述的)数据结构[哈希表1.1](8)

    (js描述的)数据结构[哈希表1.1](8) 一.数组的缺点 1.数组进行插入操作时,效率比较低. 2.数组基于索引去查找的操作效率非常高,基于内容去查找效率很低. 3.数组进行删除操作,效率也不高. ...

  2. (js描述的)数据结构[字典](7)

    (js描述的)数据结构[字典](7) 一.字典的特点 1.字典的主要特点是一一对应关系. 2.使用字典,剋通过key取出对应的value值. 3.字典中的key是不允许重复的,而value值是可以重复 ...

  3. (js描述的)数据结构[集合结构](6)

    (js描述的)数据结构[集合结构](6) 一.集合结构特点 1.集合中的元素不能重复. 2.集合是无序的. 二.集合的代码实现 function Set() { this.items = {} //1 ...

  4. (js描述的)数据结构[链表](4)

    (js描述的)数据结构 [链表](4) 一.基本结构 二.想比于数组,链表的一些优点 1.内存空间不是必须连续的,可以充分利用计算机的内存,事项灵活的内存动态管理. 2.链表不必再创建时就确定大小,并 ...

  5. (js描述的)数据结构[队列结构,优先级队列](3)

    (js描述的)数据结构[队列结构](3) 一.队列结构的特点: 1.基于数组来实现,的一种受限的线性结构. 2.只允许在表头进行删除操作,在表尾进行插入操作. 3.先进先出(FIFO) 二.队列的一些 ...

  6. (js描述的)数据结构[栈结构](2)

    (js描述的)数据结构[栈结构](2) 一.什么是栈结构 1.一种受限制的线性结构,这种结构可以基于数组来实现. 2.可以抽象成一个容器,上面的是栈顶,底下的是栈底.所以仅允许对栈顶进行操作, 二.栈 ...

  7. 用JS描述的数据结构及算法表示——栈和队列(基础版)

    前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...

  8. (js描述的)数据结构[树结构1.1](11)

    1.树结构: 我们不能说树结构比其他结构都要好,因为每种数据结构都有自己特定的应用场景. 但是树确实也综合了上面的数据结构的优点(当然有点不足于盖过其他的数据结构,比如效率一般情况下没有哈希表高) 并 ...

  9. (js描述的)数据结构[树结构之红黑树](13)

    1.二叉送搜索树的缺点: 2.红黑树难度: 3.红黑树五大规则: 4.红黑树五大规则的作用: 5.红黑树二大变换: 1)变色 2)旋转 6.红黑树的插入五种变换情况: 先声明--------插入的数据 ...

随机推荐

  1. const 详解

    ​ 简单分类:          常变量        const 类型 变量名  或者   类型 const  变量名          常引用        const 类型& 引用名   ...

  2. tomcat 对 vue的history默认支持 tomcat 开启步骤 1.build文件放入webapps目录 2.进入conf目录修改server.xml端口号改成8088 3.进入bin目录运行startup.bat 4.浏览器 localhost:8088/workName 访问即可

    tomcat 对 vue的history默认支持 tomcat 开启步骤 1.build文件放入webapps目录 2.进入conf目录修改server.xml端口号改成8088 3.进入bin目录运 ...

  3. GoJS学习笔记 (转)

    目录 基础概念 开始绘制图形 1. 通过代码构建图形 2. 通过 GraphObject.make 构建图形 3. 使用 Model 和 Templates 创建图形 获取图形数据 获取所有 Node ...

  4. asp:textbox 的 TextMode:password

    1.  用于输入或显示密码的文本框,设置属性TextMode为Password <asp:TextBox ID="txt_Password" runat="serv ...

  5. Flutter 学习路线图

    Flutter 学习路线图 如果你真的觉得很难,坚持不了了,那就放弃,既然放弃了就不要抱怨没有得到. 选择你热爱的,坚持你选择的,不抱怨放弃的. 前言 Flutter越来越火,学习Flutter的人越 ...

  6. SQL语句中,如何使用含有if....else...判断语句

    在我们开发中,有时要对数据库中的数据按照条件进行查询,用到if else类似的语句进行判断,那么if else语句只有在存储过程,触发器之类的才有,但是要在sql上当满足某种条件上要取不同的字段值,刚 ...

  7. canvas绘制折线图

    效果图: 重难点: 1.画布左上角的顶点的坐标为(0 ,0),右下角的坐标最大,与平常思维相反 2.数据的处理 html代码: <!DOCTYPE html><html lang=& ...

  8. wr720n v4 折腾笔记(三):网络配置与扩充USB

    0x01 前言 网络配置比较简单,但是USB拓展就麻烦许多了,这里由于overlay的内存分配问题导致软件安装失败,这里找到了一种方法就是直接从uboot刷入南浦月大神的wr720n的openwrt固 ...

  9. Vmware15.5安装与许可教程

    最近Windows总是提醒我1803版本的服务即将过期,劝我升级到最新版.可我在自动安装的过程中却总是安装失败.于是官网下载了更新助手.检测到的问题是升级过程和 Vmware 软件冲突,于是卸载了 V ...

  10. 一个完整的机器学习项目在Python中演练(四)

    大家往往会选择一本数据科学相关书籍或者完成一门在线课程来学习和掌握机器学习.但是,实际情况往往d是,学完之后反而并不清楚这些技术怎样才能被用在实际的项目流程中.就像你的脑海中已经有了一块块" ...