資料結構部分概念整理(未完)
阿新 • • 發佈:2020-12-23
一、對棧的操作
棧被稱為一種後入先出(LIFO,last-in-first-out)的資料結構。
對棧的主要操作:
入棧使用 push() 方法,出 棧使用 pop() 方法。
peek() 方法則只返回棧頂元素,而不刪除它。
棧的實現
function Stack() { this.dataStore = [] this.top = 0 this.push = push //入棧 this.pop = pop //出棧 this.peek = peek //返回棧頂元素 this.length = length //返回長度 } function push(item){ this.dataStore[this.top++] = item } function pop(){ return this.dataStore[--this.top] } function peek(){ return this.dataStore[this.top-1] } function length(){ return this.top } let basStack = new Stack() basStack.push('一號車') basStack.push('二號車') basStack.push('三號車') console.log(basStack.pop()) console.log(basStack.dataStore) console.log(basStack.peek()) console.log(basStack.length())
二、佇列
佇列是一種先進先出(First-In-First-Out,FIFO)的資料結構。
function Queue(){ this.dataStore = [] this.enqueue = enqueue // 向隊尾新增元素 this.delqueue = dequeue // 刪除隊首的元素 this.front = front //獲取隊首元素 this.back = back //獲取隊尾元素 this.toString = toString this.empty = empty } function enqueue(item){ this.dataStore.push(item) } function delqueue(){ return this.dataStore.unshift() } function front(item){ return this.dataStore[0] } function back(){ return this.dataStore[this.dataStore.length-1] } function toString(){ let retStr = "" for (let i = 0; i < this.dataStore.length; ++i) { retStr += this.dataStore[i] + "\n"; } return retStr } function empty(){ this.dataStore.length = 0 }
三、連結串列
JavaScript 中陣列的主要問題是,它們被實現成了物件,與其他語言(比如 C++ 和 Java) 的陣列相比,效率很低
如果你發現數組在實際使用時很慢,就可以考慮使用連結串列來替代它。除了對資料的隨機訪 問,連結串列幾乎可以用在任何可以使用一維陣列的情況中。如果需要隨機訪問,陣列仍然是 更好的選擇。
連結串列是由一組節點組成的集合。每個節點都使用一個物件的引用指向它的後繼。指向另一 個節點的引用叫做鏈。
單向連結串列
function Node(element){ this.element = element this.next = null } function LList(){ this.head = new Node("head") this.find = find this.remove = remove this.insert = insert this.display = display this.findPrevious = findPrevious } function find(item){ let currNode = this.head while( currNode.next!== null && currNode!==item){ currNode = currNode.next } return currNode } function remove(item){ let preNode = this.findPrevious(item) // console.log('preNode',preNode) preNode.next = preNode.next.next } function insert(item){ let newNode = new Node(item) let currNode = this.head while( currNode.element!== item && currNode.next !== null){ currNode = currNode.next } newNode.next = currNode.next currNode.next = newNode } function display(){ let currNode = this.head let i = 0 while( currNode.next!== null ){ currNode = currNode.next console.log('currNode',i, currNode) i++ } } function findPrevious(item){ let currNode = this.head let stop = false while( currNode.element!== item && !stop && currNode.next!== null ){ currNode = currNode.next if(currNode.next.element === item){ stop = true } } return currNode } let studentList = new LList() studentList.insert('學生1') studentList.insert('學生2') studentList.insert('學生3') studentList.insert('學生4') studentList.insert('學生5') studentList.insert('學生6') studentList.remove('學生5') studentList.display()
雙向連結串列
function Node(element){
this.element = element
this.next = null
this.previous = null
}
function LList(){
this.head = new Node('head')
this.insert = insert
this.remove = remove
this.find = find
this.display = display
}
function insert(item){
let currNode = this.head
while(currNode.next!==null){
currNode = currNode.next
}
let newNode = new Node(item)
currNode.next = newNode
newNode.previous= currNode
}
function find(item){
let currNode = this.head
while(currNode.next!==null && currNode.element !== item){
currNode = currNode.next
}
return currNode
}
function remove(item){
let currNode = this.head
while(currNode.next!==null && currNode.element !== item){
currNode = currNode.next
}
if( currNode.next === null){
currNode.previous.next = null
return
}
currNode.previous.next = currNode.next
currNode.next.previous = currNode.previous
}
function display(){
let currNode = this.head
while(currNode.next!==null){
currNode = currNode.next
console.log(currNode)
}
}
let studentList = new LList()
studentList.insert('學生1')
studentList.insert('學生2')
studentList.insert('學生3')
studentList.remove('學生2')
// studentList.display()
console.log(studentList.find('學生2'))
迴圈連結串列
function Node(element){
this.element = element
this.next = null
}
function LList(){
this.head = new Node('head')
this.head.next = this.head
this.insert = insert
this.remove = remove
this.find = find
this.display = display
this.findPrevious = findPrevious
}
function insert(item){
let currNode = this.head
while( currNode.next.element!=='head'){
currNode = currNode.next
}
let newNode = new Node(item)
newNode.next = currNode.next
currNode.next = newNode
}
function find(item){
let currNode = this.head
while( currNode.element !== item && currNode.next.element!=='head'){
currNode = currNode.next
}
return currNode
}
function remove(item){
let currNode = this.findPrevious(item)
currNode.next = currNode.next.next
}
function display(){
let currNode = this.head
while( currNode.next.element!=='head'){
currNode = currNode.next
console.log(currNode)
}
}
function findPrevious(item){
let currNode = this.head
let stop = false
while( currNode.next.element!=='head' && currNode.element !== item && !stop){
currNode = currNode.next
if(currNode.next.element === item){
stop = true
}
}
return currNode
}
let studentList = new LList()
studentList.insert('學生1')
studentList.insert('學生2')
studentList.insert('學生3')
studentList.remove('學生2')
studentList.display()
// console.log(studentList.find('學生2'))