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;
    }
  }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值