当前位置: 首页 > 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)
  • 网络传输文件的问题
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 【前端学习】-粗谈选择器
  • 0基础学习移动端适配
  • 2019年如何成为全栈工程师?
  • 3.7、@ResponseBody 和 @RestController
  • axios 和 cookie 的那些事
  • Java 23种设计模式 之单例模式 7种实现方式
  • Java面向对象及其三大特征
  • js作用域和this的理解
  • log4j2输出到kafka
  • markdown编辑器简评
  • Mysql优化
  • ng6--错误信息小结(持续更新)
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • node 版本过低
  • Otto开发初探——微服务依赖管理新利器
  • Python利用正则抓取网页内容保存到本地
  • Service Worker
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • Vue.js 移动端适配之 vw 解决方案
  • windows下mongoDB的环境配置
  • 大数据与云计算学习:数据分析(二)
  • 技术发展面试
  • 马上搞懂 GeoJSON
  • 入口文件开始,分析Vue源码实现
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 手写双向链表LinkedList的几个常用功能
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 自动记录MySQL慢查询快照脚本
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • #Linux(make工具和makefile文件以及makefile语法)
  • #pragma pack(1)
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (03)光刻——半导体电路的绘制
  • (ZT)一个美国文科博士的YardLife
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)springboot 个人网页的网站 毕业设计031623