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

一.基本结构

二.想比于数组,链表的一些优点

1.内存空间不是必须连续的,可以充分利用计算机的内存,事项灵活的内存动态管理。

2.链表不必再创建时就确定大小,并且大小可无限的延申下去

3.链表再插入和删除数据时,比数组的效率高很多

三.相比于数组,链表的一些缺点

1.链表访问任何一个位置的元素时,都需要从头开始访问

2.无法通过下标直接访问元素,需要从头开始访问,直到找到对应的元素

四.链表的封装

 // 封装链表类
function LinkedList() {
// 内部的类:节点类
function Node(data) {
this.data = data
this.next = null
}
// 属性
this.head = null
this.length = 0 // 1.追加方法
LinkedList.prototype.append = function(data) {
// 1.创建新的节点
var newNode = new Node(data)
// 2.判断添加的是否是第一个节点
if (this.length == 0) { //是一个节点
this.head = newNode
} else { //不是第一个节点
// 找到最后一个节点
var current = this.head
while (current.next) {
current = current.next
}
// 把最后的节点指向新的节点
current.next = newNode
}
// 3.长度加1
this.length += 1
}
// 2.toString方法
LinkedList.prototype.toString = function() {
var current = this.head
var listString = ''
// 取到每一个节点的data值,并把之添加到一个字符串当中
while(current) {
listString += current.data + ' '
current = current.next
}
// 返回这个字符串
return listString
}
// 3.insert方法
LinkedList.prototype.insert = function(position, data) {
// 对position进行越界判断
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 previous = null
var current = this.head while (index++ < position) {
// current的为目标位置的节点,previous为目标节点的上一个节点
previous = current
current = current.next
}
newNode.next = current
previous.next = newNode
}
this.length++
return true
}
// 4.updata方法
LinkedList.prototype.updata = function(position, data) {
if (position< 0 || position> this.length - 1) {
return false
} var index = 0
var current = this.head
while (index++ < position) {
current = current.next
}
current.data = data
return true
}
// 5.get方法
LinkedList.prototype.get =function(position) {
// 1.越界判断
if (position<0 || position > this.length - 1) {
return null
}
var index = 0
var current = this.head
// 2.取到目标位置的节点
while (index++ < position) {
current = current.next
}
// 3. 把值返回
return current.data
}
// 6.indexOf方法
LinkedList.prototype.indexOf = function(data) {
var index = 0
var current = this.head
while (current) {
if (current.data == data) return index
index++
current = current.next
}
return -1
}
// 7.removeAt方法
LinkedList.prototype.removeAt = function(position) {
if (position < 0 || position >= this.length) {
return null
}
var current = this.head
if (position == 0) {
this.head = this.head.next
} else {
var index = 0
var previous = null //找到目标节点的前一个
while (index++ < position)
previous = current
current = current.next
}
previous.next = current.next
this.length--
return current
}
// 8.remove方法
LinkedList.prototype.remove = function(data) {
// 方法一
var current = this.head
var previous = null
while (current) {
if (current.data == data) {
if (previous == null) {
this.head = this.head.next
} else {
previous.next = current.next
}
return true
}
previous = current
current = current.next
}
return false // 方法二
var position = this.indexOf(data)
return this.removeAt(data)
}
// 9.isEmpty方法
LinkedList.prototype.isEmpty = function() {
return this.length == 0
}
// 10.size方法
LinkedList.prototype.size = function() {
return this.length
}
} var list = new LinkedList()
list.append('abc')
list.append('bbc')
console.log(list)

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

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

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

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

    (js描述的)数据结构[双向链表](5) 一.单向链表的缺点 1.只能按顺序查找,即从上一个到下一个,不能反过来. 二.双向链表的优点 1.可以双向查找 三.双向链表的缺点 1.结构较单向链表复杂. ...

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

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

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

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

  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. (转)ARM GNU常用汇编语言介绍

    ARM GNU常用汇编语言介绍 原文地址:http://zqwt.012.blog.163.com/blog/static/120446842010445441611/ ARM汇编语言源程序语句,一般 ...

  2. XiaoQi.Study 项目(三)

    一.配置跨域 1.首先注册跨域要求 ,(可访问的IP.端口) //注册跨域 services.AddCors(options => { options.AddPolicy("XiaoQ ...

  3. js获取按钮的文字

    button按钮有两种情况: 1 <input type="button" id="button" value="button"> ...

  4. win10安装ubuntu子系统和图形界面

    子系统可以很方便的调用windows的文件(在/mnt里就有各个盘),也可以在windows里用VScode编辑linux的文件.还是很方便的.也可以切出去用QQ微信. 安装子系统参考教程:https ...

  5. ubuntu 远程 window

    记录一下ubuntu 远程 window ubuntu先执行安装rdesktop sudo apt-get install rdesktop 终端执行: rdesktop -f  172.16.238 ...

  6. mysql刷题(不定时更新)

    面试阶段大家基本都会问一些mysql的题,具体的高深理论以后再慢慢补充,但是刷题是不可避免的,下面直接上货 创建/删除表和索引系列 创建表 CREATE TABLE if not exists `te ...

  7. Javascript的document对象

    对象属性 document.title                 //设置文档标题等价于HTML的<title>标签 document.bgColor               / ...

  8. Web_Servlet—— Servlet生命周期

    第4章 Servlet生命周期(重要) 4.1 Servlet生命周期概述 1,应用程序中的对象不仅在空间上有层次结构的关系,在时间上也会因为处于程序运行过程中的不同阶段而表现出不同的状态和不同的行为 ...

  9. jwt token认证

    目录 1.drf-jwt手动签发与校验 2.drf小组件:过滤.筛选.排序.分页 => 针对与群查接口 jwt_token源码分析(入口) 签发token源码分析 校验token源码分析 url ...

  10. hdu1015+hdu1016 都是十分钟以内的深搜题

    hdu1015:给定一串可用序列值,每个字符映射到一个1-26之间的整数,选择五个有序数使得满足 a-b2+c3-d4+e5=target. #include<iostream> #inc ...