算法篇

59 阅读1分钟

基本概念

  1. 数据结构是计算机中存储、组织数据的方式。它包含逻辑关系、存储关系及操作。
常见的数据结构包含:栈、队列、数组、链表、树、图、堆、散列表(常见)
[B树]即为加快树状结构存取速度而设计的资料结构,常被应用在资料库和档案系统上

也可自己创造,上面这些只是常用!不纠结了
那这些数据结构的由来到底是语言自带的?还是说是由编程者自定义这些,类型的由来只是前人智慧的结果记录?-------
数据结构与编程语言,没有具体的关系,编程语言都具有内建的数据结构,(现代编程语言及其[API]中都包含了多种预设的数据结构,
例如 C++ [标准模板库]中的容器、[Java集合框架]以及微软的[.NET Framework])但各种编程语言的数据结构常有不同之处,数据结构可透过[编程语言]加以实现。

js实现栈

function Stack() {
  this.count = 0;
  this.storage = {};

  this.push = function (value) {
    this.storage[this.count] = value;
    this.count++;
  }

  this.pop = function () {
    if (this.count === 0) {
      return undefined;
    }
    this.count--;
    var result = this.storage[this.count];
    delete this.storage[this.count];
    return result;
  }

  this.peek = function () {
    return this.storage[this.count - 1];
  }

  this.size = function () {
    return this.count;
  }
}