【JavaScript版】数据结构和算法 1 -- 栈结构

平时我们在学习文档和资料的时候,看到的大都是一些静态类型的语言(Java、c#)实现的数据结构和算法。对于前端小伙伴来说不是太友好,今天特意学习了一下用JavaScriot语言实现的数据结构-栈结构。

1.首先看一下复杂度为O(n),实现的栈结构

/**
 * push() 添加一个新元素到栈顶
 * pop() 移除栈顶的元素
 * peek() 返回栈顶元素
 * iSEmpty() 如果栈里没有任何元素就返回true,否则返回false
 * clear() 移除栈里的所有元素
 * size() 返回栈里的元素个数
 */
class Stack {
    constructor() {
        this.items = []
    }
    push(element) {
        this.items.push(element)
    }
    pop() {
        return this.items.pop()
    }
    peek() {
        return this.items[this.items.length - 1]
    }
    isEmpaty() {
        return this.items.length === 0
    }
    clear() {
        this.items = [];
    }
    size() {
        return this.items.length
    }
    toString(){
        return this.items.toString()
    }
}
const stack = new Stack()
console.log(stack.isEmpaty())
stack.push(1)
stack.push(2)
stack.push(3)
console.log(stack.toString())
stack.pop()
console.log(stack.toString())
console.log(stack.peek())
console.log(stack.isEmpaty())
console.log(stack.size())
stack.clear()
console.log(stack.toString())

2.我们再用对象实现一边,负责度为O(1)的栈结构

/**
 * push() 添加一个新元素到栈顶
 * pop() 移除栈顶的元素
 * peek() 返回栈顶元素
 * iSEmpty() 如果栈里没有任何元素就返回true,否则返回false
 * clear() 移除栈里的所有元素
 * size() 返回栈里的元素个数
 * toString() 返回栈内的每一个元素
 */
class Stack {
    constructor() {
        this.count = 0;
        this.items = {}
    }
    push(element) {
        this.items[this.count] = element;
        this.count++
    }
    pop() {
        if (this.isEmpty()) {
            return undefined
        }
        this.count--
        const result = this.items[this.count]
        delete this.items[this.count]
        return result
    }
    peek() {
        if (this.isEmpty()) {
            return undefined
        }
        return this.items[--this.count]
    }
    size() {
        return this.count
    }
    isEmpty() {
        return this.count === 0
    }
    clear() {
        this.count = 0;
        this.items = {}
    }
    // toString() {
    //     if (this.isEmpty()) {
    //         return ''
    //     }
    //     let objString = ''
    //     for (let i = 0; i < this.count; i++) {
    //         objString += `${this.items[i]},`
    //     }
    //     return objString.slice(0, -1)
    // }
    toString() {
        if (this.isEmpty()) {
            return ''
        }
        let objString = `${this.items[0]}`
        for (let i = 1; i < this.count; i++) {
            objString = `${objString},${this.items[i]}`
        }
        return objString
    }
}

const stack = new Stack()
console.log(stack.isEmpty())
console.log(stack.toString())
stack.push(5)
stack.push(8)
console.log(stack.toString())
stack.pop()
console.log(stack.toString())

3. 基于栈结构我们来解决一个实际问题 实现十进制到二进制的转化

/**
 * 基于栈的思想解决一个十进制到二进制转化的问题
 */
function decimalToBinary(decNumber) {
    const remStack = new Stack()
    let number = decNumber
    let rem
    let binaryString = ''
    while (number > 0) {
        rem = Math.floor(number % 2)
        remStack.push(rem)
        number = Math.floor(number / 2)
    }
    while (!remStack.isEmpty()) {
        binaryString += remStack.pop().toString()
    }
    return binaryString

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值