【数据结构】栈Stack
1、栈(Stack)是限定仅在表尾进行插入或删除操作的线性表。 栈的顺序存储结构结构1、用数组下标为0的一端作为栈底比较好,因为首元素都存在栈底,变化最小。 function Stack() {
this.dataStore = []; /* 用于储存栈元素的数组 */
this.top = -1; /* 用于栈顶指针 */
}
操作Stack.prototype = {
constructor: Stack,push : function (data) { /* 入栈方法 */
},pop : function () { /* 出栈方法 */
},peek: function () { /* 返回顶部的栈元素 */
},clear: function () { /* 清除栈元素 */
},length: function () { /* 返回栈元素个数 */
}
};
push方法实现栈中向top位置插入元素后,top值更新,元素个数+1。 function push (data) {
this.dataStore[++this.top] = data;
}
pop方法实现元素出栈,top值减1,指向当前栈顶元素下标位置,删除该元素作为出栈操作。 function pop() {
var topvalue = this.dataStore[this.top];
delete this.dataStore[this.top--];
return topvalue;
}
peek方法实现peek方法返回顶部的栈元素,即取dataStore的最后一个位置的值,也就是 dataStore 下标为 top-1 的值。 function peek() {
return this.dataStore[this.top];
}
clear方法实现1、clear 方法清除栈内元素,把栈的长度重置为0。 function clear () {
this.top = -1;
//下面两句任选其一清除数组dataStore里的数据
// this.dataStore = [];
this.dataStore.length = 0;
}
length 方法实现length 方法获取栈中的元素个数。 function length() {
return this.top+1;
}
测试代码var a = new Stack();
a.push(1);
a.push(2);
a.push(3);
console.log("开始的dataStore为:" + a.dataStore + " 栈的长度为:" + a.length());
/* 输出:开始的dataStore为:1,2,3 栈的长度为:3 */
a.pop();
console.log("出栈一个值后dataStore为:" + a.dataStore + " 栈的长度为:" + a.length());
/* 输出:出栈一个值后dataStore为:1,栈的长度为:2 */
a.clear();
console.log("清除栈后dataStore为:" + a.dataStore + " 栈的长度为:" + a.length());
/* 输出:清除栈后dataStore为: 栈的长度为:0 */
栈的链式存储结构结构1、由于单链表有头指针,所以最好的办法就是把栈顶放在单链表的头部。 function Stack() {
this.top = null; /* 用于栈顶指针 */
this.length = 0; /* 用于表示栈的长度 */
}
操作Stack.prototype = {
constructor: Stack,push : function (element) { /* 入栈方法 */
},length: function () { /* 返回栈元素个数 */
}
};
push方法实现栈中向top栈顶插入元素后,top值更新,元素个数+1。 function push (data) {
var node = {
data: data,next: null
};
node.next = this.top;
this.top = node;
this.size++;
}
pop方法实现元素出栈,size值减1,存储要删除的栈顶元素,将栈顶指针下移一位。 function pop() {
if (this.top === null)
return null;
var out = this.top;
this.top = this.top.next;
if (this.size > 0)
this.size--;
return out.data;
},
peek方法实现peek方法返回顶部的栈元素。 function peek() {
return this.top === null ?
null :
this.top.data;
}
clear方法实现clear 方法清除栈内元素,把栈的长度重置为0。 function clear () {
this.top = null;
this.size = 0;
}
length 方法实现length 方法获取栈中的元素个数。 function length() {
return this.size;
}
测试代码/* 给链栈增加一个展示函数 */
function displayAll(){
if (this.top === null)
return null;
var arr = [];
var current = this.top;
for (var i = 0,len = this.size; i < len; i++) {
arr[i] = current.data;
current = current.next;
}
return arr;
}
var a = new Stack();
a.push(1);
a.push(2);
a.push(3);
console.log("开始的dataStore为:" + a.displayAll()+ " 栈的长度为:" + a.length());
/* 输出:开始的栈为:3,1 栈的长度为:3 */
a.pop();
console.log("出栈一个值后dataStore为:" + a.displayAll()+ " 栈的长度为:" + a.length());
/* 输出:出栈一个值后栈为:2,1 栈的长度为:2 */
a.clear();
console.log("清除栈后dataStore为:" + a.displayAll()+ " 栈的长度为:" + a.length());
/* 输出:清除栈后栈为: 栈的长度为:0 */
栈的应用四则运算表达式求值后缀(逆波兰)表示法1、不需要括号的后缀表示,所有的符号在运算数字的后面出现。
中缀表达式转后缀表达式1、中缀表达式:平时所用的标准四则运算表达式。
因此,要让计算机处理中缀表达式,最重要的就是两步:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |