JavaScript Data Structure-stack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.