当前位置: 首页 > news >正文

TypeScript实现数据结构(一)栈,队列,链表

最近在学习typescript,就想着用typescript自己练习一些基本的数据结构,记录一下,读者有什么想法和建议也可以交流一下。

class Stack<T>{
    private items = null;
    constructor() {
        this.items = new Array<T>();
    }
    push(data: T): void {
        this.items.push(data);
    }
    pop(): T {
        return this.items.pop();
    }
    top(): T {
        return this.items[this.items.length - 1];
    }
    isEmpty(): boolean {
        return this.items.length === 0;
    }
    size(): number {
        return this.items.length;
    }
    clear(): void {
        this.items = new Array<T>();
    }
    print(): void {
        console.log(this.items);
    }
}

队列

class Queue<T>{
    private items = null;
    constructor() {
        this.items = new Array<T>();
    }
    enqueue(data: T): void {
        this.items.push(data);
    }
    dequeue(): T {
        return this.items.shift();
    }
    head(): T {
        return this.items[0];
    }
    size(): number {
        return this.items.length;
    }
    clear(): void {
        this.items = new Array<T>();
    }
    isEmpty(): boolean {
        return this.items.length === 0;
    }
    tail(): T {
        return this.items[this.items.length - 1];
    }
    print(): void {
        console.log(this.items);
    }
}


链表

class LinkNode<T>{
    public data: T;
    public next: LinkNode<T>;
    constructor(data: T) {
        this.data = data;
        this.next = null;
    }
}

class LinkList<T>{
  private head: Node<T>;
  private length: number;
  private tail: Node<T>;
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(data: T): boolean {
    let new_node = new Node(data);
    if (this.head == null) {
      this.head = new_node
      this.tail = new_node;
    } else {
      this.tail.next = new_node;
      this.tail = this.tail.next;
    }
    this.length++;
    return true;
  }

  len(): number {
    return this.length;
  }

  insert(index: number, data: T): boolean {
    if (index == this.length) {
      return this.append(data);
    } else {
      let insert_index = 1;
      let cur_node = this.head;
      while(insert_index < index) {
        cur_node = cur_node.next;
        insert_index++;
      }
      let next_node = cur_node.next;
      let new_node = new Node(data);
      cur_node.next = new_node;
      cur_node.next.next = next_node;
    }
    this.length++;
    return true;
  }

  remove(index): Node<T> {
    if (index < 0 || index >= this.length) {
      return null;
    } else {
      let del_node = null;
      if (index == 0) {
        del_node = this.head;
        this.head = this.head.next;
      } else {
        let del_index = 0;
        let pre_node = null;
        let cur_node = this.head;
        while (del_index < index) {
          del_index++;
          cur_node = cur_node.next;
        }
        pre_node = cur_node;
        cur_node = cur_node.next;
        pre_node.next = cur_node;
        //如果删除的是尾节点
        if (cur_node == null) {
          this.tail = pre_node;
        }
        this.length--;
        return del_node;
      }
    }
  }

  get(index): Node<T> {
    if (index < 0 || index > this.length) {
      return null;
    }
    let node_index = 0;
    let cur_node = this.head;
    while(node_index < index) {
      node_index++;
      cur_node = cur_node.next;
    }
    return cur_node;
  }

  print(): void {
    let cur = this.head;
    while(cur != null) {
      console.log(cur.data);
      cur = cur.next;
    }
  }

}

相关文章:

  • 阿里云移动端播放器高级功能介绍
  • CentOS 7 防火墙操作
  • React-flux杂记
  • Computed property XXX was assigned to but it has no setter
  • 阿里云服务器如何修改远程端口?
  • go的基本知识
  • extract-text-webpack-plugin用法
  • 《从0开始学Elasticsearch》—初识Elasticsearch
  • vue 打包 以及跨域问题组织
  • 深入了解以太坊
  • Python之 Virtualenv简明教程
  • dva中组件的懒加载
  • 「澳洋主数据项目」主数据促企业变革
  • phpstudy中apache的默认根目录的配置
  • 面试总结之人工智能AI(Artificial Intelligence)/ 机器学习(Machine Learning)
  • 《剑指offer》分解让复杂问题更简单
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 2019.2.20 c++ 知识梳理
  • android图片蒙层
  • js对象的深浅拷贝
  • PHP 的 SAPI 是个什么东西
  • Python进阶细节
  • rabbitmq延迟消息示例
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 不上全站https的网站你们就等着被恶心死吧
  • 排序(1):冒泡排序
  • 算法之不定期更新(一)(2018-04-12)
  • nb
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #13 yum、编译安装与sed命令的使用
  • $.ajax,axios,fetch三种ajax请求的区别
  • (06)Hive——正则表达式
  • (145)光线追踪距离场柔和阴影
  • (4)Elastix图像配准:3D图像
  • (第61天)多租户架构(CDB/PDB)
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (利用IDEA+Maven)定制属于自己的jar包
  • (六)激光线扫描-三维重建
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转)创业家杂志:UCWEB天使第一步
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .net Stream篇(六)
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?
  • .NET6实现破解Modbus poll点表配置文件
  • .NET成年了,然后呢?
  • .Net接口调试与案例
  • .net连接MySQL的方法
  • .NET微信公众号开发-2.0创建自定义菜单
  • @Builder用法