javascript 棧 Stack

來源:互聯網
上載者:User

棧是只允許在表尾進行插入,刪除的線性表。特點後進先出。

下面將示範用數組實現的棧

棧初始化:建立一個空棧

Init:function(){   this.STACKMAX = 100;   this.stack = new Array(this.STACKMACK);   this.top = -1;   return this.stack;}

 

判斷棧空: 若棧為空白返回true,否則返回false

Empty:function(){   if(this.top==-1){       return true;   }   else{        return false;   }}

 

進棧:若棧滿,返回“棧滿”。否則將元素elem作為新的棧頂元素。

Push:function(elem){   if(this.top==this.STACKMAX-1){      return "棧滿";   }   else{      this.top++;      this.stack[this.top] = elem;   } }

 

退棧:刪除棧頂元素,並返回其值

Pop:function(){    if(this.top==-1){        return "空棧,無法刪除棧頂元素!";    }    else{        var x = this.stack[this.top];        this.top--;        return x;    } }

 

讀棧頂元素:返回棧頂元素

Top:function(){    if(this.top!=-1){        return this.stack[this.top];    }    else{        return "空棧,頂元素無傳回值!";     } }

 

清空棧:將棧清空為空白棧

Clear:function(){    this.top=-1;}

 

棧長度:返回棧的元素個數,既棧的長度

Length:function(){   return this.top+1;}

棧範例程式碼如下:

            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 "棧滿";                    }                    else{                        this.top++;                        this.stack[this.top] = elem;                    }                },                Pop:function(){                    if(this.top==-1){                        return "空棧,無法刪除棧頂元素!";                    }                    else{                        var x = this.stack[this.top];                        this.top--;                        return x;                    }                },                Top:function(){                    if(this.top!=-1){                        return this.stack[this.top];                    }                    else{                        return "空棧,頂元素無傳回值!";                    }                },                Clear:function(){                    this.top=-1;                },                Length:function(){                    return this.top+1;                }            }

 

在最近的日子裡會給出棧的應用的例子,在此敬請期待。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.