日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js 实现栈和队列

發(fā)布時間:2025/3/21 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js 实现栈和队列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

js實(shí)現(xiàn)棧或者隊列有兩種方式:

1.數(shù)組:數(shù)組本身提供棧方法(push,pop),隊列方法(push,shift)。

代碼實(shí)現(xiàn)(棧):

/*=======棧結(jié)構(gòu)=======*/ var stack=function(){this.data=[]this.push=pushthis.pop=popthis.clear=clearthis.length=length } var push=function(element){this.data.push(element) } var pop=function(){this.data.pop() } var clear=function(){this.data.length=0 } var length=function(){return this.data.length; } //測試 var s=new stack() s.push('first') s.push('second') console.log(s) s.pop() console.log(s) // s.clear() console.log(s)

代碼實(shí)現(xiàn)(隊列):

/*=======隊列結(jié)構(gòu)=======*/ var queue=function(){this.data=[]this.enQueue=enQueuethis.deQueue=deQueuethis.clear=clearthis.length=length } var enQueue=function(element){this.data.push(element) } var deQueue=function(){this.data.shift() } var clear=function(){this.data.length=0 } var length=function(){return this.data.length; } //測試 var q=new queue() q.enQueue('first') q.enQueue('second') console.log(q) q.deQueue() console.log(q) q.clear() console.log(q)

?

2.鏈表:構(gòu)造鏈表結(jié)構(gòu),說白了就是鏈表的插入(尾插),移除(棧:末尾節(jié)點(diǎn)移除,隊列:頭結(jié)點(diǎn)移除)

代碼實(shí)現(xiàn)(棧):

/*=====棧結(jié)構(gòu)========*/ var node=function(data){this.data=datathis.next=null } var stack=function(){this.top=new node("top")this.push=pushthis.pop=popthis.clear=clearthis.length=length }/*=======入棧=======*/ var push=function(data){let newNode=new node(data)newNode.next=this.topthis.top=newNode } /*=======出棧=======*/ var pop=function(){let curr=this.topthis.top=this.top.nextcurr.next=null } /*=======清空棧=======*/ var clear=function(){this.top=new node('top') } /*=======棧長度=======*/ var length=function(){let cnt=0while(this.top.data!=='top'){this.top=this.top.nextcnt++}return cnt} /*=======測試=======*/ var s=new stack() s.push('first') s.push('second') console.log(s) s.pop() console.log(s) // s.clear() console.log(s.length())

代碼實(shí)現(xiàn)(隊列):

/*=====隊列結(jié)構(gòu)========*/ var node=function(data){this.data=datathis.next=null } var queue=function(){this.top=new node("top")this.enQueue=enQueuethis.deQueue=deQueue }/*=======入隊=======*/ var enQueue=function(data){let newNode=new node(data)newNode.next=this.topthis.top=newNode } /*=======出隊=======*/ var deQueue=function(){let curr=this.topwhile(curr.next.next!==null && curr.next.next.data!=='top'){curr=curr.next}if(curr.next.next.data==='top'){curr.next=curr.next.next} }/*=======測試=======*/ var q=new queue() q.enQueue('first') q.enQueue('second') console.log(q) q.deQueue() console.log(q)

?

轉(zhuǎn)載于:https://www.cnblogs.com/xingguozhiming/p/9906752.html

總結(jié)

以上是生活随笔為你收集整理的js 实现栈和队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。