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)
  • 相关阅读:
    CentOS ping: unknown host 解决方法
    chmod 777 修改权限
    Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器[摘抄]
    PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
    unix:/tmp/php-cgi.sock
    Nginx虚拟主机配置教程
    nginx 浏览php的时候会变成下载
    sphinx的配置和管理.No2
    Sphinx以及coreseek的安装及使用 .No1
    76、android:supportsRtl 和 android:layout_marginEnd
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/9906752.html
Copyright © 2011-2022 走看看