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

首頁 > 編程 > JavaScript > 正文

JavaScript棧和隊列相關操作與實現方法詳解

2019-11-19 12:23:08
字體:
來源:轉載
供稿:網友

本文實例講述了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相關內容感興趣的讀者可查看本站專題:《JavaScript數據結構與算法技巧總結》、《JavaScript數學運算用法總結》、《JavaScript排序算法總結》、《JavaScript遍歷算法與技巧總結》、《JavaScript查找算法技巧總結》及《JavaScript錯誤與調試技巧總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺州市| 江达县| 教育| 潜江市| 南漳县| 偃师市| 呼伦贝尔市| 温州市| 镇平县| 马尔康县| 什邡市| 托克托县| 龙井市| 获嘉县| 孝义市| 周至县| 拉萨市| 白玉县| 梅河口市| 夹江县| 泽州县| 常宁市| 沁源县| 巴南区| 怀仁县| 新巴尔虎左旗| 鹿邑县| 衡阳县| 黄浦区| 台前县| 平定县| 沧州市| 天等县| 修武县| 枣强县| 孟津县| 齐河县| 普宁市| 镇巴县| 毕节市| 远安县|