import { ctrlbarStore } from '../store' // mobx data
/**
* 雙向鏈表
*/
class Node {
constructor (key) {
this.prev = null
this.key = key
this.element = {}
this.next = null
}
clear () {
this.value = {}
}
// append 添加元素到 element
append (key, value) {
this.element[key] = value
}
forEach (fn) {
const keys = Object.keys(this.element)
keys.forEach(k => {
fn(k, this.element[k])
})
}
}
class DoublyLinkedList {
constructor () {
this.length = 0
this.head = null
this.tail = null
}
// append 添加元素到尾部
append (key) {
if (!key) throw new Error('key is required')
const node = new Node(key)
if (!this.head) {
this.head = this.tail = node
} else {
let cur = this.head
while (cur && cur.key <= key) {
if (cur.next) {
cur = cur.next
} else {
break
}
}
if (cur.next) {
node.next = cur.next
node.prev = cur
cur.next = node
node.next.prev = node
} else {
this.tail = node
node.prev = cur
cur.next = node
}
}
this.length += 1
ctrlbarStore.canvasCount = this.length
}
getNode (key) {
if (!key) return null
let cur = this.head
let node = null
while (!!cur && !node) {
if (cur.key === key) {
node = cur
} else {
cur = cur.next
}
}
return node
}
getIndexFromKey (key) {
if (!key) return 0
let cur = this.head
let index = 0
while (true) {
index += 1
if (cur.key === key) break
if (!cur.next) {
index = 0
break
}
cur = cur.next
}
return index
}
removeNode (key) {
if (!key) return null
let cur = this.head
let node = null
while (!!cur && !node) {
if (cur.key === key) {
node = cur
} else {
cur = cur.next
}
}
if (!node) return
this.length -= 1
node.prev.next = node.next
if (!node.next) this.tail = node.prev
if (this.head.key === node.key) this.head = null
}
}
export { Node, DoublyLinkedList }
export const canvasStore = new DoublyLinkedList()
前端-定制化鏈表
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門墨叛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來石窑,“玉大人炫欺,你說我怎么就攤上這事胖笛⊥郑” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵长踊,是天一觀的道長功舀。 經(jīng)常有香客問我,道長身弊,這世上最難降的妖魔是什么辟汰? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮阱佛,結(jié)果婚禮上帖汞,老公的妹妹穿的比我還像新娘。我一直安慰自己凑术,他們只是感情好翩蘸,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著淮逊,像睡著了一般催首。 火紅的嫁衣襯著肌膚如雪扶踊。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼尺锚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了惜浅?” 一聲冷哼從身側(cè)響起缩麸,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赡矢,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阅仔,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡吹散,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了八酒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片空民。...
- 正文 年R本政府宣布浊猾,位于F島的核電站,受9級特大地震影響热鞍,放射性物質(zhì)發(fā)生泄漏葫慎。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一薇宠、第九天 我趴在偏房一處隱蔽的房頂上張望偷办。 院中可真熱鬧,春花似錦澄港、人聲如沸椒涯。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽废岂。三九已至祖搓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間泪喊,已是汗流浹背棕硫。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像蚓再,于是被迫代替她去往敵國和親滑肉。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 六年,你又開演唱會了娃属。 我不知道第一次喜歡周董是什么時(shí)候六荒,只知道他的《稻香》真的香飄十里,彌漫了我整個(gè)童年...
- 人的價(jià)值無論是在農(nóng)業(yè)時(shí)代矾端,工業(yè)時(shí)代掏击,還是互聯(lián)網(wǎng)時(shí)代,其本質(zhì)是不變的秩铆,都是靠人來傳播砚亭。古時(shí)候叫揚(yáng)名立萬。現(xiàn)在也...