javascript封装栈

215 阅读1分钟

栈及其应用:

es5封装一个简单的栈(数组篇)

        function Stack () {
            this.items = []
        }
        // 封装栈的操作
        Stack.prototype.push = function (element) {
            this.items.push(element)
        }

        // 从栈中取出一个元素
        Stack.prototype.pop = function () {
            return this.items.pop()
        }

        // 查看栈顶的元素
        Stack.prototype.peek = function () {
            return this.items[this.items.length - 1]
        }

        // 判断栈是否为空
        Stack.prototype.isEmpty = function () {
            return this.items.length === 0
        }

        // 查看栈中的元素个数
        Stack.prototype.size = function () {
            return this.items.length
        }

        // toString() 方法
        Stack.prototype.toString = function () {
            return this.items.join()
        }

应用这个栈空间

        // 利用栈来实现一个将十进制的数转换为二进制的函数
        function decTobin (decNum) {
            let stack = new Stack()
            while (decNum > 0) {
                // 将进制对2取余放入栈中
                stack.push(decNum % 2)
                decNum = Math.floor(decNum / 2)
            }
            // 循环结束之后将二进制从栈中取出
            let binStr = ''
            while (!stack.isEmpty()) {
                binStr += stack.pop()
            }
            return binStr
        }

es6封装一个栈(对象篇)

// 使用js的类来实现栈
class Stack {
  constructor() {
    this.count = 0;
    this.items = {};
  }

  // 向栈中插入元素
  push(element) {
    this.items[this.count] = element;
    this.count++;
  }

  // 返回栈空间的大小
  size() {
    return this.count;
  }

  // 验证栈空间是否为空
  isEmpty() {
    return this.count === 0;
  }

  // 从栈空间中删除最后一个元素,并返回最后一个元素
    pop() {
        if (this.isEmpty()) {
          return undefined
        }
        this.count--
        const res = this.items[this.count]
        delete this.items[this.count]
        return res
    }
    
    // 访问栈顶的元素
    peek() {
        if (this.isEmpty()) {
            return undefined
        }
        return this.items[this.count - 1]
    }

    // 清空栈
    clear() {
        this.count = 0
        this.items = {}

        // while (!this.isEmpty()) {
        //     this.pop()
        // }
    }

    // 将栈中的元素从栈低到栈顶拼成一个字符串
    toString() {
        if (this.isEmpty()) {
            return ''
        }
        let objString = `${this.items[0]}`
        for (let i = 1; i < this.count; i++) {
            objString = `${objString}, ${this.items[this.count]}`
        }
        return objString
    }
}

es6 封装一个栈(数组篇)

// 在这里创建一个简单的stack类
class stackArray {
    constructor() {
        this.items = []
    }

    // push方法
    push(element) {
        this.items.push(element)
    }
    // pop方法,移除栈顶的元素,同时返回栈顶的元素
    pop() {
         return this.items.pop()
    }

    // peek 方法查看栈顶的元素
    peek() {
        return this.items[this.items.length - 1]
    }

    // 检查这个栈是否为空,如果为空返回true,否则返回false
    isEmpty() {
        return this.items.length === 0
    }

    // 查看栈的长度
    size() {
        return this.items.length
    }

    // 清空这个栈
    clear() {
        this.items = []
    }
}