栈作为常见的一种数据存储结构,具有很多优点。因存储方式的限制,导致栈中数据的存储只准从栈顶进入,所以不存在线性表的插入和删除时数据前后移动的问题。但也因为固定了栈底,所以常规栈有个问题就是需要在使用前,事先规定好数组存储的空间大小,万一栈溢出,就需要手动扩容,甚是麻烦。
另外,当我们有两个相同数据类型的栈,那我们就需要为它们分别开辟两块存储空间,而这其中,可能一个栈已经快满了,而另一个栈还有很多剩余空间,这在一定程度上也会造成空间浪费。
此处我们通过一个数组来存储两个栈内容,达到合理的空间分配以及数据存储。
双栈具有两个栈顶和两个栈底,两栈底分别位于数组起始处和数组尾部,两栈顶根据栈存储指针进行动态调整
双栈存储的核心思想是:两个栈数据存储分别由数组两端向数组中间靠近,只要这两个栈顶指针不重合或冲突,这两个栈就可以一直使用。
原理图如下:
首先声明基本的变量
dualstack = []// 空栈
top1 = -1 // 栈1指针
top2 = 0 // 栈2指针
rightNumID = 0 // 指针标记位
MaxLenght = 0 // 数组最大长度
双栈初始化:
/**
* 初始化栈
* @param length 初始化栈长度
*/
InitDualStack(length) {
this.dualstack = new Array(length)
this.top1 = -1
this.top2 = this.dualstack.length
this.rightNumID = 0
this.MaxLenght = this.dualstack.length
}
双栈重置:
/**
* 状态重置
* @param stackID 需要重置栈ID(默认为双栈全部重置)
*/
ResetDualStack(stackID) {
switch(stackID) {
case 1:
this.top1 = -1
break;
case 2:
this.top2 = this.MaxLenght
this.rightNumID = 0
break;
default:
this.top1 = -1
this.top2 = this.MaxLenght
this.rightNumID = 0
break;
}
console.log('左栈占用空间 ', this.top1 + 1)
console.log('右栈占用空间 ', this.MaxLenght - this.top2)
console.log('剩余空间 ', this.MaxLenght - this.top1 - (this.MaxLenght - this.top2) - 1)
}
栈顶元素插入:
/**
* 插入新元素到栈顶
* @param value 插入元素
*/
Push(stackID, value) {
if(this.top1 + 1 == this.top2) {
console.error('已满栈,无法加入元素 ', this.dualstack.length)
return
}
if (stackID == 1) {
this.dualstack[++this.top1] = value
} else if (stackID == 2) {
this.dualstack[--this.top2] = value
this.rightNumID = this.top2
}
console.log('—————————————————————————————————————————————')
console.log('左栈占用空间 ', this.top1 + 1)
console.log('右栈占用空间 ', this.MaxLenght - this.top2)
console.log('剩余空间 ', this.MaxLenght - this.top1 - (this.MaxLenght - this.top2) - 1)
}
栈顶元素移除:
/**
* 栈顶元素移除
* @param stackID 需要移除元素的栈ID
*/
Pop(stackID) {
if (stackID == 1) {
if(this.top1 == -1) {
console.error('左栈已空,无元素可移除')
return
}
var item_left = this.dualstack.splice(this.top1--,1)
this.rightNumID--
console.log('左栈移除数据 ',item_left)
} else if (stackID == 2) {
if(this.top2 == this.MaxLenght) {
console.error('右栈已空,无元素可移除')
return
}
var item_right = this.dualstack.splice(this.rightNumID,1)
this.top2++
console.log('右栈移除数据 ', item_right)
}
// this.dualstack.map((element,index) => {
// console.log('dualstack ', index, ' ', element)
// })
console.log('—————————————————————————————————————————————')
console.log('左栈占用空间 ', this.top1 + 1)
console.log('右栈占用空间 ', this.MaxLenght - this.top2)
console.log('剩余空间 ', this.MaxLenght - this.top1 - (this.MaxLenght - this.top2) - 1)
}
获取栈元素个数:
/**
* 获取栈长度
* @param stackID 指定需要获取长度的栈(默认为全栈)
*/
StackLength(stackID) {
switch(stackID) {
case 1:
console.log('right stack ', this.top1 + 1)
return this.top1 + 1
case 2:
console.log('left stack ', this.MaxLenght - this.top2)
return this.MaxLenght - this.top2
default:
console.log('all stack ', this.top1 + (this.MaxLenght - this.top2) + 1)
return this.top1 + (this.MaxLenght - this.top2) + 1
}
}
清空栈:
/**
* 清空栈元素
* @param stackID 指定需要清空的栈(默认为全栈)
*/
ClearStack(stackID) {
switch(stackID){
case 1:
var items_left = this.dualstack.splice(0,this.StackLength(1))
console.log('清除左栈元素 ', items_left)
break;
case 2:
var items_right = this.dualstack.splice(this.dualstack.length - this.StackLength(2),this.dualstack.length)
console.log('清除右栈元素 ', items_right)
break;
default:
this.dualstack.map((ele) => {
console.log(ele)
})
var items = this.dualstack.splice(0,this.MaxLenght)
console.log('清除全栈元素 ', items)
break
}
this.ResetDualStack(stackID)
}
栈判空:
/**
* 栈判空
* @param stackID 指定需要判空的栈ID
*/
isEmpty(stackID) {
switch(stackID) {
case 1:
console.log(this.top1 + 1 > 0?false:true)
return this.top1 + 1 > 0?false:true
case 2:
console.log(this.MaxLenght - this.top2 > 0? false:true)
return this.MaxLenght - this.top2 > 0? false:true
default:
console.log((this.top1 + 1 <= 0 && this.MaxLenght - this.top2 <= 0)? true : false)
return (this.top1 + 1 <= 0 && this.MaxLenght - this.top2 <= 0)? true : false
}
}
获取栈顶元素:
/**
* 获取栈顶元素
* @param stackID 指定需要返回栈顶元素的栈ID
*/
GetTop(stackID) {
switch(stackID) {
case 1:
console.log('get left top element ', this.dualstack[this.top1])
return this.dualstack[this.top1]
case 2:
console.log('get right top element ', this.dualstack[this.rightNumID])
return this.dualstack[this.top2]
default:
console.error('栈编号有误')
break;
}
}
总而言之,双栈数据存储内部空间采取此消彼长形式,如果双栈同时存入大量数据,也会导致栈溢出,因此,该存储方式通常用在两栈空间需求具有相反关系时使用。
双栈空间共享一定有一个前提为,左右两个栈存储的数据类型一定是一样的,否则这种存储方式不仅不能很好的解决问题,还会使问题变得更加复杂。