GitHub Blog Address
Stack, aka stacks, is a linear table in which operations are limited. Follow the LIFO principle, like a trash can.
Functional implementation is still in accordance with additions and deletions, internal data storage can borrow the language natively supported arrays.
Stack class
function Stack () {this.data = [];}
Add data
Data is added to the end
Push:function (Element) {This.data.push (element);}
Delete data
Remove from end
Pop:function () {This.data.pop ();}
Get Data
Returns the last added
Peek:function () {return this.data[this.size ()-1];}
is empty
Isempty:function () {return this.data.length = = 0;}
Clear Data
Clear:function () {this.data= [];}
Data length
Size:function () {return this.data.length;}
Auxiliary functions, printing data
Print:function () {Console.log (this.data.toString ());}
Full code
1 functionStack () {2 This. data = [];3 }4Stack.prototype = {5Pushfunction(Element) {6 This. Data.push (element);7 },8Popfunction (){9 This. Data.pop ();Ten }, OnePeek:function (){ A return This. data[ This. data.length-1]; - }, -IsEmpty:function (){ the return This. Data.length = = 0; - }, -Clearfunction (){ - This. data= []; + }, -Sizefunction (){ + return This. data.length; A }, atPrintfunction (){ -Console.log ( This. data.tostring ()); - } -}View Code
JavaScript Data Structure-stack