js 实现栈和队列

摘要:js实现栈或者队列有两种方式: 1.数组:数组本身提供栈方法(push,pop),队列方法(push,shift)。 2.链表:构造链表结构,说白了就是链表的插入(尾插),移除(栈:末尾节点移除,队列:头结点移除)

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)


原文地址:https://www.cnblogs.com/xingguozhiming/p/9906752.html  

 

本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_1261