亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript棧和隊列相關(guān)操作與實現(xiàn)方法詳解

 更新時間:2018年12月07日 08:41:10   作者:hlinghoney  
這篇文章主要介紹了JavaScript棧和隊列相關(guān)操作與實現(xiàn)方法,結(jié)合實例形式較為詳細的分析了javascript棧和隊列的概念、原理、定義、用法及相關(guān)操作注意事項,需要的朋友可以參考下

本文實例講述了JavaScript棧和隊列相關(guān)操作與實現(xiàn)方法。分享給大家供大家參考,具體如下:

一、棧的介紹

棧就是和列表類似的一種數(shù)據(jù)結(jié)構(gòu),數(shù)據(jù)只能在棧頂添加或者刪除。棧是一種特殊的列表,棧內(nèi)的元素只能通過列表的一端訪問,成為棧頂。棧具有后進先出的特點,所以任何不在棧頂?shù)脑囟紵o法訪問。

后進先出(LIFO,last-in-first-out)的數(shù)據(jù)結(jié)構(gòu)。

對棧的操作

1.對棧的兩種主要操作為將一個元素壓入棧和將一個元素彈出棧。

入棧:push();
出棧:pop();

2.預覽棧頂?shù)脑?code>peek();

pop()雖然可以訪問棧頂元素,但調(diào)用后,棧頂元素也從棧中永久性的被刪除。peek()方法只返回棧頂元素,并不刪除它。

對棧的實現(xiàn)

定義stack類的構(gòu)造函數(shù):

function Stack(){
 this.dataStore=[];//數(shù)組dataStore保存棧內(nèi)元素,初始化為空數(shù)組
 this.top=0;
 //top為棧頂位置,被構(gòu)造函數(shù)初始化為0,表示棧頂對應(yīng)數(shù)組的起始位置0
 this.push=push;
 this.pop=pop;
 this.peek=peek;
}

實現(xiàn)push()方法:

function push(element){
 this.dataStore[this.top++]=element;
}

實現(xiàn)pop()方法:

function pop(element){
 return this.dataStore[--this.top];
 //pop方法與push方法相反,它返回棧頂元素,同時將變量top的值減1
}

實現(xiàn)peek()方法:

function peek(element){
 return this.dataStore[this.top-1];
 //peek方法返回數(shù)組的第top-1個位置的元素,即棧頂元素。
}

如果對一個空棧調(diào)用peek()方法,結(jié)果為undefined,因為棧是空的,棧頂沒有任何元素。

實現(xiàn)length()

需要知道棧內(nèi)存儲了多少元素,length()方法通過返回變量top值得方法返回棧內(nèi)的元素個數(shù)。

function length(){
 return this.top();
}

實現(xiàn)clear()

clear()將變量top的值設(shè)置為0,清空一個棧:

function clear(){
 this.top=0;
}

總結(jié):Stack類

function stack(){
 this.dataStore=[];
 this.top=0;
 this.push=push;
 this.pop=pop;
 this.peek=peek;
 this.clear=clear;
 this.length=length;
}
function push(element){
 this.dataStore[this.top++]=element;
}
function peek(){
 return this.dataStore[this.top-1];
}
function pop(){
 return this.dataStore[--this.top];
}
function clear(){
 this.top=0;
}
function length(){
 return this.top;
}

二、隊列

隊列是一種列表,隊列智能在隊尾插入元素,在隊首刪除元素。隊列用于存儲按順序排列的數(shù)據(jù),先進先出。

對隊列的操作

隊列主要兩種操作,入隊和出隊,入隊是在隊尾插入新元素,出隊是刪除隊首的元素。另一種是讀取隊頭的元素,peek();

push()在數(shù)組末尾添加元素

names=[];
names.push("hling");
names.push("aling");
print(names); //顯示hling,aling

shift()刪除數(shù)組中第一個元素

names.shift();
print(names); //顯示aling

定義Queue

function Queue(){
 this.dataStore=[];
 this.enqueue=enqueue;
 this.dequeue=dequeue;
 this.front=front;
 this.back=back;
 this.toString=toString;
 this.empty=empty;
}

enqueue()向隊尾添加一個元素

function enqueue(element){
 this.dataStore.push(element);
}

dequeue()向隊尾添加一個元素

function dequeue(element){
 return this.dataStore.shift(element);
}

讀取隊首和隊尾的元素

function front(){
 return this.dataStore[0];
}
function back(){
 return this.dataStore[this.dataStore.length-1];
}

toString()顯示隊列內(nèi)的所有元素

function toString(){
 var retStr="";
 for(var i=0;i<this.dataStore.length;i++){
  retStr+=this.dataStore[i]+"\n";
 }
 return retStr;

empty()方法盤對隊列是否為空

function empty(){
 if(this.dataStore.length==0){
  return true;
 }else{
  return false;
 }
}

**Queue隊列的類

function Queue(){
 this.dataStore=[];
 this.enqueue=enqueue;
 this.dequeue=dequeue;
 this.front=front;
 this.back=back;
 this.toString=toString;
 this.empty=empty;
}
function enqueue(element){
 this.dataStore.push(element);
}
function dequeue(element){
 return this.dataStore.shift(element);
}
function front(){
 return this.dataStore[0];
}
function back(){
 return this.dataStore[this.dataStore.length-1];
}
function toString(){
 var retStr="";
 for(var i=0;i<this.dataStore.length;i++){
  retStr+=this.dataStore[i]+"\n";
 }
return retStr;
function empty(){
 if(this.dataStore.length==0){
  return true;
 }else{
  return false;
 }
}

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學運算用法總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)

希望本文所述對大家JavaScript程序設(shè)計有所幫助。

相關(guān)文章

最新評論