首页 技术 正文
技术 2022年11月15日
0 收藏 941 点赞 3,240 浏览 2785 个字

(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)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844