国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > JS > 正文

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

2024-05-06 16:47:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平昌县| 太白县| 当阳市| 运城市| 临朐县| 衡阳市| 邢台县| 米脂县| 武宣县| 永吉县| 兰坪| 沙洋县| 牙克石市| 金秀| 阳信县| 城口县| 黄陵县| 栾城县| 岱山县| 双桥区| 沙湾县| 广平县| 宽城| 泾川县| 梁河县| 安岳县| 广德县| 交城县| 托克托县| 青岛市| 侯马市| 湛江市| 济源市| 凤山市| 海口市| 紫金县| 乐安县| 修水县| 广昌县| 黄骅市| 白河县|