Stack is a linear table that can be inserted or deleted only at the end of the table. Feature first-in-first-out.
The following shows the stack implemented with arrays.
Stack initialization: creates an empty stack.
Init:function(){ this.STACKMAX = 100; this.stack = new Array(this.STACKMACK); this.top = -1; return this.stack;}
Judge stack empty: True is returned if Stack is empty; otherwise, false is returned.
Empty:function(){ if(this.top==-1){ return true; } else{ return false; }}
Stack entry: If the stack is full, "stack full" is returned ". Otherwise, ELEM is used as the new top element of the stack.
Push: function (ELEM) {If (this. top = This. STACKMAX-1) {return "stack full" ;}else {This. top ++; this. stack [this. top] = ELEM ;}}
Stack return: deletes the top element of the stack and returns its value.
Pop: function () {If (this. Top =-1) {return "Empty stack, unable to delete the top element of the stack! ";}Else {var x = This. Stack [This. Top]; this. Top --; return x ;}}
Read stack top element: return stack top Element
Top: function () {If (this. Top! =-1) {return this. Stack [This. Top];} else {return "Empty stack, no return value for the top element! ";}}
Stack clearing: Empty Stack
Clear:function(){ this.top=-1;}
Stack length: return the number of elements in the stack, which is the length of the stack.
Length:function(){ return this.top+1;}
The stack sample code is as follows:
VaR stack = function () {} stack. prototype = {init: function () {This. stackmax = 100; this. stack = new array (this. stackmack); this. top =-1; return this. stack;}, empty: function () {If (this. top =-1) {return true;} else {return false ;}}, push: function (ELEM) {If (this. top = This. STACKMAX-1) {return "stack full" ;}else {This. top ++; this. stack [this. top] = ELEM ;}}, POP: function () {If (this. top =-1) {return "Empty stack, cannot be deleted Except the top element of the stack! ";}Else {var x = This. Stack [This. Top]; this. Top --; return x ;}, top: function () {If (this. Top! =-1) {return this. Stack [This. Top];} else {return "Empty stack, no return value for the top element! ";}}, Clear: function () {This. Top =-1 ;}, length: function () {return this. Top + 1 ;}}
Examples of stack applications will be provided in the recent days, so stay tuned...