数据结构 –栈与队列
栈(first in last out)
栈的封装:
//封装栈类 function Stack(){ //栈中的属性 this.items=[] //栈的相关操作 //1.将元素压入栈 Stack.prototype.push=function(element){ this.items.push(element) }, //2.从栈中取出元素 Stack.prototype.pop=function(){ return this.items.pop() }, //3.查看一下栈顶元素 Stack.prototype.peek=function(){ return this.items[this.items.length -1] }, //4.判断是否为空 Stack.prototype.isEmpty=function(){ return this.items.length==0 }, //5.获取元素个数 Stack.prototype.size=function(){ return this.items.length }, //6.toString方法 Stack.prototype.toString=function(){ let resultString = '' for (let i=0;i<this.items.length; i++){ resultString += this.items[i]+'' } return resultString } } <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 栈的使用: </span><br><span class="line"></span><br><span class="line">* ```javascript</span><br><span class="line"> //函数 :十进制转成二进制</span><br><span class="line"> function dec2bin(decNumber) {</span><br><span class="line"> //1.定义栈对象</span><br><span class="line"> let stack= new Stack()</span><br><span class="line"> //2.循环操作</span><br><span class="line"> while (decNumber>0){</span><br><span class="line"> //2.1获取余数,并且放到栈中</span><br><span class="line"> stack.push(decNumber % 2)</span><br><span class="line"> //2.2获取整除后的结果 作为下次运行的数字</span><br><span class="line"> decNumber=Math.floor(decNumber/ 2 )</span><br><span class="line"> }</span><br><span class="line"> //3.从栈中取出0 和 1</span><br><span class="line"> let binaryString=''</span><br><span class="line"> while (!stack.isEmpty()){</span><br><span class="line"> binaryString += stack.pop()</span><br><span class="line"> }</span><br><span class="line"> return binaryString</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
栈实现队列
function Squeue(){ let stack1=[],stack2=[] Squeue.prototype.Senqueue=function(node){ stack1.push(node) }, Squeue.prototype.Sdequeue=function(){ if(stack2.length>0){ return stack2.pop() }else { while(stack1.length>0){ stack2.push(stack1.pop()) } return stack2.pop() } }, <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">### 队列结构(FILO)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#### 队列的封装:</span><br><span class="line">![](https://lh3.googleusercontent.com/proxy/22AM0yZpzwydNKQmezZ6jPPsWoGj5SSmYG-FlGavh05ehxHIFIe0werYdr8h_c0yg6fWW1gOqwjhEapjlABgOS-RlebuGo1xpjejQzKCUdamRlOgiBwv77mvrQJV4O82bFJ0hnySA8l0GWVk2SXdIr6XPqzFKC4SNA8dwM5CZZ5v9ly7RKSbFR4sVCuugmrsfy4kMus)</span><br><span class="line">```javascript</span><br><span class="line"> function Queue() {</span><br><span class="line"> //属性</span><br><span class="line"> this.items = []</span><br><span class="line"> //方法</span><br><span class="line"> //1.将元素加入队列</span><br><span class="line"> Queue.prototype.enqeue = function (element) {</span><br><span class="line"> this.items.push(element)</span><br><span class="line"> }</span><br><span class="line"> //2.从队列中删除前端元素</span><br><span class="line"> Queue.prototype.dequeue = function () {</span><br><span class="line"> return this.items.shift()</span><br><span class="line"> }</span><br><span class="line"> //3.查看前端元素</span><br><span class="line"> Queue.prototype.front = function () {</span><br><span class="line"> return this.items[0]</span><br><span class="line"> }</span><br><span class="line"> //4.查看队列是否为空</span><br><span class="line"> Queue.prototype.isEmpty = function () {</span><br><span class="line"> return this.items.length === 0</span><br><span class="line"> }</span><br><span class="line"> //5.查看队列中元素的个数</span><br><span class="line"> Queue.prototype.size = function () {</span><br><span class="line"> return this.items.length</span><br><span class="line"> }</span><br><span class="line"> //6.toString方法</span><br><span class="line"> Queue.prototype.toString = function () {</span><br><span class="line"> let resultString = ''</span><br><span class="line"> for (let i = 0; i < this.items.length; i++) {</span><br><span class="line"> resultString += this.items[i] + ''</span><br><span class="line"> }</span><br><span class="line"> return resultString</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
击鼓传花
1 | //击鼓传花 |
队列实现栈
function QStack() { let queue = [] QStack.prototype.Stackpush = function (node) { var len = queue.length; //让len等于插入前的queue的长度 因为length会+1 queue.push(node); for (var i = 0; i < len; i++) { queue.push( queue.shift()); } }, QStack.prototype.Stackpop = function () { return queue.shift() },
QStack.prototype.toString = function () {
let resultString = ''
for (let i = 0; i < queue.length; i++) {
resultString += queue[i] + ' '
}
return resultString
}
}