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

发布时间:2019-07-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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;
    }
  }

}

脚本宝典总结

以上是脚本宝典为你收集整理的TypeScript实现数据结构(一)栈,队列,链表全部内容,希望文章能够帮你解决TypeScript实现数据结构(一)栈,队列,链表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: