棧(Stack)
棧的特點:
先進后出鳞尔,后進先出
棧的方法:
1push: 推入
2pop: 推出
示意圖
https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Data_stack.svg/391px-Data_stack.svg.png
javascript實現(xiàn)
const Stack = function () {
let count = 0
let storage = {}
this.push = function (value) {
storage[count] = value
count++
}
this.pop = function () {
if (count === 0) return undefined
count--
const result = storage[count]
delete storage[count]
return result
}
this.peek = function () {
return storage[count - 1]
}
this.size = function () {
return count
}
}
隊列(queue)
隊列的特點:先進先出
javascript實現(xiàn)
function Queue () {
let collection = []
this.print = function (element) {
console.log(collection)
}
this.enqueue = function (element) {
collection.push(element)
}
this.dequeue = function () {
return collection.shift()
}
this.front = function () {
return collection[0]
}
this.size = function () {
return collection.length
}
this.isEmpty = function () {
return (collection.length === 0)
}
}
優(yōu)先級隊列(PriorityQueue)
優(yōu)先級隊列特點:根據(jù)插入元素的優(yōu)先級插入指定隊列位置
javascript實現(xiàn)
function PriorityQueue () {
let collection = []
this.print = function () {
const result = collection.map(item => item[0])
console.log(result)
}
this.enqueue = function (element) {
if (this.isEmpty()) {
collection.push(element)
} else {
let added = false
for (let i = 0; i < collection.length; i++) {
if (element[1] < collection[i][1]) {
collection.splice(i, 0, element)
added = true
break // 一旦滿足插入條件跳出循環(huán)
}
}
if (!added) { // 優(yōu)先級最小的情況插入隊列
collection.push(element)
}
}
}
this.dequeue = function () {
const result = collection.shift()
return result[0]
}
this.front = function () {
const result = collection[0]
return result[0]
}
this.size = function () {
return collection.length
}
this.isEmpty = function () {
return (collection.length === 0)
}
}
Set數(shù)據(jù)結(jié)構(gòu)
Set數(shù)據(jù)結(jié)構(gòu)的特點: 不存在重復(fù)數(shù)據(jù)
Set方法:add添加嬉橙, remove刪除,size長度
javascript實現(xiàn)
const MySet = function () {
let collection = []
this.has = function (element) {
return collection.indexOf(element) !== -1
}
this.values = function () {
return collection
}
this.add = function (element) {
if (!this.has(element)) {
collection.push(element)
return true
}
return false
}
this.remove = function (element) {
if (this.has(element)) {
let index = collection.indexOf(element)
collection.splice(index, 1)
return true
}
return false
}
this.size = function () {
return collection.length
}
this.union = function (otherSet) { // 連接
let unionSet = new MySet()
let thisSetVal = this.values()
let otherSetVal = otherSet.values()
thisSetVal.forEach(item => unionSet.add(item))
otherSetVal.forEach(item => unionSet.add(item))
return unionSet
}
this.intersection = function (otherSet) { // 交集
let intersectionSet = new MySet()
let thisSetVal = this.values()
thisSetVal.forEach(item => {
if (otherSet.has(item)) {
intersectionSet.add(item)
}
})
return intersectionSet
}
// 返回thisSet不屬于otherSet的值
this.defference = function (otherSet) {
let defferenceSet = new MySet()
let thisSetVal = this.values()
thisSetVal.forEach(item => {
if (!otherSet.has(item)) {
defferenceSet.add(item)
}
})
return defferenceSet
}
// thisSet是否為otherSet的子集
this.subset = function (otherSet) {
let thisSetVal = this.values()
return thisSetVal.every(item => {
return otherSet.has(item)
})
}
}