使用js栈stack类的实现
.dataStore = [];
.top = 0;
.push = push;
.pop = pop;
.peek = peek;
.clear = clear;
.length = length;
<span style="color: #0000ff;">function<span style="color: #000000;"> push(element){
<span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top++] =<span style="color: #000000;"> element;
} <span style="color: #0000ff;">function<span style="color: #000000;"> pop(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[--<span style="color: #0000ff;">this<span style="color: #000000;">.top];
} <span style="color: #0000ff;">function<span style="color: #000000;"> peek(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top-1<span style="color: #000000;">];
} <span style="color: #0000ff;">function<span style="color: #000000;"> clear(){
<span style="color: #0000ff;">this.top = 0<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> length(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.top;
} <span style="color: #008000;">/<span style="color: #008000;">测试stack类的实现<span style="color: #008000;">/
<span style="color: #0000ff;">var s = <span style="color: #0000ff;">new<span style="color: #000000;"> stack();
s.push("aa"<span style="color: #000000;">);
s.push("bb"<span style="color: #000000;">);
s.push("cc"<span style="color: #000000;">);
console.log(s.length());<span style="color: #008000;">//<span style="color: #008000;">3
console.log(s.peek());<span style="color: #008000;">//<span style="color: #008000;">cc
<span style="color: #0000ff;">var popped =<span style="color: #000000;"> s.pop();
console.log(popped);<span style="color: #008000;">//<span style="color: #008000;">cc
console.log(s.peek());<span style="color: #008000;">//<span style="color: #008000;">bb
<span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top++] =<span style="color: #000000;"> element;
} <span style="color: #0000ff;">function<span style="color: #000000;"> pop(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[--<span style="color: #0000ff;">this<span style="color: #000000;">.top];
} <span style="color: #0000ff;">function<span style="color: #000000;"> peek(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this.dataStore[<span style="color: #0000ff;">this.top-1<span style="color: #000000;">];
} <span style="color: #0000ff;">function<span style="color: #000000;"> clear(){
<span style="color: #0000ff;">this.top = 0<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> length(){
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.top;
} <span style="color: #008000;">/<span style="color: #008000;">测试stack类的实现<span style="color: #008000;">/
<span style="color: #0000ff;">var s = <span style="color: #0000ff;">new<span style="color: #000000;"> stack();
s.push("aa"<span style="color: #000000;">);
s.push("bb"<span style="color: #000000;">);
s.push("cc"<span style="color: #000000;">);
console.log(s.length());<span style="color: #008000;">//<span style="color: #008000;">3
console.log(s.peek());<span style="color: #008000;">//<span style="color: #008000;">cc
<span style="color: #0000ff;">var popped =<span style="color: #000000;"> s.pop();
console.log(popped);<span style="color: #008000;">//<span style="color: #008000;">cc
console.log(s.peek());<span style="color: #008000;">//<span style="color: #008000;">bb
原文链接:https://www.f2er.com/js/403385.html