本文實(shí)例講述了JavaScript棧和隊(duì)列相關(guān)操作與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一、棧的介紹
棧就是和列表類似的一種數(shù)據(jù)結(jié)構(gòu),數(shù)據(jù)只能在棧頂添加或者刪除。棧是一種特殊的列表,棧內(nèi)的元素只能通過(guò)列表的一端訪問(wèn),成為棧頂。棧具有后進(jìn)先出的特點(diǎn),所以任何不在棧頂?shù)脑囟紵o(wú)法訪問(wèn)。
后進(jìn)先出(LIFO,last-in-first-out)的數(shù)據(jù)結(jié)構(gòu)。
對(duì)棧的操作
1.對(duì)棧的兩種主要操作為將一個(gè)元素壓入棧和將一個(gè)元素彈出棧。
	入棧:push();
	出棧:pop();
2.預(yù)覽棧頂?shù)脑?code style="margin: 3px auto 0px; padding: 2px 4px; outline: none; font-style: inherit; font-weight: inherit; background: rgb(249, 242, 244); width: 640px; line-height: 1.5; clear: both; font-size: 12px; border: 1px solid rgb(204, 204, 204); color: rgb(199, 37, 78); border-radius: 0px; font-family: Menlo, Monaco, Consolas, "Courier New", monospace;">peek();
	pop()雖然可以訪問(wèn)棧頂元素,但調(diào)用后,棧頂元素也從棧中永久性的被刪除。peek()方法只返回棧頂元素,并不刪除它。
對(duì)棧的實(shí)現(xiàn)
定義stack類的構(gòu)造函數(shù):
function Stack(){ this.dataStore=[];//數(shù)組dataStore保存棧內(nèi)元素,初始化為空數(shù)組 this.top=0; //top為棧頂位置,被構(gòu)造函數(shù)初始化為0,表示棧頂對(duì)應(yīng)數(shù)組的起始位置0 this.push=push; this.pop=pop; this.peek=peek;}	實(shí)現(xiàn)push()方法:
function push(element){ this.dataStore[this.top++]=element;}	實(shí)現(xiàn)pop()方法:
function pop(element){ return this.dataStore[--this.top]; //pop方法與push方法相反,它返回棧頂元素,同時(shí)將變量top的值減1}	實(shí)現(xiàn)peek()方法:
function peek(element){ return this.dataStore[this.top-1]; //peek方法返回?cái)?shù)組的第top-1個(gè)位置的元素,即棧頂元素。}	如果對(duì)一個(gè)空棧調(diào)用peek()方法,結(jié)果為undefined,因?yàn)闂J强盏模瑮m敍](méi)有任何元素。
	實(shí)現(xiàn)length():
	需要知道棧內(nèi)存儲(chǔ)了多少元素,length()方法通過(guò)返回變量top值得方法返回棧內(nèi)的元素個(gè)數(shù)。
function length(){ return this.top();}	實(shí)現(xiàn)clear():
	clear()將變量top的值設(shè)置為0,清空一個(gè)棧:
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;}二、隊(duì)列
隊(duì)列是一種列表,隊(duì)列智能在隊(duì)尾插入元素,在隊(duì)首刪除元素。隊(duì)列用于存儲(chǔ)按順序排列的數(shù)據(jù),先進(jìn)先出。
對(duì)隊(duì)列的操作
	隊(duì)列主要兩種操作,入隊(duì)和出隊(duì),入隊(duì)是在隊(duì)尾插入新元素,出隊(duì)是刪除隊(duì)首的元素。另一種是讀取隊(duì)頭的元素,peek();
	push()在數(shù)組末尾添加元素
names=[];names.push("hling");names.push("aling");print(names); //顯示hling,aling	shift()刪除數(shù)組中第一個(gè)元素
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()向隊(duì)尾添加一個(gè)元素
function enqueue(element){ this.dataStore.push(element);}	dequeue()向隊(duì)尾添加一個(gè)元素
function dequeue(element){ return this.dataStore.shift(element);}讀取隊(duì)首和隊(duì)尾的元素
function front(){ return this.dataStore[0];}function back(){ return this.dataStore[this.dataStore.length-1];}	toString()顯示隊(duì)列內(nèi)的所有元素
function toString(){ var retStr=""; for(var i=0;i<this.dataStore.length;i++){  retStr+=this.dataStore[i]+"/n"; } return retStr;	empty()方法盤對(duì)隊(duì)列是否為空
function empty(){ if(this.dataStore.length==0){  return true; }else{  return false; }}**Queue隊(duì)列的類
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; }}希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
| 
 
 | 
新聞熱點(diǎn)
疑難解答