zoukankan      html  css  js  c++  java
  • js 实现栈和队列

    js实现栈或者队列有两种方式:

    1.数组:数组本身提供栈方法(push,pop),队列方法(push,shift)。

    代码实现(栈):

    /*=======栈结构=======*/
    var stack=function(){
        this.data=[]
    
        this.push=push
        this.pop=pop
    
        this.clear=clear
        this.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)

    代码实现(队列):

    /*=======队列结构=======*/
    var queue=function(){
        this.data=[]
    
        this.enQueue=enQueue
        this.deQueue=deQueue
    
        this.clear=clear
        this.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.链表:构造链表结构,说白了就是链表的插入(尾插),移除(栈:末尾节点移除,队列:头结点移除)

    代码实现(栈):

    /*=====栈结构========*/
    var node=function(data){
        this.data=data
        this.next=null
    }
    var stack=function(){
        this.top=new node("top")
    
        this.push=push
        this.pop=pop
    
        this.clear=clear
        this.length=length
    }
    
    /*=======入栈=======*/
    var push=function(data){
        let newNode=new node(data)
        newNode.next=this.top
        this.top=newNode
    }
    /*=======出栈=======*/
    var pop=function(){
        let curr=this.top
        this.top=this.top.next
        curr.next=null
    }
    /*=======清空栈=======*/
    var clear=function(){
        this.top=new node('top')
    }
    /*=======栈长度=======*/
    var length=function(){
        let cnt=0
        while(this.top.data!=='top'){
            this.top=this.top.next
            cnt++
        }
        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())

    代码实现(队列):

    /*=====队列结构========*/
    var node=function(data){
        this.data=data
        this.next=null
    }
    var queue=function(){
        this.top=new node("top")
    
        this.enQueue=enQueue
        this.deQueue=deQueue
    }
    
    /*=======入队=======*/
    var enQueue=function(data){
        let newNode=new node(data)
        newNode.next=this.top
        this.top=newNode
    }
    /*=======出队=======*/
    var deQueue=function(){
        let curr=this.top
        while(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)
  • 相关阅读:
    JAVA学习总结-基础语法
    git stash save -a 遇到的坑 , 弹出匿藏错误
    TP5模型belongsTo和hasOne这两个方法的区别
    phpstorm设置的快捷键突然失效了,提示: IdeaVim ...
    layui 时间选择器 不要秒的选项
    SQL 判断表是否存在 数据表不存在是致命错误
    layui 第三方组件 eleTree 树组件 树形选择器
    tp5 ThinkPHP5 自定义异常处理类
    TP5隐藏url中的index.php
    phpstorm断点调试 php.ini 文件中 Xdebug 配置
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/9906752.html
Copyright © 2011-2022 走看看