数据结构之 - 栈

48 阅读1分钟
let Stack = function () {
  let items = [];
  this.push = function (ele) {
    items.push(ele);
  };
  this.getItems = function () {
    return items;
  };
  this.pop = function () {
    return items.pop();
  };
  // 查看栈顶元素
  this.peek = function () {
    return items[items.length - 1];
  };
  this.isEmpty = function () {
    return items.length === 0;
  };
  this.clear = function () {
    items = [];
  };
  this.size = function () {
    return items.length;
  };
};

// 十进制转二进制

var tenToTwo = function (number) {
  let stack = new Stack();
  let yushu;
  var str = "";
  while (number > 0) {
    yushu = number % 2;
    stack.push(yushu);
    number = Math.floor(number / 2);
  }
  while (!stack.isEmpty) {
    str += stack.pop();
  }
  return str;
};

后进先出 栈作用: 在编程语言的编译器和内存中保存变量。方法调用 入栈和出栈都是操作栈顶元素 push pop peek isEmpty length

算法效率度量方法

散列表

相对于其他数据结构可以快速的定位到元素,不需要遍历所有的元素

散列表(哈希表) 散列算法

散列函数 两种


// 将 blob 数据储存为本地文件
export const saveBlobAsSystemFile = (blob: Blob, fileName: string) => {
const blobIns = new Blob([blob]);
const ElementA = document.createElement('a');
ElementA.style.display = 'none';
ElementA.href = URL.createObjectURL(blobIns);
ElementA.download = fileName;
document.body.appendChild(ElementA);
ElementA.click();
URL.revokeObjectURL(ElementA.href);
document.body.removeChild(ElementA);
};
// 为参数创建 FormData 实例
export const createFormDataByParams = (params: AnyObject) => {
const formData = new FormData();
if (!params) return formData;
Object.entries(params).forEach((item: any) => {
const [key, value] = item;
formData.append(key, value);
});
return formData;
};

二叉搜素树

左侧是比父节点小的值 右侧是比父节点大的值