本文實例講述了JavaScript棧和隊列相關操作與實現方法。分享給大家供大家參考,具體如下:
一、棧的介紹
棧就是和列表類似的一種數據結構,數據只能在棧頂添加或者刪除。棧是一種特殊的列表,棧內的元素只能通過列表的一端訪問,成為棧頂。棧具有后進先出的特點,所以任何不在棧頂的元素都無法訪問。
后進先出(LIFO,last-in-first-out)的數據結構。
對棧的操作
1.對棧的兩種主要操作為將一個元素壓入棧和將一個元素彈出棧。
入棧:push();
出棧:pop();
2.預覽棧頂的元素peek();
pop()
雖然可以訪問棧頂元素,但調用后,棧頂元素也從棧中永久性的被刪除。peek()方法只返回棧頂元素,并不刪除它。
對棧的實現
定義stack類的構造函數:
function Stack(){ this.dataStore=[];//數組dataStore保存棧內元素,初始化為空數組 this.top=0; //top為棧頂位置,被構造函數初始化為0,表示棧頂對應數組的起始位置0 this.push=push; this.pop=pop; this.peek=peek;}
實現push()
方法:
function push(element){ this.dataStore[this.top++]=element;}
實現pop()
方法:
function pop(element){ return this.dataStore[--this.top]; //pop方法與push方法相反,它返回棧頂元素,同時將變量top的值減1}
實現peek()
方法:
function peek(element){ return this.dataStore[this.top-1]; //peek方法返回數組的第top-1個位置的元素,即棧頂元素。}
如果對一個空棧調用peek()
方法,結果為undefined,因為棧是空的,棧頂沒有任何元素。
實現length()
:
需要知道棧內存儲了多少元素,length()
方法通過返回變量top值得方法返回棧內的元素個數。
function length(){ return this.top();}
實現clear()
:
clear()
將變量top的值設置為0,清空一個棧:
function clear(){ this.top=0;}
總結: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;}
二、隊列
隊列是一種列表,隊列智能在隊尾插入元素,在隊首刪除元素。隊列用于存儲按順序排列的數據,先進先出。
對隊列的操作
隊列主要兩種操作,入隊和出隊,入隊是在隊尾插入新元素,出隊是刪除隊首的元素。另一種是讀取隊頭的元素,peek()
;
push()
在數組末尾添加元素
names=[];names.push("hling");names.push("aling");print(names); //顯示hling,aling
shift()
刪除數組中第一個元素
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()
顯示隊列內的所有元素
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; }}
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答