链表
一、概念 
数组
要存储多个元素,数组可能是最常用的数据结构。
几乎每一种编程语言都有默认实现数组结构。
数组的缺点:
数组的创建通常需要申请一段连续的内存空间(整块的内存),并且大小是固定的(大多数编程语言数组都是固定的);
所以当 当前数组 不能满足容量需求时,需要扩容(一般情况下是申请—个更大的数组,比如2倍。然后将原数组中的元素复制过去)。
而且在数组开头或中间位置插入数据的成本很高,需要进行大量元素的位移。
尽管我们已经学过的 JavaScript 的Array类方法可以帮我们做这些事,但背后的原理依然是这样。
链表结构
不同于数组,链表中的元素在内存中不必是连续的空间。
链表的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(有些语言称为指针或者连接)组成。
优点:
内存空间不是必须连续的。可以充分利用计算机的内存,实现灵活的内存动态管理。
链表不必在创建时就确定大小,并且大小可以无限的延伸下去。
链表在插入和删除数据时,时间复杂度可以达到O(1)。相对数组效率高很多。
结构模型
head -> { data, next } -> { data, next } -> { data, next } -> null;
head 指向头部节点;
next 指向下一个节点;
data 当前节点数据
二、属性和方法 链表的属性:
head: 头部节点
node: 节点
data: 节点数据
next:指向下一个节点的指针
length: 链表长度
链表的方法(6个):
append(data):向链表尾部添加一个新的项
insert(position, data):向链表的特定位置 插入一个新的项
get(position):获取对应位置的元素
indexOf(data):返回元素在链表中的索引。如果链表中没有该元素,则返回-1
update(position, data):修改某个位置的元素
removeAt(position):从链表的特定位置删除一项
remove(data):从列表中删除一项
isEmpty():如果链表中不包含任何元素,返回true;否则返回false
size():返回链表中包含的元素个数
toString():将链表中的内容,转换成字符串形式
具体实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 function LinkedList ( ) { function Node (data ) { this .data = data; this .next = null ; } this .head = null ; this .length = 0 ; LinkedList .prototype .append = function (data ) { const node = new Node (data); if (this .length === 0 ) { this .head = node; } else { let currentNode = this .head ; while (currentNode.next ) { currentNode = currentNode.next ; } currentNode.next = node; } this .length ++; } LinkedList .prototype .insert = function (position, data ) { if (position < 0 || position > this .length ) { return false } const node = new Node (data); if (position === 0 ) { node.next = this .head ; this .head = node; } else { let currentNode = this .head ; let prevNode = null ; let index = 0 ; while (index++ < position) { prevNode = currentNode; currentNode = currentNode.next ; } node.next = currentNode; prevNode.next = node; } this .length ++; return true ; } LinkedList .prototype .get = function (position ) { if (position < 0 || position >= this .length ) { return null } let currentNode = this .head ; let index = 0 ; while (index++ < position) { currentNode = currentNode.next ; } return currentNode.data ; } LinkedList .prototype .indexOf = function (data ) { let currentNode = this .head ; let index = 0 ; while (currentNode) { if (data === currentNode.data ) { return index; } currentNode = currentNode.next ; index++; } return -1 ; } LinkedList .prototype .update = function (position, data ) { if (position < 0 || position >= this .length ) { return false } let currentNode = this .head ; let index = 0 ; while (index++ < position) { currentNode = currentNode.next ; } currentNode.data = data; return true ; } LinkedList .prototype .removeAt = function (position ) { if (position < 0 || position >= this .length ) { return false } let currentNode = this .head ; if (position === 0 ) { this .head = this .head .next ; } else { let index = 0 ; let prevNode = null ; while (index++ < position) { prevNode = currentNode; currentNode = currentNode.next ; } prevNode.next = currentNode.next ; } this .length --; return currentNode.data ; } LinkedList .prototype .remove = function (data ) { const postion = this .indexOf (data); return this .removeAt (postion); } LinkedList .prototype .isEmpty = function ( ) { return this .length === 0 ; } LinkedList .prototype .size = function ( ) { return this .length } LinkedList .prototype .toString = function ( ) { let current = this .head ; let listString = '' ; while (current) { listString += current.data + ' ' ; current = current.next ; } return listString; } }
Post title : 链表
Post author : Chao
Create time : 2019-10-08 14:45:00
Post link : 2019/10/08/链表/
Copyright notice : All articles in this blog are licensed under BY-NC-SA unless stating additionally.
$tools-item-width = 2.2rem
$tools-item-font-size = 1.1rem
$tools-item-border-radius = 0.1rem
.side-tools-container {
position relative
.tools-item {
width $tools-item-width
height $tools-item-width
margin-bottom 0.2rem
color var(--text-color-3)
font-size $tools-item-font-size
background var(--background-color-1)
border-right none
border-radius $tools-item-border-radius
box-shadow 0.1rem 0.1rem 0.2rem var(--shadow-color)
cursor pointer
i {
color var(--text-color-3)
}
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
i {
color var(--background-color-1)
}
}
+keep-tablet() {
width $tools-item-width * 0.9
height $tools-item-width * 0.9
margin-bottom 0.2rem
font-size $tools-item-font-size * 0.9
}
&.rss {
a {
width 100%
height 100%
border-radius $tools-item-border-radius
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
}
}
}
}
.side-tools-list {
transform translateX(100%)
opacity 0
transition-t("transform, opacity", "0, 0", "0.2, 0.2", "linear, linear")
&.show {
transform translateX(0)
opacity 1
}
}
.exposed-tools-list {
if (hexo-config('style.scroll.percent') == true) {
.tool-scroll-to-top {
display none
&.show {
display flex
}
&:hover {
.percent {
display none
}
.arrow-up {
display flex
}
}
.arrow-up {
display none
}
.percent {
display flex
font-size 1rem
}
}
}
}
}
$icon-size = 1.2rem
$search-header-height = 3rem
.search-pop-overlay {
position fixed
top 0
left 0
z-index $z-index-8
display flex
width 100%
height 100%
background rgba(0, 0, 0, 0)
visibility hidden
transition-t("visibility, background", "0, 0", "0.3, 0.3", "ease, ease")
&.active {
background rgba(0, 0, 0, 0.35)
visibility visible
.search-popup {
transform scale(1)
}
}
.search-popup {
z-index $z-index-6
width 70%
height 80%
margin auto
background var(--background-color-1)
border-radius 0.4rem
transform scale(0)
transition-t("transform", "0", "0.3", "ease")
+keep-tablet() {
width 80%
}
+keep-mobile() {
width 90%
}
.search-header {
display flex
align-items center
height $search-header-height
padding 0 1rem
background var(--text-color-6)
border-top-left-radius 0.2rem
border-top-right-radius 0.2rem
.search-input-field-pre {
margin-right 0.2rem
color var(--text-color-3)
font-size 1.3rem
cursor pointer
}
.search-input-container {
flex-grow 1
padding 0.2rem
.search-input {
width 100%
color var(--text-color-3)
font-size 1.2rem
background transparent
border 0
outline 0
&::-webkit-search-cancel-button {
display none
}
&::-webkit-input-placeholder {
color var(--text-color-4)
font-size 1rem
}
}
}
.close-popup-btn {
color var(--text-color-3)
font-size $icon-size
cursor pointer
&:hover {
color var(--text-color-1)
}
}
}
#search-result {
position relative
display flex
box-sizing border-box
height 'calc(100% - %s)' % $search-header-height
padding 0.3rem 1.5rem
overflow auto
.search-result-list {
width 100%
height 100%
font-size 1rem
li {
box-sizing border-box
margin 0.8rem 0
padding 0.8rem 0
border-bottom 0.1rem dashed var(--border-color)
&:last-child {
border-bottom none
}
.search-result-title {
position relative
display flex
align-items center
margin-bottom 0.8rem
padding-left 1rem
font-weight bold
&::after {
position absolute
top 50%
left 0
width 0.4rem
height 0.4rem
background var(--text-color-3)
border-radius 50%
transform translateY(-50%)
content ''
}
}
.search-result {
margin 0
padding-left 1rem
line-height 2rem
word-wrap break-word
}
a {
&:hover {
color var(--text-color-3)
}
}
.search-keyword {
color var(--primary-color)
font-weight bold
border-bottom 0.1rem dashed var(--primary-color)
}
}
}
#no-result {
margin auto
color var(--text-color-4)
}
}
}
}