zoukankan      html  css  js  c++  java
  • js 数据结构学习之栈

    • 实例:二进制转换
        divide(decNumber,base){
          const items = new WeakMap();
          class Stack {
            constructor(){
              items.set(this, []);
            }
            push(element){
              let s = items.get(this);
              s.push(element);
            }
            pop(){
              let s = items.get(this);
              let r = s.pop();
              return r;
            }
            peek(element){
              let s = items.get(this);
              return s[s.length -1];
            }
            isEmpty(element){
              let s = items.get(this);
              return s.length == 0;
            }
            size(element){
              let s = items.get(this);
              return s.length;
            }
            clear(element){
              let s = items.get(this);
              s = [];
            }
            print(element){
              let s = items.get(this);
              console.log(s.toString());
            }
          }
    
          var remStack = new Stack(),rem,baseString = '',digits = '0123456789ABCDEF';
          while(decNumber > 0){
            rem = Math.floor(decNumber % base);
            remStack.push(rem);
            decNumber = Math.floor(decNumber / base);
          }
    
          while(!remStack.isEmpty()){
            baseString += digits[remStack.pop()];
          }
          return baseString;
        }
    
    学贵有恒,而行胜于言
  • 相关阅读:
    java面向对象小总结
    eclipce 快捷键
    linux中查找和过滤的用法:find,grep
    shell脚本
    条形码和二维码
    Json简介
    牛客练习
    输入流操作
    Java编码
    maven入门
  • 原文地址:https://www.cnblogs.com/huangbinlooksgood/p/14305541.html
Copyright © 2011-2022 走看看