Arkar Myat

Stacks in Typescript

May 19, 2023Arkar
CS

Implementing stack data structure in Typescript

// with dynamic arrays
class Stack {
  size: number;
  topIndex: number;
  data: string[];
  constructor() {
    this.size = 0;
    this.topIndex = -1;
    this.data = [];
  }
  private _resize() {
    if (this.topIndex === this.size - 1) {
      this.size = (this.size + 1) * 2;
    }
  }
  push(value: string) {
    this._resize();
    this.topIndex = this.topIndex + 1;
    this.data[this.topIndex] = value;
  }
  pop() {
    let last = null;
    if (this.topIndex > -1) {
      last = this.data[this.topIndex];
      this.data[this.topIndex] = null;
      this.topIndex -= 1;
      return last;
    } else {
      console.warn("Stack is empty");
      return null;
    }
  }
}

//  with Linked Lists
class Node {
  value: string;
  next: Node | null;
  constructor(value: string) {
    this.value = value;
    this.next = null;
  }
}

class Stack {
  top: Node | null;
  length: number;
  constructor() {
    this.top = null;
    this.length = 0;
  }
  push(value: string) {
    let newNode = new Node(value);
    if (this.length === 0) {
      this.top = newNode;
    } else {
      newNode.next = this.top;
      this.top = newNode;
    }
    this.length += 1;
  }
  getAll() {
    let current = this.top;
    for (let i = 0; i < this.length; i++) {
      console.log(current.value);
      current = current.next;
    }
  }
  pop() {
    let tmp = this.top.value;
    this.top = this.top.next;
    this.length -= 1;
    return tmp;
  }
}