栈是一种非常常见的数据结构,在程序中的应用非常广泛

我们看一下数组

数组是一种线性结构,并且可以在数组的任意位置插入和删除数据。但是在某些时候需要,必须要对这种任意性进行限制,栈和队列就是比较常见的受限的线性结构

如果此时的数据比较多,就会导致效率比较低, 

 

现在再来看栈结构

栈结构示意图

 

 

 栈(stack)是一种受限的线性表,先进后出[LIFO](或者说是后进先出)

 

 

  • 其限制是仅允许在表的一端进行插入个删除运算,这一端被称为栈顶,相对的,把另一端称为栈低;
  • LIFO(last in first out)表示就是后进入的元素,第一个弹出栈空间。类似于自动餐托盘,最后当上面的托盘,往往先拿出去使用;
  • 向一个栈插入新元素又称为进栈、入栈、或压栈、它是把新元素放到栈顶元素上面,使之成为新的栈顶元素
  • 从一个栈删除元素又称为出栈或者是退栈,它是把栈顶元素删除,使其相邻的元素成为新的栈顶元素

 

 栈的操作

栈常见的操作:

<1>  push(element):添加一个新元素到栈顶的位置

<2>  pop():移除栈顶的元素,同时返回被移除的元素

<3>  peek():返回栈顶的元素,不对栈做任何修改

<4>  isEmpty():如果栈里没有任何元素就返回true,否则就返回false

<5>  size():返回栈里的元素个数。跟length属性相似

<6>  toString():将栈结构的内容以字符形式返回

 

栈结构的实现

 实现栈结构有两种比较常见的方式:

  1. 基于数组实现
  2. 基于链表实现
    //封装栈类
    function Stack() {
      //栈中的属性
      this.items = [] 
      // push(element): 添加一个新元素到栈顶的位置
      Stack.prototype.push=function(element){
        this.items.push(element)
      }
      // pop(): 移除栈顶的元素, 同时返回被移除的元素
      Stack.prototype.pop=function(){ 
        return this.items.pop()
      }
      // peek(): 返回栈顶的元素, 不对栈做任何修改
      Stack.prototype.peek=function(){
        return this.items[this.items.length-1]
      }
      // isEmpty(): 如果栈里没有任何元素就返回true, 否则就返回false
      Stack.prototype.isEmpty=function(){
        return this.items.length==0
      }
      // size(): 返回栈里的元素个数。 跟length属性相似
      Stack.prototype.size=function(){
        return this.items.length
      }
      // toString(): 将栈结构的内容以字符形式返回
      Stack.prototype.toString=function(){
        var result=''
        for(var i=0;i<this.items.length;i++){
          result += this.items[i]+' '
        }
        return result
      }
    }
    //使用栈
    var stack = new Stack()
    //验证添加
    stack.push(1)
    console.log(stack,'添加')
    //验证移除
    stack.pop()
    console.log(stack,'移除')
    //验证是否为空
    console.log(stack.isEmpty())

 

 使用栈将十进制数据转换成二进制数据

我们看一下十进制转换成二进制需要的步骤

 

 

   // //封装栈类
    function Stack() {
      //栈中的属性
      this.items = [] 
      // push(element): 添加一个新元素到栈顶的位置
      Stack.prototype.push=function(element){
        this.items.push(element)
      }
      // pop(): 移除栈顶的元素, 同时返回被移除的元素
      Stack.prototype.pop=function(){ 
        return this.items.pop()
      }
      // peek(): 返回栈顶的元素, 不对栈做任何修改
      Stack.prototype.peek=function(){
        return this.items[this.items.length-1]
      }
      // isEmpty(): 如果栈里没有任何元素就返回true, 否则就返回false
      Stack.prototype.isEmpty=function(){
        return this.items.length==0
      }
      // size(): 返回栈里的元素个数。 跟length属性相似
      Stack.prototype.size=function(){
        return this.items.length
      }
      // toString(): 将栈结构的内容以字符形式返回
      Stack.prototype.toString=function(){
        var result=''
        for(var i=0;i<this.items.length;i++){
          result += this.items[i]+' '
        }
        return result
      }
    }
    function decToBin(num) {
      //定义一个栈的对象,用来保存余数
      var stack = new Stack
      while (num > 0) {
        //获取余数并存入栈中
        stack.push(num % 2)
        //获取整除后的结果作为下一次运算
        num = Math.floor(num / 2)
      }
      //从栈中取出
      var result = ''
      while (!stack.isEmpty()) {
        result += stack.pop()
      }
      return result
    }
    //测试
    console.log(decToBin(100))

 

posted @ 2022-01-09 23:16  keyeking  阅读(123)  评论(0编辑  收藏  举报