Javascript stack Stack

Source: Internet
Author: User

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...

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.